Java – 数组大小不更新

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

Java - array size not updating

问题

  1. public class Occurrence
  2. {
  3. public static void main(String[] args)
  4. {
  5. // variables
  6. Scanner keyboard = new Scanner(System.in);
  7. int numInputs = 1, temp;
  8. int[] numbers = new int[31];
  9. int[] count = new int[31];
  10. boolean success = false;
  11. // start of program
  12. System.out.println("How many input values [max:30]?");
  13. // while no valid input
  14. while (!success)
  15. {
  16. try
  17. {
  18. numInputs = keyboard.nextInt(); // get a number
  19. numInputChecker(numInputs); // is it valid?
  20. success = true; // ok
  21. }
  22. catch (Exception e) // else get a new number
  23. {
  24. keyboard.nextLine();
  25. System.out.println("Whole numbers 1 through 30 only, please.");
  26. }
  27. }
  28. // reset the loop checker
  29. success = false;
  30. // read numbers to fill that array
  31. System.out.println("Enter " + numInputs + " numbers.");
  32. for (int i = 0; i < numInputs; i++) // from 0 to max number
  33. {
  34. while (!success) // while no valid number
  35. {
  36. try
  37. {
  38. numbers[i] = keyboard.nextInt(); // fill the current cell with a number
  39. numberChecker(numbers[i]); // is it valid?
  40. success = true; // ok
  41. }
  42. catch (Exception e) // else get a new number
  43. {
  44. keyboard.nextLine();
  45. System.out.println("Whole numbers 0 through 9 only, please.");
  46. }
  47. }
  48. }
  49. // take the input and count each use of element
  50. for (int i = 0; i < numbers.length; i++) // for 0 to max number
  51. {
  52. temp = numbers[i]; // get the current value of the cell
  53. count[temp]++; // add the use of that value to a new array's cell
  54. }
  55. for (int i = 0; i < count.length; i++) // from 0 to 9 (expected)
  56. {
  57. if (count[i] > 0 && count[i] == 1) // if cell not empty
  58. {
  59. System.out.println(i + " " + count[i]); // print the current cell and how many times it was used
  60. }
  61. }
  62. }
  63. static void numInputChecker(int integer) throws Exception
  64. {
  65. if ((integer < 1) || (integer > 30)) // if 0 or negative, or if 31+
  66. {
  67. throw new Exception(); // say no
  68. }
  69. }
  70. static void numberChecker(int integer) throws Exception
  71. {
  72. if ((integer < 0) || (integer > 9)) // if negative or 10+
  73. {
  74. throw new Exception(); // say no
  75. }
  76. }
  77. }
英文:

Read whole numbers from a user, then display the list of numbers input and the frequency of each value. The number of inputs should vary from 1-30, and the values accept 0-9.

