[英]Why does my if else statement not work as desired? Problems with input
Why can my code not go on the process of if else statement?为什么我的代码不能在if else语句的过程中没有go? it's inputting 3 times and I need only 1 input.
它输入了 3 次,我只需要 1 个输入。
I can't see what's wrong with this code, please help me我看不出这段代码有什么问题,请帮助我
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Employee Full name: ");
String fn = input.nextLine();
System.out.print("Employee Number: ");
int en = input.nextInt();
System.out.print("Enter the current monthly salary: ");
int ms = input.nextInt();
System.out.print("Enter the performance rating: ");
double pr = input.nextDouble();
double raise = input.nextDouble();
double nms = input.nextDouble();
{
if(pr == 1){
System.out.println("EXCELLENT!!");
raise = ms * .06;
}
else if(pr == 2){
System.out.println("Good!");
raise = ms * .04;
}
else if(pr == 3){
System.out.println("poor.");
raise = ms * .015;
}
else{
System.out.println("ERROR!!!");
}
nms = ms + raise;
System.out.println(fn);
System.out.println(en);
System.out.println(raise);
System.out.println(nms);
}
}
}
OUTPUT: OUTPUT:
That's the output那是 output
I really don't know what is wrong我真的不知道怎么了
Scanner(System.in) uses the buffer to store the input value(s) from begin to the end marked by ENTER (\r\n). Scanner(System.in) 使用缓冲区存储输入值,从开始到由 ENTER (\r\n) 标记的结束。 As long as you use
只要你用
scaner.nextLine(); // read the whole line and remove ENTER
But if you do但如果你这样做
int ms = input.nextInt();
System.out.print("Enter the performance rating: ");
double pr = input.nextDouble();
The pr = input.nextDouble() gets the white space ENTER and that is invalid. pr = input.nextDouble() 获取空格 ENTER 并且这是无效的。 You have 2 choices: either
你有两个选择:要么
int ms = Integer.parseInt(input.nextLine());
or或者
int ms = input.nextInt();
imput.netxLine(); // jump over ENTER
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.