【发布时间】:2020-10-10 00:45:19
【问题描述】:
所以我需要制作一个基本的收银机,它可以接受数组中的 5 件物品,上面有价格。不过,有些商品将包含 HST(税)。了解哪些项目有税哪些没有。用户将在输入美元金额之前或之后按 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();
value.toLowerCase();
// 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]);
}
}
}
【问题讨论】:
-
使用
value = value.toLowerCase();。 -
这能回答你的问题吗? Converting to upper and lower case in Java
-
@wilx 哦,那行得通,我想知道为什么它不能将其识别为小写,现在我意识到这是因为我没有将它分配给变量 lol
-
@K.Smith 是的,谢谢
标签: java