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

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

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

问题

  1. // Import scanner class
  2. import java.util.Scanner;
  3. // Create class and method
  4. class Main {
  5. public static void main(String[] args) {
  6. // Create scanner object and set scanner variables
  7. Scanner inp = new Scanner(System.in);
  8. System.out.println("Press any key to start");
  9. String key = inp.nextLine();
  10. System.out.println("\nEnter the amount of each item");
  11. System.out.println("Upto 5 inputs are allowed!\n");
  12. // Initialize counter and index variables to use in the while loop
  13. int counter = 0;
  14. int index = 0;
  15. // Create a double array variable and set the limit to 5
  16. double[] numbers = new double[5];
  17. // Create a boolean variable to use in the while loop
  18. boolean go = true;
  19. while (go) {
  20. String value = inp.nextLine();
  21. value = value.toLowerCase(); // Convert input to lowercase
  22. // Set the index value to "h" or "H"
  23. int indexOfh = value.indexOf('h');
  24. boolean containsh = indexOfh == 0 || indexOfh == (value.length() - 1);
  25. if (containsh) { // Validate "h" at the beginning or end
  26. numbers[index] = Double.parseDouble(value.replace("h", ""));
  27. index++;
  28. System.out.println("HST will be taken into account for this value");
  29. }
  30. counter++;
  31. if (counter == 5) {
  32. go = false;
  33. }
  34. }
  35. System.out.println("HST Values:");
  36. for (int i = 0; i < numbers.length; i++) {
  37. System.out.println(numbers[i]);
  38. }
  39. }
  40. }
