My problem is that when I run this program I have to press 2 twice, or 3 three times to get the else if statements to run. I have tried switching the input to string and it has the same issues. (you can ignore all the code except the (sc.nextInt()==1)/(sc.nextInt()==2)/(sc.nextInt()==3))
Those are where I'm having problems. Thanks
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);
答案 0 :(得分:1)
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);
because each time u write nextInt(), it will wait for input from User. So, just try to catch 1 input from User, then just check whats the input is.
答案 1 :(得分:1)
You are reading a 2nd time from sc.nextInt() in your 'else'. Read once before the 'if', then test the value.
答案 2 :(得分:1)
Whenever you call nextInt()
, the process waits and reads your input once.
So, the first if(sc.nextInt()==1)
reads your input once, and you need to type the second time to reach else if (sc.nextInt()==2)
.
To solve this, try following structure:
int input = sc.nextInt();
if (input == 1) {
} else if (input == 2) {
} else if (input == 3) {
}