需要将字符转换为小写并在Java中进行替换。

huangapple go评论74阅读模式
英文:

Need to turn char into lower case and replace it in Java

问题

// 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 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 in the while loop
        boolean go = true;

        while (go) {
            String value = inp.nextLine();
            value = value.toLowerCase(); // Convert input to lowercase
            // 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 the beginning or end
                numbers[index] = Double.parseDouble(value.replace("h", ""));
                index++;
                System.out.println("HST will be taken into 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]);
        }
    }
}
英文:

So I need to make a basic cash register that accepts 5 items in an array with a price on them. Nevertheless, some items will have HST(tax) included with them. To know which items have tax and which don't. The user will press h or H before or after entering the dollar amount. I have got most of the program working, but I cannot get my code to recognize the upper case H to put the tax in it.
Here is my code, Thx in advance for any information:

// 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(&quot;Press any key to start&quot;);
String key = inp.nextLine();
System.out.println(&quot;\nEnter the amount of each item&quot;);
System.out.println(&quot;Upto 5 inputs are allowed!\n&quot;);
// 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 &quot;h&quot; or &quot;H&quot;
int indexOfh = value.indexOf(&#39;h&#39;);
boolean containsh = indexOfh == 0 || indexOfh == (value.length()-1);
if(containsh){ //Validate h at beginning or end
numbers[index] = Double.parseDouble(value.replace(&quot;h&quot;, &quot;&quot;));
index++;
System.out.println(&quot;HST will be taken account for this value&quot;);
}
counter++;
if (counter == 5){
go = false;
}
}
System.out.println(&quot;HST Values:&quot;);
for(int i=0; i&lt; numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}

答案1

得分: 0

我将完整的代码内容翻译如下:

// 我会完全删除 `indexOfh`:你可以将一系列语句使用 `||` 进行逻辑“或”操作。这是安全的,因为 `&gt;=` 在 `||` 之前[求值][1]。

// 设置索引值为 "h" 或 "H"
boolean containsh = value.indexOf('h') >= 0
    || value.indexOf('H') >= 0;

// 此外,你可以通过使用 `startsWith` 和 `endsWith` 来更严格地满足你的需求,以忽略中间的 'H' 或 'h'。

// 设置索引值为 "h" 或 "H"
boolean containsh = value.startsWith("h")
    || value.startsWith("H") || value.endsWith("h")
    || value.endsWith("H");

// 另外,你的代码存在资源泄漏,因为你从未关闭 `Scanner`:你可以使用 [try-with-resources][2] 让 Java 来管理这个。结合一些其他简化,得到如下新版本。

// 导入 Scanner 类
import java.util.Scanner;

// 创建类和方法
public class so64283526 {
    public static void main(String[] args) {

        // 创建 scanner 对象并设置 scanner 变量
        try (Scanner inp = new Scanner(System.in)) {
            System.out.println("按任意键开始");
            System.out.println("\n输入每个项目的金额");
            System.out.println("最多允许输入 5 个值!\n");

            // 初始化计数器和索引变量以在循环中使用
            int counter = 0;
            int index = 0;

            // 创建双精度数组变量,并将限制设置为 5
            double[] numbers = new double[5];

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

                // 设置索引值为 "h" 或 "H"
                boolean containsh = value.startsWith("h")
                        || value.startsWith("H") || value.endsWith("h")
                        || value.endsWith("H");

                if (containsh) { // 验证是否在开头或结尾包含 'h'
                    numbers[index] = Double
                            .parseDouble(value.toLowerCase().replace("h", ""));
                    index++;
                    System.out.println(
                            "这个值将计算 HST");
                }
            }
            System.out.println("HST 值:");

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

运行结果:

按任意键开始
输入每个项目的金额
最多允许输入 5 个值!
1
2H
这个值将计算 HST
H3
这个值将计算 HST
h4.7
这个值将计算 HST
5.h6
HST 值:
2.0
3.0
4.7
0.0
0.0
[1]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
[2]: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
英文:

I would remove indexOfh entirely: you can OR a bunch of statements together. This is safe because &gt;= is evaluated before ||.

// Set the index value to &quot;h&quot; or &quot;H&quot;
boolean containsh = value.indexOf(&#39;h&#39;) &gt;= 0
|| value.indexOf(&#39;H&#39;) &gt;= 0;

Also, you can make your requirement a little tighter by using startsWith and endsWith to ignore an 'H' or 'h' in the middle.

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

Additionally, you have a resource leak because you never close your Scanner: you can use try-with-resources to have Java manage this for you. That, along with a few other simplifications, yields a new version like this.

// 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(&quot;Press any key to start&quot;);
System.out.println(&quot;\nEnter the amount of each item&quot;);
System.out.println(&quot;Upto 5 inputs are allowed!\n&quot;);
// 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++ &lt; 5) {
String value = inp.nextLine();
value.toLowerCase();
// Set the index value to &quot;h&quot; or &quot;H&quot;
boolean containsh = value.startsWith(&quot;h&quot;)
|| value.startsWith(&quot;H&quot;) || value.endsWith(&quot;h&quot;)
|| value.endsWith(&quot;H&quot;);
if (containsh) { // Validate h at beginning or end
numbers[index] = Double
.parseDouble(value.toLowerCase().replace(&quot;h&quot;, &quot;&quot;));
index++;
System.out.println(
&quot;HST will be taken account for this value&quot;);
}
}
System.out.println(&quot;HST Values:&quot;);
for (int i = 0; i &lt; numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
}

Results:

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

huangapple
  • 本文由 发表于 2020年10月10日 00:02:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64283526.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定