【问题标题】:Need to turn char into lower case and replace it in Java [duplicate]需要将char转换为小写并在Java中替换它[重复]
【发布时间】: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


【解决方案1】:

我会完全删除indexOfh :您可以将OR 一堆语句放在一起。这是安全的,因为 &gt;=evaluated before ||

// Set the index value to "h" or "H"
boolean containsh = value.indexOf('h') >= 0
    || value.indexOf('H') >= 0;

此外,您可以通过使用startsWithendsWith 忽略中间的“H”或“h”来使您的要求更严格。

// Set the index value to "h" or "H"
boolean containsh = value.startsWith("h")
    || value.startsWith("H") || value.endsWith("h")
    || value.endsWith("H");

此外,由于您从不关闭Scanner,因此您有资源泄漏:您可以使用try-with-resources 让Java 为您管理它。这与其他一些简化一起产生了一个像这样的新版本。

// Import scanner class
import java.util.Scanner;

// Create class and method
public class so64283526 {
    public static void main(String[] args) {

        // Create scanner object and set scanner variables
        try (Scanner inp = new Scanner(System.in)) {
            System.out.println("Press any key to start");
            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];

            while (counter++ < 5) {
                String value = inp.nextLine();
                value.toLowerCase();

                // Set the index value to "h" or "H"
                boolean containsh = value.startsWith("h")
                        || value.startsWith("H") || value.endsWith("h")
                        || value.endsWith("H");

                if (containsh) { // Validate h at beginning or end
                    numbers[index] = Double
                            .parseDouble(value.toLowerCase().replace("h", ""));
                    index++;
                    System.out.println(
                            "HST will be taken account for this value");
                }
            }
            System.out.println("HST Values:");

            for (int i = 0; i < numbers.length; i++) {
                System.out.println(numbers[i]);
            }
        }
    }
}

结果:

Press any key to start

Enter the amount of each item
Upto 5 inputs are allowed!

1
2H
HST will be taken account for this value
H3
HST will be taken account for this value
h4.7
HST will be taken account for this value
5.h6
HST Values:
2.0
3.0
4.7
0.0
0.0

【讨论】:

    猜你喜欢
    • 2011-10-03
    • 2021-11-09
    • 1970-01-01
    • 2011-01-11
    • 2020-12-04
    • 2015-07-14
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多