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]);
}
}
}
解决方案
推荐阅读
- apache-spark - 如何在火花中执行多个连接数据帧?
- sql - impala sql 只选择一定长度的数字
- javascript - NodeJS服务器更改JSON对象的键名
- imap - 显示电子邮件的节点 imap
- c - 从 stat(2) 打印信息
- python - 在 Python pandas 数据帧上并行化反向地理编码功能
- ios - Swift - 更改幻灯片的大小以删除 tableView 中的按钮
- python - unittest - 比较列表,不考虑顺序
- javascript - Onclick 功能仅显示一秒钟的内容
- javascript - React/Javascript - 需要一个更好的进度条逻辑解决方案