读取用户的整数,然后显示输入的数字列表和每个值的频率。输入的数量应在1-30之间变化,并且值接受0-9。
我的问题是,即使我将数组更改为int[] numbers = new int[numInputs];
,它也始终默认为1。在第一个for
循环期间,numInputs
似乎被写为1,但即使更改了numInputs
的值,它仍然上限为1。我确信我的逻辑是错误的,但我不确定在哪里。
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
}
}
}
逻辑在第二个循环中被破坏,因为成功变量不会在随后的while循环中重置。这样修复它很容易:
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
}
可能会发生java.util.NoSuchElementException
并捕获处理块的错误。
在keyboard.nextInt()
之前使用keyboard.hasNextInt()
。
像这样!
try {
keyboard.hasNext(); // here!
numInputs = keyboard.nextInt(); //get a number
numInputChecker(numInputs); //is it valid?
success = true; //ok
}
...
在最后一个for循环中应该是
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
}
}