声明落到下一行

时间:2013-04-13 20:05:14

标签: java

出于某种原因,我的陈述不断进入下一行而没有按照预期进行。

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"

我的方法类如下所示:     包员工;     import static 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;
}

}

我也得到了最终年薪的奇数输出。不确定为什么,任何帮助都会受到赞赏。

5 个答案:

答案 0 :(得分:5)

nextDouble()将仅返回该行的Double值,将该行返回后面,这将由下一个nextLine()语句获取。

答案 1 :(得分:2)

试试这个

imput.nextDouble();

nextDouble();

之后执行以下行
imput.nextLine();

如果您想通过.next()来电扫描号码,请务必在其旁边添加空.nextLine(),以便Scanner移动到正确的位置

答案 2 :(得分:2)

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

使用nextLine()检索它和所有其他的一样,然后将结果转换为double(显然有错误检查)。

答案 3 :(得分:2)

问题是你在双重后按下输入; System.in获取一个double后跟一个换行符,换行符被读取为double(换行符停止扫描)然后尝试读取一个字符串直到新行(这导致一个空字符串)

答案 4 :(得分:1)

还有关于问题的最后部分

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

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

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