Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法让do while循环正常工作_Java_Loops_Do While - Fatal编程技术网

Java 无法让do while循环正常工作

Java 无法让do while循环正常工作,java,loops,do-while,Java,Loops,Do While,我的问题是,当我运行这个程序时,我必须按2两次或3次才能运行else if语句。我曾尝试将输入切换为字符串,但它也有同样的问题(您可以忽略除(sc.nextInt()==1)/(sc.nextInt()==2)/(sc.nextInt()==3)之外的所有代码)这些都是我遇到的问题。谢谢 Scanner sc = new Scanner(System.in); int running = 0; do {

我的问题是,当我运行这个程序时,我必须按2两次或3次才能运行else if语句。我曾尝试将输入切换为字符串,但它也有同样的问题<代码>(您可以忽略除(sc.nextInt()==1)/(sc.nextInt()==2)/(sc.nextInt()==3)之外的所有代码)这些都是我遇到的问题。谢谢

 Scanner sc = new Scanner(System.in);
            int running = 0;
            do
            {
                System.out.println("What would you like to do?");
                System.out.println("(1)Enter hourly employee");
                System.out.println("(2)Enter commissionary employee");
                System.out.println("(3)Terminate program");
                System.out.println();
                if (sc.nextInt()==1){
                    System.out.println("Enter their first name. \n");
                    hour.firstName= sc.next();
                    System.out.println("Enter their last name. \n");
                    hour.lastName= sc.next();
                    System.out.println("Enter their Id #. \n");
                    hour.id= sc.nextInt();
                    System.out.println("Enter their wage amount. \n");
                    hour.wage= sc.nextInt();
                    System.out.println("Enter how many hours they will be working. \n");
                    hour.hrs= sc.nextInt();
                    System.out.println("employee "+ hour.id + "'s name is " + hour.firstName + " "+ hour.lastName + " their id is "+ hour.id);
                    System.out.println("employee "+ hour.id + "'s wage is " + hour.wage + "$ and will be working "+ hour.hrs +" hours per week.");
                    running++;

                }

                //have to type 2 twice here
                 else if (sc.nextInt()== 2){
                    System.out.println("Enter their first name. \n");
                    com.firstName= sc.next();
                    System.out.println("Enter their last name. \n");
                    com.lastName= sc.next();
                    System.out.println("Enter their Id #. \n");
                    com.id= sc.nextInt();
                    System.out.println("Enter their base salary. \n");
                    bcom.baseSalary= sc.nextInt();
                    System.out.println("Enter their wage commission rate. \n");
                    com.cRate= sc.nextInt();
                    System.out.println("Enter their expected gross sales goal. \n");
                    com.gSales= sc.nextInt();
                    System.out.println("employee "+ com.id + "'s name is " + com.firstName + " "+ com.lastName + " their id is "+ com.id);
                    System.out.println("employee "+ com.id + "'s base salary is "+ bcom.baseSalary + "$ their commission rate is \n" + com.cRate + "% and is estimated to make "+ com.gSales +"$ in gross sales.");
                    running++;

                }
                //have to type 3 three times here
                else if (sc.nextInt()== 3)
                {
                    running++;
                }


            }while(running < 1);
Scanner sc=新扫描仪(System.in);
int运行=0;
做
{
System.out.println(“您想做什么?”);
System.out.println(“(1)输入小时员工”);
System.out.println(“(2)输入佣金员工”);
System.out.println(“(3)终止程序”);
System.out.println();
如果(sc.nextInt()==1){
System.out.println(“输入他们的名字。\n”);
hour.firstName=sc.next();
System.out.println(“输入他们的姓氏。\n”);
hour.lastName=sc.next();
System.out.println(“输入他们的Id.\n”);
hour.id=sc.nextInt();
System.out.println(“输入他们的工资金额。\n”);
小时工资=sc.nextInt();
System.out.println(“输入他们的工作时间。\n”);
小时=sc.nextInt();
System.out.println(“员工”+hour.id+”的名字是“+hour.firstName+”+hour.lastName+”,他们的id是“+hour.id”);
System.out.println(“雇员”+hour.id+“”的工资为“+hour.wage+”$,将工作“+hour.hrs+”每周小时数”);
运行++;
}
//必须在这里输入2次
else if(sc.nextInt()==2){
System.out.println(“输入他们的名字。\n”);
com.firstName=sc.next();
System.out.println(“输入他们的姓氏。\n”);
com.lastName=sc.next();
System.out.println(“输入他们的Id.\n”);
com.id=sc.nextInt();
System.out.println(“输入他们的基本工资。\n”);
bcom.baseSalary=sc.nextInt();
System.out.println(“输入他们的工资佣金率。\n”);
com.claiter=sc.nextInt();
System.out.println(“输入他们的预期总销售目标。\n”);
com.gSales=sc.nextInt();
System.out.println(“雇员”+com.id+”的名字是“+com.firstName+”+com.lastName+”,他们的id是“+com.id”);
System.out.println(“员工”+com.id+“”的基本工资是“+bcom.baseSalary+”$,他们的佣金率是\n“+com.claiter+”,估计在总销售额中为“+com.gSales+”$”);
运行++;
}
//必须在这里输入3次
else if(sc.nextInt()==3)
{
运行++;
}
}同时(运行<1);
Scanner sc=新扫描仪(System.in);
int运行=0;
做
{
System.out.println(“您想做什么?”);
System.out.println(“(1)输入小时员工”);
System.out.println(“(2)输入佣金员工”);
System.out.println(“(3)终止程序”);
System.out.println();
int i=sc.nextInt();
如果(i==1){
System.out.println(“输入他们的名字。\n”);
hour.firstName=sc.next();
System.out.println(“输入他们的姓氏。\n”);
hour.lastName=sc.next();
System.out.println(“输入他们的Id.\n”);
hour.id=sc.nextInt();
System.out.println(“输入他们的工资金额。\n”);
小时工资=sc.nextInt();
System.out.println(“输入他们的工作时间。\n”);
小时=sc.nextInt();
System.out.println(“员工”+hour.id+”的名字是“+hour.firstName+”+hour.lastName+”,他们的id是“+hour.id”);
System.out.println(“雇员”+hour.id+“”的工资为“+hour.wage+”$,将工作“+hour.hrs+”每周小时数”);
运行++;
}
//必须在这里输入2次
else如果(i==2){
System.out.println(“输入他们的名字。\n”);
com.firstName=sc.next();
System.out.println(“输入他们的姓氏。\n”);
com.lastName=sc.next();
System.out.println(“输入他们的Id.\n”);
com.id=sc.nextInt();
System.out.println(“输入他们的基本工资。\n”);
bcom.baseSalary=sc.nextInt();
System.out.println(“输入他们的工资佣金率。\n”);
com.claiter=sc.nextInt();
System.out.println(“输入他们的预期总销售目标。\n”);
com.gSales=sc.nextInt();
System.out.println(“雇员”+com.id+”的名字是“+com.firstName+”+com.lastName+”,他们的id是“+com.id”);
System.out.println(“员工”+com.id+“”的基本工资是“+bcom.baseSalary+”$,他们的佣金率是\n“+com.claiter+”,估计在总销售额中为“+com.gSales+”$”);
运行++;
}
//必须在这里输入3次
else如果(i==3)
{
运行++;
}
}同时(运行<1);
因为每次写入nextInt(),它都会等待inpu
 Scanner sc = new Scanner(System.in);
            int running = 0;
            do
            {
                System.out.println("What would you like to do?");
                System.out.println("(1)Enter hourly employee");
                System.out.println("(2)Enter commissionary employee");
                System.out.println("(3)Terminate program");
                System.out.println();
                int i = sc.nextInt();
                if (i==1){
                    System.out.println("Enter their first name. \n");
                    hour.firstName= sc.next();
                    System.out.println("Enter their last name. \n");
                    hour.lastName= sc.next();
                    System.out.println("Enter their Id #. \n");
                    hour.id= sc.nextInt();
                    System.out.println("Enter their wage amount. \n");
                    hour.wage= sc.nextInt();
                    System.out.println("Enter how many hours they will be working. \n");
                    hour.hrs= sc.nextInt();
                    System.out.println("employee "+ hour.id + "'s name is " + hour.firstName + " "+ hour.lastName + " their id is "+ hour.id);
                    System.out.println("employee "+ hour.id + "'s wage is " + hour.wage + "$ and will be working "+ hour.hrs +" hours per week.");
                    running++;

                }

                //have to type 2 twice here
                 else if (i== 2){
                    System.out.println("Enter their first name. \n");
                    com.firstName= sc.next();
                    System.out.println("Enter their last name. \n");
                    com.lastName= sc.next();
                    System.out.println("Enter their Id #. \n");
                    com.id= sc.nextInt();
                    System.out.println("Enter their base salary. \n");
                    bcom.baseSalary= sc.nextInt();
                    System.out.println("Enter their wage commission rate. \n");
                    com.cRate= sc.nextInt();
                    System.out.println("Enter their expected gross sales goal. \n");
                    com.gSales= sc.nextInt();
                    System.out.println("employee "+ com.id + "'s name is " + com.firstName + " "+ com.lastName + " their id is "+ com.id);
                    System.out.println("employee "+ com.id + "'s base salary is "+ bcom.baseSalary + "$ their commission rate is \n" + com.cRate + "% and is estimated to make "+ com.gSales +"$ in gross sales.");
                    running++;

                }
                //have to type 3 three times here
                else if (i== 3)
                {
                    running++;
                }


            }while(running < 1);
int input = sc.nextInt();
if (input == 1) {
} else if (input == 2) {
} else if (input == 3) {
}