2011-07-22 6 views
0

このプログラムはwhileループを2回実行することになっています。最初に実行されると、名前、職業、年齢、給与および給与(ボーナス)が表示されます。しかし、ループが2度目に実行されると、名前フィールドは空白になり、最初の従業員の給与に給与が加算されます。ありがとう。名前フィールドは、2回目のwhileループで何も返さない

import java.util.Scanner; 

public class Worker { 

    public static void main(String[] args) { 

     String name; 
     int age; 
     String occupation; 
     double salary; 
     double total = 10000; 
     int count = 0; 

     Employee employeeInfo = new Employee(); 

     Scanner keyboard = new Scanner(System.in); 

     while (count < 2) { 

      //User Name 
      System.out.println("Enter your name: "); 
      name = keyboard.nextLine(); 
      employeeInfo.setEmployeeName(name); 

      keyboard.nextLine(); 

      //User occupation 
      System.out.println("Enter your occupation: "); 
      occupation = keyboard.nextLine(); 
      employeeInfo.setOccupation(occupation); 

      //User age 
      System.out.println("Enter your age: "); 
      age = keyboard.nextInt(); 
      employeeInfo.setAge(age); 

      //User salary 
      System.out.println("Enter your salary: "); 
      salary = keyboard.nextDouble(); 
      employeeInfo.setSalary(salary); 
      total = total + salary; 

      //Output information 
      System.out.println("Name: " + name); 
      System.out.println("Age: " + age); 
      System.out.println("Occupation: " + occupation); 
      System.out.println("Original Salary: " + salary); 
      System.out.println("Salary with bonus: " + total); 

      count++; 
     } 

     // employeeInfo.greetingMessage(""); 

    } 

} 





public class Employee { 

    private String employeeName;// Employee Name 
    private int age;// Employee Age 
    private String occupation;// Employee job title 
    private double salary;// Employee salary 

    // Setters 
    public void setEmployeeName(String name) { 

     employeeName = name;// Initializes employee name 

    } 

    public void setAge(int num) { 
     age = num;// Initializes employee age 
    } 

    public void setOccupation(String occu) { 

     occupation = occu;// Initializes occupation 
    } 

    public void setSalary(double num2) { 

     salary = num2;// Initializes salary 
    } 

    // Getters 

    public String getEmployeeName() { 

     return employeeName; 
    } 

    public int getAge() { 

     return age; 
    } 

    public String getOccupation() { 

     return occupation; 
    } 

    public double getSalary() { 

     return salary; 
    } 


    public void greetingMessage(String greeting){ 
     System.out.println("Greetings " + getEmployeeName()); 
    } 
} 

答えて

1

スキャナに問題があります。 nextIntの後にnextLineと呼ぶべきではありません。それはhereです。あなたは、第2のスキャナを作り、そのような数字を、キャプチャするためにそれを使用する必要があります。

Scanner keyboard = new Scanner(System.in); 
Scanner key2 = new Scanner(System.in); 

あなたは文字列が最初のものを使用し、数が第2を使用するためにしたいときは:

name = keyboard.nextLine(); 
age = key2.nextInt(); 

もwhileループ内でEmployeeオブジェクトをインスタンス化するか、反復ごとに上書きされます。

while (count < 2) { 
    employeeInfo = new Employee(); 

最後に、あなたが名前を取得した後、第二nextLineを削除していますが占領を取得する前に。

+0

は、それが動作するようになりました、ありがとうございます。 –

+0

私は助けることができてうれしいです! – Paul

0

あなたは、キーボードから名前を読んだ後も、余分なkeyboard.nextLine();があるので、新しい従業員オブジェクトが各反復

Employee employeeInfo = new Employee(); 

で作成されたループ内でこの行を移動する必要があります。

0

あなたはwhileループの最初にあなたの従業員ojectを作成する必要があります。

while(count < 2) 
{ 
    Employee employeeInfo = new Employee(); 
    //...rest of your code... 
} 
+0

とにかく 'employeeInfo'インスタンスで何もしません。これは問題ではありません。 – Paul

関連する問題