英文:

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:

  1. // Import scanner class
  2. import java.util.Scanner;
  3. // Create class and method
  4. class Main {
  5. public static void main(String[] args) {
  6. // Create scanner object and set scanner variables
  7. Scanner inp = new Scanner(System.in);
  8. System.out.println(&quot;Press any key to start&quot;);
  9. String key = inp.nextLine();
  10. System.out.println(&quot;\nEnter the amount of each item&quot;);
  11. System.out.println(&quot;Upto 5 inputs are allowed!\n&quot;);
  12. // Initialize counter and index variables to use it in the while loop
  13. int counter = 0;
  14. int index = 0;
  15. // Create a double array variable, and set the limit to 5
  16. double[] numbers = new double[5];
  17. // Create a boolean variable to use it in the while loop
  18. boolean go = true;
  19. while(go) {
  20. String value = inp.nextLine();
  21. value.toLowerCase();
  22. // Set the index value to &quot;h&quot; or &quot;H&quot;
  23. int indexOfh = value.indexOf(&#39;h&#39;);
  24. boolean containsh = indexOfh == 0 || indexOfh == (value.length()-1);
  25. if(containsh){ //Validate h at beginning or end
  26. numbers[index] = Double.parseDouble(value.replace(&quot;h&quot;, &quot;&quot;));
  27. index++;
  28. System.out.println(&quot;HST will be taken account for this value&quot;);
  29. }
  30. counter++;
  31. if (counter == 5){
  32. go = false;
  33. }
  34. }
  35. System.out.println(&quot;HST Values:&quot;);
  36. for(int i=0; i&lt; numbers.length; i++) {
  37. System.out.println(numbers[i]);
  38. }
  39. }
  40. }

答案1

得分: 0

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

  1. // 我会完全删除 `indexOfh`:你可以将一系列语句使用 `||` 进行逻辑“或”操作。这是安全的,因为 `&gt;=` 在 `||` 之前[求值][1]。
  2. // 设置索引值为 "h" 或 "H"
  3. boolean containsh = value.indexOf('h') >= 0
  4. || value.indexOf('H') >= 0;
  5. // 此外,你可以通过使用 `startsWith` 和 `endsWith` 来更严格地满足你的需求,以忽略中间的 'H' 或 'h'。
  6. // 设置索引值为 "h" 或 "H"
  7. boolean containsh = value.startsWith("h")
  8. || value.startsWith("H") || value.endsWith("h")
  9. || value.endsWith("H");
  10. // 另外,你的代码存在资源泄漏,因为你从未关闭 `Scanner`:你可以使用 [try-with-resources][2] 让 Java 来管理这个。结合一些其他简化,得到如下新版本。
  11. // 导入 Scanner 类
  12. import java.util.Scanner;
  13. // 创建类和方法
  14. public class so64283526 {
  15. public static void main(String[] args) {
  16. // 创建 scanner 对象并设置 scanner 变量
  17. try (Scanner inp = new Scanner(System.in)) {
  18. System.out.println("按任意键开始");
  19. System.out.println("\n输入每个项目的金额");
  20. System.out.println("最多允许输入 5 个值!\n");
  21. // 初始化计数器和索引变量以在循环中使用
  22. int counter = 0;
  23. int index = 0;
  24. // 创建双精度数组变量,并将限制设置为 5
  25. double[] numbers = new double[5];
  26. while (counter++ < 5) {
  27. String value = inp.nextLine();
  28. value.toLowerCase();
  29. // 设置索引值为 "h" 或 "H"
  30. boolean containsh = value.startsWith("h")
  31. || value.startsWith("H") || value.endsWith("h")
  32. || value.endsWith("H");
  33. if (containsh) { // 验证是否在开头或结尾包含 'h'
  34. numbers[index] = Double
  35. .parseDouble(value.toLowerCase().replace("h", ""));
  36. index++;
  37. System.out.println(
  38. "这个值将计算 HST");
  39. }
  40. }
  41. System.out.println("HST 值:");
  42. for (int i = 0; i < numbers.length; i++) {
  43. System.out.println(numbers[i]);
  44. }
  45. }
  46. }
  47. }

运行结果:

  1. 按任意键开始
  2. 输入每个项目的金额
  3. 最多允许输入 5 个值!
  4. 1
  5. 2H
  6. 这个值将计算 HST
  7. H3
  8. 这个值将计算 HST
  9. h4.7
  10. 这个值将计算 HST
  11. 5.h6
  12. HST 值:
  13. 2.0
  14. 3.0
  15. 4.7
  16. 0.0
  17. 0.0
  18. [1]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
  19. [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 ||.

  1. // Set the index value to &quot;h&quot; or &quot;H&quot;
  2. boolean containsh = value.indexOf(&#39;h&#39;) &gt;= 0
  3. || 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.

  1. // Set the index value to &quot;h&quot; or &quot;H&quot;
  2. boolean containsh = value.startsWith(&quot;h&quot;)
  3. || value.startsWith(&quot;H&quot;) || value.endsWith(&quot;h&quot;)
  4. || 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.

  1. // Import scanner class
  2. import java.util.Scanner;
  3. // Create class and method
  4. public class so64283526 {
  5. public static void main(String[] args) {
  6. // Create scanner object and set scanner variables
  7. try (Scanner inp = new Scanner(System.in)) {
  8. System.out.println(&quot;Press any key to start&quot;);
  9. System.out.println(&quot;\nEnter the amount of each item&quot;);
  10. System.out.println(&quot;Upto 5 inputs are allowed!\n&quot;);
  11. // Initialize counter and index variables to use it in the while
  12. // loop
  13. int counter = 0;
  14. int index = 0;
  15. // Create a double array variable, and set the limit to 5
  16. double[] numbers = new double[5];
  17. while (counter++ &lt; 5) {
  18. String value = inp.nextLine();
  19. value.toLowerCase();
  20. // Set the index value to &quot;h&quot; or &quot;H&quot;
  21. boolean containsh = value.startsWith(&quot;h&quot;)
  22. || value.startsWith(&quot;H&quot;) || value.endsWith(&quot;h&quot;)
  23. || value.endsWith(&quot;H&quot;);
  24. if (containsh) { // Validate h at beginning or end
  25. numbers[index] = Double
  26. .parseDouble(value.toLowerCase().replace(&quot;h&quot;, &quot;&quot;));
  27. index++;
  28. System.out.println(
  29. &quot;HST will be taken account for this value&quot;);
  30. }
  31. }
  32. System.out.println(&quot;HST Values:&quot;);
  33. for (int i = 0; i &lt; numbers.length; i++) {
  34. System.out.println(numbers[i]);
  35. }
  36. }
  37. }
  38. }

Results:

  1. Press any key to start
  2. Enter the amount of each item
  3. Upto 5 inputs are allowed!
  4. 1
  5. 2H
  6. HST will be taken account for this value
  7. H3
  8. HST will be taken account for this value
  9. h4.7
  10. HST will be taken account for this value
  11. 5.h6
  12. HST Values:
  13. 2.0
  14. 3.0
  15. 4.7
  16. 0.0
  17. 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:

确定