如何在Java中将值存储在数组中?

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

How would I store values in an array in java?

问题

我想在一个数组中存储非“h”值。所以给你一些背景,我需要制作一个基本的现金注册机,它接受一个包含价格的数组中的5个项目。然而,有些项目将包括HST(税金)。为了知道哪些项目有税收,哪些没有。用户将在输入美元金额之前或之后按下h或H。我已经将带有HST的值存储在一个数组中,但是如何存储非HST的值呢?

示例输入:

4.565H
H2.3435
4.565h
5.234
5.6576h

示例输出:

HST 值:
4.565
2.3435
4.565
5.6576

非-HST 值:
5.234


我的代码:

  1. import java.util.Scanner;
  2. class Main {
  3. public static void main(String[] args) {
  4. // 创建扫描器对象并设置扫描器变量
  5. Scanner inp = new Scanner(System.in);
  6. System.out.println("按任意键开始");
  7. String key = inp.nextLine();
  8. System.out.println("\n输入每个项目的金额");
  9. System.out.println("最多允许输入5个值!\n");
  10. // 初始化计数器和索引变量以在 while 循环中使用
  11. int counter = 0;
  12. int index = 0;
  13. // 创建一个 double 数组变量,并将限制设置为 5
  14. double[] numbers = new double[5];
  15. // 创建一个布尔变量以在 while 循环中使用
  16. boolean go = true;
  17. while (go) {
  18. String value = inp.nextLine();
  19. value = value.toLowerCase();
  20. // 将索引值设置为“h”或“H”
  21. int indexOfh = value.indexOf('h');
  22. boolean containsh = (indexOfh == 0 || indexOfh == value.length() - 1);
  23. if (containsh) { // 验证 h 在开头或结尾
  24. numbers[index] = Double.parseDouble(value.replace("h", ""));
  25. index++;
  26. System.out.println("此值将考虑 HST");
  27. }
  28. counter++;
  29. if (counter == 5) {
  30. go = false;
  31. }
  32. }
  33. System.out.println("HST 值:");
  34. for (int i = 0; i < numbers.length; i++) {
  35. System.out.println(numbers[i]);
  36. }
  37. }
  38. }
英文:

I want to store non "h" values in an array. So to give you a background, 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 stored the values with the HST in an array, but how would I store the non-HST values?

Sample input:

  1. 4.565H
  2. H2.3435
  3. 4.565h
  4. 5.234
  5. 5.6576h

Sample Output:

  1. HST Values:
  2. 4.565
  3. 2.3435
  4. 4.565
  5. 5.6576
  6. Non-HST Values
  7. 5.234

My Code:

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

答案1

得分: 1

我建议您改用ArrayList

  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. class Main {
  4. public static void main(String[] args) {
  5. // 创建扫描器对象并设置扫描器变量
  6. Scanner inp = new Scanner(System.in);
  7. System.out.println("按任意键开始");
  8. String key = inp.nextLine();
  9. System.out.println("\n输入每个项目的金额");
  10. System.out.println("最多允许输入5个金额!\n");
  11. // 初始化计数器和索引变量以在 while 循环中使用
  12. int counter = 0;
  13. // 创建一个双精度数组变量,并将限制设置为5
  14. ArrayList<Double> numbers = new ArrayList<Double>();
  15. // 创建一个布尔变量以在 while 循环中使用
  16. boolean go = true;
  17. while (go) {
  18. String value = inp.nextLine().toLowerCase();
  19. int indexOfH = value.indexOf('h');
  20. if (indexOfH == 0 || indexOfH == value.length() - 1) { // 验证是否在开头或结尾处有 'h'
  21. numbers.add(Double.parseDouble(value.replace("h", "")));
  22. System.out.println("这个值将考虑HST");
  23. }
  24. counter++;
  25. if (counter == 5)
  26. go = false;
  27. }
  28. System.out.println("HST 值:");
  29. System.out.println(numbers);
  30. }
  31. }
英文:

I would suggest you to work with ArrayList instead:

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

答案2

得分: 0

你只需要两个数组,一个用于存储 H 值,另一个用于存储非 H 值。为什么不使用列表呢?使用列表会更好。

然而,如果要求使用数组,只需通过检查 'H' 值将 h 值存储起来,如果 'H' 的索引为 -1,表示该值不包含 h,然后将这些值添加到另一个数组中,并在最后一起打印出来。

英文:

You just need two arrays, one for H values and the other for non H. Why not use lists? Lists would be better

However, if the requirement is arrays, just store the h values by checking 'H' values like you have, and if index of 'H' is -1 that means the value does not contain h, then just add those to the other array and print it out as well at the end.

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

发表评论

匿名网友

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

确定