My issue is that the array always defaults to 1 even if I change it to int[] numbers = new int[numInputs];. It appears that numInputs gets written over to 1 during the first for loop but even changing the value of numInputs it still caps out at 1. I'm sure my logic is wrong but I'm not sure where.

  1. public class Occurrence
  2. {
  3. public static void main(String[] args)
  4. {
  5. //variables
  6. Scanner keyboard = new Scanner(System.in);
  7. int numInputs = 1, temp;
  8. int[] numbers = new int[31];
  9. int[] count = new int[31];
  10. boolean success = false;
  11. //start of program
  12. System.out.println(&quot;How many input values [max:30]?&quot;);
  13. //while no valid input
  14. while (!success)
  15. {
  16. try
  17. {
  18. numInputs = keyboard.nextInt(); //get a number
  19. numInputChecker(numInputs); //is it valid?
  20. success = true; //ok
  21. }
  22. catch (Exception e) //else get a new number
  23. {
  24. keyboard.nextLine();
  25. System.out.println(&quot;Whole numbers 1 through 30 only, please.&quot;);
  26. }
  27. }
  28. //reset the loop checker
  29. success = false;
  30. //read numbers to fill that array
  31. System.out.println(&quot;Enter &quot; + numInputs + &quot; numbers.&quot;);
  32. for(int i = 0; i &lt; numInputs; i++) //from 0 to max number
  33. {
  34. while (!success) //while no valid number
  35. {
  36. try
  37. {
  38. numbers[i] = keyboard.nextInt(); //fill the current cell with a number
  39. numberChecker(numbers[i]); //is it valid?
  40. success = true; //ok
  41. }
  42. catch (Exception e) //else get a new number
  43. {
  44. keyboard.nextLine();
  45. System.out.println(&quot;Whole numbers 0 through 9 only, please.&quot;);
  46. }
  47. }
  48. }
  49. //take the input and count each use of element
  50. for (int i = 0; i&lt; numbers.length; i++) //for 0 to max number
  51. {
  52. temp = numbers[i]; //get the current value of the cell
  53. count[temp]++; //add the use of that value to a new array&#39;s cell
  54. }
  55. for(int i = 0; i &lt; count.length; i++) //from 0 to 9 (expected)
  56. {
  57. if (count[i] &gt; 0 &amp;&amp; count[i] == 1) //if cell not empty
  58. {
  59. System.out.println(i + &quot; &quot; + count[i]); //print the current cell and how many times it was used
  60. }
  61. }
  62. }
  63. static void numInputChecker(int integer) throws Exception
  64. {
  65. if ((integer &lt; 1) || (integer &gt; 30)) //if 0 or negative, or if 31+
  66. {
  67. throw new Exception(); //say no
  68. }
  69. }
  70. static void numberChecker(int integer) throws Exception
  71. {
  72. if ((integer &lt; 0) || (integer &gt; 9)) //if negative or 10+
  73. {
  74. throw new Exception(); //say no
  75. }
  76. }
  77. }
  78. </details>
  79. # 答案1
  80. **得分**: 3
  81. 代码中第二个循环在填充数组时出现了逻辑错误,因为 success 变量在后续的 while 循环中没有被重置。修复方法如下:
  82. ```java
  83. for (int i = 0; i < numInputs; i++) //从 0 到最大数
  84. {
  85. while (!success) //当无有效数字时
  86. {
  87. try {
  88. numbers[i] = keyboard.nextInt(); //将当前单元格填充为数字
  89. numberChecker(numbers[i]); //是否有效?
  90. success = true; //有效
  91. } catch (Exception e) //否则获取一个新数字
  92. {
  93. keyboard.nextLine();
  94. System.out.println("仅限 0 到 9 的整数,请重新输入。");
  95. }
  96. }
  97. success = false; // [amsilf]: 这是循环重置
  98. }

注意:上述内容为你要翻译的代码部分,不包含其他额外的内容。

英文:

The logic is broken at the second cycle where you do feel up an array, because success variable isn't reset for a subsequent while loops. It's quite easy to fix it as such:

  1. for (int i = 0; i &lt; numInputs; i++) //from 0 to max number
  2. {
  3. while (!success) //while no valid number
  4. {
  5. try {
  6. numbers[i] = keyboard.nextInt(); //fill the current cell with a number
  7. numberChecker(numbers[i]); //is it valid?
  8. success = true; //ok
  9. } catch (Exception e) //else get a new number
  10. {
  11. keyboard.nextLine();
  12. System.out.println(&quot;Whole numbers 0 through 9 only, please.&quot;);
  13. }
  14. }
  15. success = false; // [amsilf]: That&#39;s the cycle reset
  16. }

答案2

得分: 1

可能会出现java.util.NoSuchElementException并捕获错误以处理此块。

在使用keyboard.nextInt()之前使用keyboard.hasNextInt()

像这样!

  1. try {
  2. keyboard.hasNextInt(); // 在这里!
  3. numInputs = keyboard.nextInt(); // 获取一个数字
  4. numInputChecker(numInputs); // 验证它是否有效?
  5. success = true; // 行
  6. }
  7. ...
英文:

Probably, java.util.NoSuchElementException occurs and catches the error handling the block.

Use keyboard.hasNextInt() before keyboard.nextInt().

Like this!

  1. try {
  2. keyboard.hasNext(); // here!
  3. numInputs = keyboard.nextInt(); //get a number
  4. numInputChecker(numInputs); //is it valid?
  5. success = true; //ok
  6. }
  7. ...

答案3

得分: 0

在最后的 for 循环中应该是这样的:

  1. for (int i = 0; i < count.length; i++) //从 0 到 31(预期)
  2. {
  3. if (count[i] > 0) //如果单元格不为空
  4. {
  5. System.out.println(i + " " + count[i]); //打印当前单元格及其使用次数
  6. }
  7. }
英文:

In the last for loop should be that

  1. for(int i = 0; i &lt; count.length; i++) //from 0 to 31(expected)
  2. {
  3. if (count[i] &gt; 0) //if cell not empty
  4. {
  5. System.out.println(i + &quot; &quot; + count[i]); //print the current cell and how many times it was used
  6. }
  7. }

huangapple
  • 本文由 发表于 2020年9月10日 09:27:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63821574.html
匿名

发表评论

匿名网友

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

确定