我试图为我的一个类(class)编写这个程序,但我的输出遇到了问题。它应该读取我为 Scanner
输入输入的任何内容。但是,输出跳过了第一个单词,我不太确定为什么。您可以忽略 main 方法中的大部分变量声明。这些仅对程序的其余部分有用。
public static void main(String[] args) {
String fullName;
int anniversaryM;
int anniversaryY;
int periodHours;
String jobTitle;
double payRate;
int monthsWorked;
double vacationHours;
double grossPay;
double retirement;
double taxWithholding;
double netPay;
Scanner in = new Scanner(System.in);
fullName = inputLine(in, "Enter your full name:");
System.out.print(fullName);
}
public static double inputNumber(Scanner input, String prompt) {
Scanner in = new Scanner(System.in);
in.nextDouble();
return in.nextDouble();
}
public static String inputLine(Scanner input, String prompt) {
Scanner in = new Scanner(System.in);
System.out.println(prompt);
in.next();
return in.next();
}
public static double calcPercentage(double grossPay, double retirement) {
Scanner in = new Scanner(System.in);
in.nextDouble();
return in.nextDouble();
}
输出:
Enter your full name:
John Doe
Doe
最佳答案
您对 in.next()
进行了两次调用。只需将其删除即可。此外,请注意,您将 Scanner
传递给该方法,因此您不应在该方法内创建新的:
public static String inputLine(Scanner input, String prompt) {
System.out.println(prompt);
return input.nextLine();
}
关于java - System.out.print() 读取 Scanner 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52471856/