java如何使程序也能识别大写字母?
我正在创建一个收银机,我必须使用扫描仪,只能有5个输入金额。它还必须包括HST(统一销售税),也就是说,在金额之后或之前只有一个“h”或“h”。在一些帮助下,我让程序识别小写字母h,但我如何让程序也识别大写字母“h”
代码:
// Import scanner class
import java.util.Scanner;
// Create class and method
class Main {
public static void main(String[] args) {
// Create scanner object and set scanner variables
Scanner inp = new Scanner(System.in);
System.out.println("Press any key to start");
String key = inp.nextLine();
System.out.println("\nEnter the amount of each item");
System.out.println("Upto 5 inputs are allowed!\n");
// Initialize counter and index variables to use it in the while loop
int counter = 0;
int index = 0;
// Create a double array variable, and set the limit to 5
double[] numbers = new double[5];
// Create a boolean variable to use it in the while loop
boolean go = true;
while(go) {
String value = inp.nextLine();
// Set the index value to "h" or "H"
int indexOfh = value.indexOf("h");
boolean containsh = indexOfh == 0 || indexOfh == (value.length()-1);
if(containsh){ //Validate h at beginning or end
numbers[index] = Double.parseDouble(value.replace("h", ""));
index++;
System.out.println("HST will be taken account for this value");
}
counter++;
if (counter == 5){
go = false;
}
}
System.out.println("HST Values:");
for(int i=0; i< numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
共 (0) 个答案