0

出于某种原因,我的陈述一直延续到下一行,而没有做它应该做的事情。

package Employee;
import java.util.Scanner;
import static java.lang.System.out;



public class EmployeeTest {

public static void main (String args[]) {

out.println ("Welcome to employee storer: ");

Scanner imput = new Scanner (System.in);
Employee employee1 = new Employee();// Creates an employee
Employee employee2 = new Employee();

out.println ("Please enter first name: ");
String fName = imput.nextLine(); //reads keyboard
    employee1.setfName(fName);// sets first name

out.println("please enter last name: ");
String lName = imput.nextLine();//reads keyboard
    employee1.setlName(lName);// sets second name

out.println("Please enter pay: ");
double pay = imput.nextDouble();//reads keyboard
    employee1.setPay(pay);//sets the pay

out.println ("Please enter first name: ");
String fName1 = imput.nextLine(); //reads keyboard
    employee2.setfName(fName1);// sets first name

out.println("please enter last name: ");
String lName1 = imput.nextLine();//reads keyboard
    employee2.setlName(lName1);// sets second name

out.println("Please enter pay: ");
double pay1 = imput.nextDouble();//reads keyboard
    employee2.setPay(pay1);//sets the pay

employee1.printer();// prints the info
employee2.printer();// same here

employee1.raise();
}

}

我的问题是第二个 fName 输入,它永远不会从控制台获得输入,它最终看起来像这样

"Welcome to employee storer: 
 Please enter first name: 
 Jake
 please enter last name: 
 Smith
 Please enter pay: 
 100
 Please enter first name: 
 please enter last name: 
 Jake
 Please enter pay: 
 100
 The employee Jake Smith yearly salary is: 1200.0
 The employee Jake yearly salary is: 1200.0
 Your raise is: 120.0
 Your new yearly pay is: 1200.0120.0"

我的方法类如下所示: package Employee; 导入静态 java.lang.System.out;

public class Employee {
private String fName;
private String lName;
private double pay;

public void payCheck() {
    if (pay > 0 )
        pay = 0;
}
public double yearly() {
    double overall = pay * 12; 
    return overall;
}
public void printer() {
    out.println( " The employee " + fName + " " + lName + 
            " yearly salary is: " + pay * 12 );
}
public void raise() {
    double year = pay * 12;
    double increase = .1;
    double raise = increase * year;
    out.println("Your raise is: " + raise );
    out.print("Your new yearly pay is: " + year + raise);
}


// Getters and setters 
public String getfName() {
    return fName;
}
public void setfName(String fName) {
    this.fName = fName;
}
public String getlName() {
    return lName;
}
public void setlName(String lName) {
    this.lName = lName;
}
public double getPay() {
    return pay;
}
public void setPay(double pay) {
    this.pay = pay;
}

}

最后的年薪我也得到了一个奇怪的输出。不太确定为什么,任何帮助将不胜感激。

4

5 回答 5

5

nextDouble()将仅返回该行上的值,将行 return 留在后面,由下一个语句Double拾取。nextLine()

于 2013-04-13T20:12:43.173 回答
2

尝试这个

imput.nextDouble();

只需在下面执行nextDouble();

imput.nextLine();

每当您想通过.next()呼叫扫描号码时,请确保在其.nextLine()旁边添加一个空白,以便Scanner移动到正确的位置

于 2013-04-13T20:13:13.103 回答
2

nextDouble() 只是从输入中读取双精度,而不是整行的双精度。在 double 之后输入的回车保留在输入中,之后的 nextLine() 语句会立即检索,而不是暂停。

使用 nextLine() 检索它,然后将结果转换为双精度(显然带有错误检查)。

于 2013-04-13T20:13:16.637 回答
2

问题是你在加倍之后按回车键;System.in 得到一个双精度,后跟一个换行符,它被读取为双精度(换行符停止扫描),然后尝试读取一个字符串直到新行(这导致一个空字符串)

于 2013-04-13T20:14:05.287 回答
1

还有关于你问题的最后一部分

out.print("Your new yearly pay is: " + year + raise);

这是做简单的字符串连接,所以你得到“奇怪”的结果。相反,尝试一对额外的括号进行适当的求和,然后连接

out.print("Your new yearly pay is: " + (year + raise));
于 2013-04-13T20:21:57.660 回答