当前位置: 首页 > 知识库问答 >
问题:

扫描器请求输入两次,只将第二次输入存储到阵列中

谭越
2023-03-14

我用java编写了一个带条件的switch case语句。如果一个人在2到12个月的时间内将一个项目添加到系统中,它将被添加到一个数组中。如果项目持续时间小于2或大于12,系统应要求他们重新输入有效数字。出于某种原因,我的系统会提示他们输入两次数字,并且只存储数组中的第二个条目:终端提示两次的图像

我在想这是因为我有“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();}

共有1个答案

危飞文
2023-03-14
    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
}
 类似资料:
  • 编写一个程序,向用户询问一顿饭的费用,计算这顿饭的小费(18%),计算这顿饭的税收(8.25%),然后显示这顿饭的费用、这顿饭的小费金额、这顿饭的税收这顿饭,以及这顿饭的总数,这是成本、小费和税额的总和 这是我的密码: Python Shell上的输出: 输入餐费:19.95 成本:19.95 输入餐费:19.95 提示:3.59 输入餐费:19.95 税费:1.65 输入餐费:19.95 这可能

  • 我有以下课程 我不能输入歌曲名nr 1,因为它总是同时显示前两首歌。 如果我输入3,就像这样:

  • 这是菜单类 import java.util.Scanner; 公共类菜单{private String[]Menu_options; } 这是我使用的菜单驱动程序 公共类 Menutester { } 我第一次输入一些东西时,它根本不会重新生成,当我尝试在eclipse中调试它时,它会给我错误FileNotFoundException(Throwable)。(字符串)行:第一次输入195。 这就

  • 我有一个扫描仪,它读入字符串作为坐标输入。我必须将这些字符串转换成整数,并将它们存储在坐标的Arraylist中,但我输入的最后一个值没有存储在Arraylist中。 我尝试在for-loop之外使用,但仍然没有变化。在存储和解析字符串时,我也尝试使用while循环而不是for循环,但得到了相同的结果。 预期结果: 点:[1,2,3,4] 实际结果 点:[1,2]

  • Python提供一个标准的模块,称为pickle。使用它你可以在一个文件中储存任何Python对象,之后你又可以把它完整无缺地取出来。这被称为 持久地 储存对象。 还有另一个模块称为cPickle,它的功能和pickle模块完全相同,只不过它是用C语言编写的,因此要快得多(比pickle快1000倍)。你可以使用它们中的任一个,而我们在这里将使用cPickle模块。记住,我们把这两个模块都简称为p