英文:
Java - array size not updating
问题
public class Occurrence
{
public static void main(String[] args)
{
// variables
Scanner keyboard = new Scanner(System.in);
int numInputs = 1, temp;
int[] numbers = new int[31];
int[] count = new int[31];
boolean success = false;
// start of program
System.out.println("How many input values [max:30]?");
// while no valid input
while (!success)
{
try
{
numInputs = keyboard.nextInt(); // get a number
numInputChecker(numInputs); // is it valid?
success = true; // ok
}
catch (Exception e) // else get a new number
{
keyboard.nextLine();
System.out.println("Whole numbers 1 through 30 only, please.");
}
}
// reset the loop checker
success = false;
// read numbers to fill that array
System.out.println("Enter " + numInputs + " numbers.");
for (int i = 0; i < numInputs; i++) // from 0 to max number
{
while (!success) // while no valid number
{
try
{
numbers[i] = keyboard.nextInt(); // fill the current cell with a number
numberChecker(numbers[i]); // is it valid?
success = true; // ok
}
catch (Exception e) // else get a new number
{
keyboard.nextLine();
System.out.println("Whole numbers 0 through 9 only, please.");
}
}
}
// take the input and count each use of element
for (int i = 0; i < numbers.length; i++) // for 0 to max number
{
temp = numbers[i]; // get the current value of the cell
count[temp]++; // add the use of that value to a new array's cell
}
for (int i = 0; i < count.length; i++) // from 0 to 9 (expected)
{
if (count[i] > 0 && count[i] == 1) // if cell not empty
{
System.out.println(i + " " + count[i]); // print the current cell and how many times it was used
}
}
}
static void numInputChecker(int integer) throws Exception
{
if ((integer < 1) || (integer > 30)) // if 0 or negative, or if 31+
{
throw new Exception(); // say no
}
}
static void numberChecker(int integer) throws Exception
{
if ((integer < 0) || (integer > 9)) // if negative or 10+
{
throw new Exception(); // say no
}
}
}
英文:
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.
public class Occurrence
{
public static void main(String[] args)
{
//variables
Scanner keyboard = new Scanner(System.in);
int numInputs = 1, temp;
int[] numbers = new int[31];
int[] count = new int[31];
boolean success = false;
//start of program
System.out.println("How many input values [max:30]?");
//while no valid input
while (!success)
{
try
{
numInputs = keyboard.nextInt(); //get a number
numInputChecker(numInputs); //is it valid?
success = true; //ok
}
catch (Exception e) //else get a new number
{
keyboard.nextLine();
System.out.println("Whole numbers 1 through 30 only, please.");
}
}
//reset the loop checker
success = false;
//read numbers to fill that array
System.out.println("Enter " + numInputs + " numbers.");
for(int i = 0; i < numInputs; i++) //from 0 to max number
{
while (!success) //while no valid number
{
try
{
numbers[i] = keyboard.nextInt(); //fill the current cell with a number
numberChecker(numbers[i]); //is it valid?
success = true; //ok
}
catch (Exception e) //else get a new number
{
keyboard.nextLine();
System.out.println("Whole numbers 0 through 9 only, please.");
}
}
}
//take the input and count each use of element
for (int i = 0; i< numbers.length; i++) //for 0 to max number
{
temp = numbers[i]; //get the current value of the cell
count[temp]++; //add the use of that value to a new array's cell
}
for(int i = 0; i < count.length; i++) //from 0 to 9 (expected)
{
if (count[i] > 0 && count[i] == 1) //if cell not empty
{
System.out.println(i + " " + count[i]); //print the current cell and how many times it was used
}
}
}
static void numInputChecker(int integer) throws Exception
{
if ((integer < 1) || (integer > 30)) //if 0 or negative, or if 31+
{
throw new Exception(); //say no
}
}
static void numberChecker(int integer) throws Exception
{
if ((integer < 0) || (integer > 9)) //if negative or 10+
{
throw new Exception(); //say no
}
}
}
</details>
# 答案1
**得分**: 3
代码中第二个循环在填充数组时出现了逻辑错误,因为 success 变量在后续的 while 循环中没有被重置。修复方法如下:
```java
for (int i = 0; i < numInputs; i++) //从 0 到最大数
{
while (!success) //当无有效数字时
{
try {
numbers[i] = keyboard.nextInt(); //将当前单元格填充为数字
numberChecker(numbers[i]); //是否有效?
success = true; //有效
} catch (Exception e) //否则获取一个新数字
{
keyboard.nextLine();
System.out.println("仅限 0 到 9 的整数,请重新输入。");
}
}
success = false; // [amsilf]: 这是循环重置
}
注意:上述内容为你要翻译的代码部分,不包含其他额外的内容。
英文:
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:
for (int i = 0; i < numInputs; i++) //from 0 to max number
{
while (!success) //while no valid number
{
try {
numbers[i] = keyboard.nextInt(); //fill the current cell with a number
numberChecker(numbers[i]); //is it valid?
success = true; //ok
} catch (Exception e) //else get a new number
{
keyboard.nextLine();
System.out.println("Whole numbers 0 through 9 only, please.");
}
}
success = false; // [amsilf]: That's the cycle reset
}
答案2
得分: 1
可能会出现java.util.NoSuchElementException
并捕获错误以处理此块。
在使用keyboard.nextInt()
之前使用keyboard.hasNextInt()
。
像这样!
try {
keyboard.hasNextInt(); // 在这里!
numInputs = keyboard.nextInt(); // 获取一个数字
numInputChecker(numInputs); // 验证它是否有效?
success = true; // 行
}
...
英文:
Probably, java.util.NoSuchElementException
occurs and catches the error handling the block.
Use keyboard.hasNextInt()
before keyboard.nextInt()
.
Like this!
try {
keyboard.hasNext(); // here!
numInputs = keyboard.nextInt(); //get a number
numInputChecker(numInputs); //is it valid?
success = true; //ok
}
...
答案3
得分: 0
在最后的 for 循环中应该是这样的:
for (int i = 0; i < count.length; i++) //从 0 到 31(预期)
{
if (count[i] > 0) //如果单元格不为空
{
System.out.println(i + " " + count[i]); //打印当前单元格及其使用次数
}
}
英文:
In the last for loop should be that
for(int i = 0; i < count.length; i++) //from 0 to 31(expected)
{
if (count[i] > 0) //if cell not empty
{
System.out.println(i + " " + count[i]); //print the current cell and how many times it was used
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论