英文:
JAVA Method returns unexpected value
问题
我是一个非常新的Java程序员,下面的代码是我自己项目的第一次尝试。我相信代码很混乱,请原谅我。
在下面的代码中,用户被提示输入5个介于1和50之间的值。
我将输入的值存入一个int[]中。
我想验证这些数字是否在范围内,所以我将值传递给一个方法。
我的问题是:
如果值在范围内,它会被返回,然后for循环会递增以重复 - 正常行为
如果输入了一个无效的值,会进行检查,显示错误消息,并提示用户重新输入正确的值。
如果进行了一次无效的输入,并且在第二次尝试时输入了正确的值,正确的值会被返回 - 正常行为
如果进行了两次无效的输入,第二次无效输入会以某种方式被传回for循环,并被添加到数组中 - 不正常的行为
我确信我漏掉了一些简单的东西。
英文:
I am a very new java programmer, the below code is my first attempt at my own project. I'm certain the code is a mess, please forgive me.
In the below code the user is prompted to enter 5 values between 1 and 50.
I am placing the input values into an int[].
I want to verify that the numbers are in range so I pass the value to a method.
MY ISSUE:
If the value is in range it gets returned then the for loop increments to repeat - Good Behavior
If an invalid value is entered the check is done, error message is displayed and the user is prompted to reenter a proper value.
If one invalid entry is made and a proper value is entered on the second attempt, a correct value is returned - Good Behavior
If two invalid entries are made the second invalid entry somehow gets passed back to the for loop and gets added to array - BAD Behavior
I am certain there is something simple I am missing.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("you get 5 elements between 01 & 50:");
int a[] = new int[5];
System.out.println("\nEnter all the elements:");
for(int i = 0; i < 5;)
{
int b = in.nextInt();
a[i] = checkNum(b);
i++;
}
System.out.println("Numbers:" + Arrays.toString(a));
in.close();
}
static int checkNum(int z) {
Scanner s = new Scanner(System.in);
if (z>0 && z<51) {
return z;
} else {
System.out.println("Invalid Entry!! Enter a valid number between 01 & 50");
int qz = s.nextInt();
z = qz;
checkNum(qz);
}
return z;
}
答案1
得分: 0
问题出在你的 checkNum()
函数中,你在这里使用了递归,我不认为你知道这一点(如果你知道那很好)。
你需要 return
checkNum(qz)
的值,我稍微简化了你的逻辑。
static int checkNum(int z) {
if (z < 1 || z > 50) // 检查无效值
{
System.out.println("无效输入!! 请输入一个在01到50之间的有效数字");
Scanner s = new Scanner(System.in);
return checkNum(s.nextInt());
}
return z;
}
英文:
The problem resides in your checkNum()
, you are using recursion here, I don't think you know this (if you do that's great).
You need to return
the checkNum(qz) value, I have simplified your logic a bit.
static int checkNum(int z) {
if (z<1 || z>50) // check for false value
{
System.out.println("Invalid Entry!! Enter a valid number between 01 & 50");
Scanner s = new Scanner(System.in);
return checkNum(s.nextInt());
}
return z;
}
答案2
得分: 0
你可以采用更简单的方法,假设用户输入的是数字而不是其他字符(会引发异常)。
public static void main(String[] args) {
System.out.print("在01和50之间输入5个元素:");
Scanner in = new Scanner(System.in);
int a[] = new int[5];
int i = 0;
do {
System.out.println("输入一个元素:");
int b = in.nextInt();
if (b > 0 && b < 51){
a[i] = b;
i++;
} else {
System.out.println("无效输入!请输入01到50之间的有效数字。");
}
} while(i < 5);
in.close();
System.out.println("数字:" + Arrays.toString(a));
}
在这里,您要求输入有效的数据,直到达到5个的限制。因为您事先不知道用户将提供多少次输入,直到满足条件为止,使用了do-while
循环。
英文:
You could follow a simpler approach assuming the user will input numbers and not other characters (that would throw an exception),
public static void main(String[] args) {
System.out.print("you get 5 elements between 01 & 50:");
Scanner in = new Scanner(System.in);
int a[] = new int[5];
int i = 0;
do{
System.out.println("Enter an element:");
int b = in.nextInt();
if (b > 0 && b < 51){
a[i] = b;
i++;
}else{
System.out.println("Invalid Entry!! Enter a valid number between 01 & 50");
}
}while(i < 5);
in.close();
System.out.println("Numbers:" + Arrays.toString(a));
}
Here you're asking for valid input until the threshold of 5 is reached. Because you don't know beforehand how many times the user will provide input, until the criteria are met, a do-while
loop is used.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论