[英]Scanner asking for input twice and only storing second input to an array
I'm making a switch case statement in java with conditions.我正在使用条件在 java 中制作一个 switch case 语句。 If a person adds a project to the system between 2 and 12 months long, it will be added to an array.
如果一个人在 2 到 12 个月之间将一个项目添加到系统中,它将被添加到一个数组中。 If the project duration is less than 2 or more than 12 the system should request them to re-enter a valid number.
如果项目持续时间小于 2 或大于 12,系统应要求他们重新输入有效数字。 For some reason, my system is prompting them to enter the number twice, and only storing the second entry to the array: Image of terminal prompting twice & Image of second entry only being stored to the array .
出于某种原因,我的系统提示他们输入数字两次,并且只将第二个条目存储到数组中: Image of terminal prompting twice & Image of second entry only being stored to the array 。
I am thinking it's because I have 'myMonths[index] = sc.nextInt();'我想这是因为我有 'myMonths[index] = sc.nextInt();' declared twice, but I dont know how I can get around it without it being there twice.
声明了两次,但我不知道如何在不出现两次的情况下绕过它。 Can anyone figure out why this is happening and how I could rectify it?
谁能弄清楚为什么会这样,我该如何纠正? Any help would be greatly appreciated.
任何帮助将不胜感激。 Here's my code:
这是我的代码:
//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
You can try something like this in your code你可以在你的代码中尝试这样的事情
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
}
An example on how you could use while loop for prompting ip until it's valid一个关于如何使用 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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.