Java数组用户输入验证



新版

这里是Java初学者。我的任务是做一个简单的Java控制台应用程序来监控一家四口猴子的食物消耗。应用要求为:

二维4x7阵列;4只猴子,每周7天

整个猴子家族每天的平均食物量。

每只猴子每周平均进食量。

猴子一周中每天吃的最少的食物。

一只猴子一周中每天吃的最多的食物。

没有负数。

用户输入介于1磅和50磅之间。

到目前为止,我有平均每周的食物消耗量,每个猴子部分工作的食物消耗最多和最少。当我到达他们那里时,我会处理剩下的。我希望代码尽可能简单。

然而,我一直停留在用户输入验证部分。我希望能够针对1到50之间的正整数以外的任何东西进行验证。当应用程序运行时,编译器会显示"找不到符号:变量col。col是数组中的列。我不确定我做错了什么。我很确定这是一件非常简单的事情,但我撞到了墙上,因为我看不出可能是什么。

有人能帮我看看我做错了什么,或者给我指明正确的方向吗?非常感谢!

*****************************新编辑:***************************

我有新的代码可以工作,但每只猴子的每周平均消耗量是不正确的。逻辑似乎还可以。我能做些什么来纠正这个问题?

谢谢

新代码:

package monkeybusiness;
import java.util.Scanner;
import java.util.InputMismatchException;
public class MonkeyBusiness {
public static void main(String[] args) {
System.out.println("Welcome to MonkeyBusiness!");
System.out.println("Feed Four Monkeys.n");
// Array - four monkeys (row) and seven days (col)
int monkeyarray[][] = new int[4][7];
//User input and validation
Scanner scanner = new Scanner(System.in);
for (int row = 0; row < 4; row++) {
for (int col = 0; col < 7; col++) {
boolean inputValidate = false;
int number = 0;
do {
System.out.println("Enter food amount (1-50) for Monkeys "
+ (row + 1) + " on Day " + (col + 1));
try {
number = scanner.nextInt();
if (number >= 1 && number <= 50) {
inputValidate = true;
} else {
System.out.println("Entry not in range. ");
scanner.nextLine();
}
} catch (InputMismatchException exception) {
System.out.println("Entry not a number.");
scanner.nextLine();
}
} while (!(inputValidate));
monkeyarray[row][col] = number;
}
}
// Calculate total daily average for family
int average;
int[] perday = new int[]{0, 0, 0, 0, 0, 0, 0};
for (int col = 0; col < 7; col++) {
for (int row = 0; row < 4; row++) {
perday[col] += monkeyarray[row][col];
}
}
int sum = 0;
for (int row = 0; row < 7; row++) {
sum += perday[row];
}
average = sum / 28;
System.out.println("");
System.out.println("*************************************************");
System.out.println("Daily average amount for "
+ "whole family is: " + average);
// Calculate average weekly amount of food for each monkey
// Monkey# 1
int avg1;
int sum1 = 0;
int[] perweek1 = new int[]{0, 0, 0, 0, 0, 0, 0};
for (int col = 0; col < 7; col++) {
for (int row = 0; row < 4; row++) {
perweek1[col] += monkeyarray[row][col];
}
}
for (int row = 0; row < 7; row++) {
sum1 += perweek1[row];
}
System.out.println("Monkey# 1: " + sum1);
avg1 = sum1 / 7;
System.out.println("Weekly average amount for "
+ "Monkey# 1 is: " + avg1);
// Monkey# 2
int avg2;
int sum2 = 0;
int[] perweek2 = new int[]{0, 0, 0, 0, 0, 0, 0};
for (int col = 0; col < 7; col++) {
for (int row = 0; row < 4; row++) {
perweek2[col] += monkeyarray[row][col];
}
}
for (int row = 0; row < 7; row++) {
sum2 += perweek2[row];
}
System.out.println("Monkey# 2: " + sum2);
avg2 = sum2 / 7;
System.out.println("Weekly average amount for "
+ "Monkey# 2 is: " + avg2);
// Monkey# 3
int avg3;
int sum3 = 0;
int[] perweek3 = new int[]{0, 0, 0, 0, 0, 0, 0};
for (int col = 0; col < 7; col++) {
for (int row = 0; row < 4; row++) {
perweek3[col] += monkeyarray[row][col];
}
}
for (int row = 0; row < 7; row++) {
sum3 += perweek3[row];
}
System.out.println("Monkey# 3: " + sum3);
avg3 = sum3 / 7;
System.out.println("Weekly average amount for "
+ "Monkey# 3 is: " + avg3);
// Monkey# 4
int avg4;
int sum4 = 0;
int[] perweek4 = new int[]{0, 0, 0, 0, 0, 0, 0};
for (int col = 0; col < 7; col++) {
for (int row = 0; row < 4; row++) {
perweek4[col] += monkeyarray[row][col];
}
}
for (int row = 0; row < 7; row++) {
sum4 += perweek4[row];
}
System.out.println("Monkey# 4: " + sum4);
avg4 = sum4 / 7;
System.out.println("Weekly average amount for "
+ "Monkey# 4 is: " + avg4);
// Calculate least amount of food for monkey
int minimum = monkeyarray[0][0];
for (int row = 0; row < 4; row++) {
for (int col = 0; col < 7; col++) {
if (minimum > monkeyarray[row][col]) {
minimum = monkeyarray[row][col];
}
}
}
System.out.println("Least amount of food by any monkey: " + minimum);
// Calculate most amount of food for monkey
int maximum = monkeyarray[0][0];
for (int row = 0; row < 4; row++) {
for (int col = 0; col < 7; col++) {
if (maximum < monkeyarray[row][col]) {
maximum = monkeyarray[row][col];
}
}
}
System.out.println("Most amount of food by any monkey: " + maximum);
System.out.println("*************************************************");
System.out.println("");
}
}

第一件事是‘col’不在for循环的范围内,您已经在for循环中编写了验证用户输入0到50之间值的条件。

if (val > 0 && val <= 50) {
for (int col = 0; col < 7; col++) {
monkeyarray[row][col] = input.nextInt();
}
}

如果你想要col,那么你可以有另一个for循环,如上面的代码所示

最新更新