首页 > 解决方案 > 如何让程序也能识别大写字母?

问题描述

我正在创建一个收银机,我必须使用扫描仪并且只能有 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]);
     }
   }
 }

标签: javaarrays

解决方案


推荐阅读