扫描器要求输入两次并且只将第二个输入存储到数组中
Scanner asking for input twice and only storing second input to an array
我正在 java 中使用条件创建一个 switch case 语句。如果一个人在 2 到 12 个月之间将一个项目添加到系统中,它将被添加到一个数组中。如果项目持续时间小于 2 或大于 12,系统应要求他们重新输入有效数字。
出于某种原因,我的系统提示他们输入两次数字,并且只将第二个条目存储到数组中:Image of terminal prompting twice & Image of second entry only being stored to the array.
我认为这是因为我已经 'myMonths[index] = sc.nextInt();' 声明了两次,但我不知道如何在它不存在两次的情况下绕过它。谁能弄清楚为什么会这样,我该如何纠正?任何帮助将不胜感激。这是我的代码:
//Menu loop
int myMonths[] = new int[5];
int index = 0;
while(choice !=6){
switch (choice){
case 1:
//int n = number of projects
int n = 1;
Scanner sc = new Scanner(System.in);
System.out.println("How many months was your project?");
for(int i=0; i<1; i++){
myMonths[index] = sc.nextInt();
//if months is lesser than 2/greater than 12
if((myMonths[index] < 2) || (myMonths[index] > 12)){
System.out.println("Enter a number between 2 and 12 months");}
//if months is between 2 and 12 add it to the array
for(int x=0; x<1; x++){
//read the elements of the array
myMonths[index++]=sc.nextInt();}
for(int i=0; i<1; i++){
myMonths[index] = sc.nextInt();// put this in a variable int a = sc.nextInt();
//if months is lesser than 2/greater than 12
if((myMonths[index] < 2) || (myMonths[index] > 12)){
System.out.println("Enter a number between 2 and 12 months");}//here your are just checking if provided value is between 2 or 12 but there is no logical branching i.e. an else
//if months is between 2 and 12 add it to the array
for(int x=0; x<1; x++){
//read the elements of the array
myMonths[index++]=sc.nextInt();}//since there is no else this block will always execute
你可以在你的代码中尝试这样的事情
for(int i=0; i<1; i++){
int ip = sc.nextInt();
//if months is lesser than 2/greater than 12
if((ip < 2) || ip > 12){
System.out.println("Enter a number between 2 and 12 months");
//here ask for ip again
ip = sc.nextInt();//if you wanna continue doing this until you get a valid ip use an infinite while loop with a flag for break --- see below
}else{
//code for saving ip to array whatever logic you might need
}
关于如何使用 while 循环提示 ip 直到它有效的示例
int ip;
boolean valid = false;
while(!valid){
ip = sc.nextInt();
if(condition){
//if codition is satisfied
valid = true;
}
//else sysout("invalid ip"); loop will continue until you get a valid ip
}
我正在 java 中使用条件创建一个 switch case 语句。如果一个人在 2 到 12 个月之间将一个项目添加到系统中,它将被添加到一个数组中。如果项目持续时间小于 2 或大于 12,系统应要求他们重新输入有效数字。 出于某种原因,我的系统提示他们输入两次数字,并且只将第二个条目存储到数组中:Image of terminal prompting twice & Image of second entry only being stored to the array.
我认为这是因为我已经 'myMonths[index] = sc.nextInt();' 声明了两次,但我不知道如何在它不存在两次的情况下绕过它。谁能弄清楚为什么会这样,我该如何纠正?任何帮助将不胜感激。这是我的代码:
//Menu loop
int myMonths[] = new int[5];
int index = 0;
while(choice !=6){
switch (choice){
case 1:
//int n = number of projects
int n = 1;
Scanner sc = new Scanner(System.in);
System.out.println("How many months was your project?");
for(int i=0; i<1; i++){
myMonths[index] = sc.nextInt();
//if months is lesser than 2/greater than 12
if((myMonths[index] < 2) || (myMonths[index] > 12)){
System.out.println("Enter a number between 2 and 12 months");}
//if months is between 2 and 12 add it to the array
for(int x=0; x<1; x++){
//read the elements of the array
myMonths[index++]=sc.nextInt();}
for(int i=0; i<1; i++){
myMonths[index] = sc.nextInt();// put this in a variable int a = sc.nextInt();
//if months is lesser than 2/greater than 12
if((myMonths[index] < 2) || (myMonths[index] > 12)){
System.out.println("Enter a number between 2 and 12 months");}//here your are just checking if provided value is between 2 or 12 but there is no logical branching i.e. an else
//if months is between 2 and 12 add it to the array
for(int x=0; x<1; x++){
//read the elements of the array
myMonths[index++]=sc.nextInt();}//since there is no else this block will always execute
你可以在你的代码中尝试这样的事情
for(int i=0; i<1; i++){
int ip = sc.nextInt();
//if months is lesser than 2/greater than 12
if((ip < 2) || ip > 12){
System.out.println("Enter a number between 2 and 12 months");
//here ask for ip again
ip = sc.nextInt();//if you wanna continue doing this until you get a valid ip use an infinite while loop with a flag for break --- see below
}else{
//code for saving ip to array whatever logic you might need
}
关于如何使用 while 循环提示 ip 直到它有效的示例
int ip;
boolean valid = false;
while(!valid){
ip = sc.nextInt();
if(condition){
//if codition is satisfied
valid = true;
}
//else sysout("invalid ip"); loop will continue until you get a valid ip
}