2012-05-21 92 views
1

我有一個關於我的實驗室分配了一些麻煩:麻煩與陣列輸出,並提示用戶輸入輸出

當我的程序試圖提示用戶輸入,程序輸出在同一條線上兩個問題,只需要第二個問題的輸入。

我的程序的輸出:

請輸入第二個僱員的姓名:請輸入第二個僱員的數量:1

(他們出現在同一行,而不是單獨的行)

而且輸出用於陣列輸出這樣的:

0.00.00.00.00.00.00.00.00.00.0 

,而不是像這樣:

0, 0, 0, 0, 0, 0, 0, 0, 0, 0 

我不太清楚如何解決這兩個任何幫助,將不勝感激!

這裏是我的代碼:

Employee.java

//import java.util.*; 

public class Employee 
{ 
    private String empName; 
    private int empNumber; 
    private String empAddress; 
    private double empSalary; 

private double[] empBonus=new double[10]; 

public Employee(){} 

public Employee(String empName_, int empNumber_, String empAddress_, double empSalary_, double[] empBonus_) 
{ 
    this.empName=empName_; 
    this.empNumber=empNumber_; 
    this.empAddress=empAddress_; 
    this.empSalary=empSalary_; 

    this.empBonus=empBonus_; 
} 

public String getName() 
{ 
    return this.empName; 
} 

public int getEmployeeNumber() 
{ 
    return this.empNumber; 
} 

public String getAddress() 
{ 
    return this.empAddress; 
} 

public double getSalary() 
{ 
    return this.empSalary; 
} 

public String changeAddress(String chAddress) 
{ 
    return empAddress=chAddress; 
} 

public double changeSalary(double chSalary) 
{ 
    return empSalary=chSalary; 
} 

public String addBonus(double[] empBonus) 
{ 
    String arrayBonus=new String(""); 

    for(int i=0; i<empBonus.length;i++) 
    { 
     arrayBonus+=empBonus[i]; 
    } 

    return arrayBonus; 
} 

public String toString() 
{ 
    return ("\nEmployee's name: "+empName+"\nEmployee's Number: "+empNumber+"\nEmployee's address: "+empAddress+ 
      "\nEmployee's original salary: "+empSalary+ "\nEmployee's bonuses: "+addBonus(empBonus)+"\n"); 
} 
} 

EmployeeTester.java

import java.util.*; 

public class EmployeeTester 
{ 
public static void main(String[] args) 
{ 
    Scanner in1=new Scanner(System.in); 
    Scanner in2=new Scanner(System.in); 
    Scanner in3=new Scanner(System.in); 

    Employee emp1; 
    Employee emp2; 

    emp1=read_input("first", in1, in2, in3); 
    emp2=read_input("second", in1, in2, in3); 

    System.out.println(emp1.toString()); 
    System.out.println(emp2.toString()); 

} 

public static Employee read_input(String msg, Scanner scan1, Scanner scan2, Scanner scan3) 
{ 
    String name, address; 
    int num; 
    double salary; 
    double[] bonus=new double[10]; 

    System.out.print("\nPlease enter the name of the "+msg+" employee:"); 
    name=scan1.nextLine(); 

    System.out.print("Please enter the number of the "+msg+" employee:"); 
    num=scan2.nextInt(); 

    System.out.print("Please enter the address of the "+msg+" employee:"); 
    address=scan1.nextLine(); 

    System.out.print("Please enter the salary of the "+msg+" employee:"); 
    salary=scan3.nextDouble(); 

    System.out.print("Please add a bonus for the "+msg+" employee:"); 
    bonus[0]=scan3.nextDouble(); 

    System.out.print("Add more bonuses to the "+msg+"employee? (y/n) \nNote: Enter 0.0 if you would like to terminate adding more bonuses: "); 

    if(scan1.next().startsWith("y")) 
    { 
     for(int i=1; i<bonus.length;i++) 
     { 
      System.out.print("Continue entering a bonus to "+msg+" employee:"); 
      bonus[i]=scan3.nextDouble(); 

      if(bonus[i]==0.0 || i==bonus.length) 
      { 
       break; 
      } 
     } 
    } 

    return new Employee(name, num, address, salary, bonus); 
} 
} 

回答

2

對於第一個問題,只需將Scanners轉換爲read_input方法,以便每次都能重新開始。

public static void main(String[] args) { 
    Employee emp1; 
    Employee emp2; 

    emp1 = read_input("first"); 

    emp2 = read_input("second"); 

    System.out.println(emp1.toString()); 
    System.out.println(emp2.toString()); 

} 

public static Employee read_input(String msg) { 
    Scanner scan1 = new Scanner(System.in); 
    Scanner scan2 = new Scanner(System.in); 
    Scanner scan3 = new Scanner(System.in); 
    ... 

關於第二個問題,在您的addBonus,你建立你不加入任何空格或逗號輸出字符串的方法。如果對這種循環串聯使用StringBuilder而不是重複創建新的字符串對象,效率會更高。

public String addBonus(double[] empBonus) 
{ 
    StringBuilder arrayBonus = new StringBuilder(); 

    for(int i=0; i<empBonus.length;i++) 
    { 
     arrayBonus.append(empBonus[i] + ", "); 
    } 

    return arrayBonus.toString(); 
} 
+0

由於一噸這樣解決了我的問題的第二部分:!d – xGiraffe

+0

QUQ非常感謝爲你的幫助!我真的很感激它! – xGiraffe

0

你並不需要3臺不同的掃描儀,人會做。

對於打印部分,您需要println()或使用'\n'換行。

例如爲:

System.out.println("Please enter the name of the "+msg+" employee:"); 
System.out.print("Please enter the name of the "+msg+" employee:\n"); 

在第一種情況下,你調用一個函數(的println()代替print()),其自動附加一個新行到outputes文本。在第二種情況下,您正在使字符串的新行部分(您已在第一行的開頭已經使用過)

數組輸出使用0.0,因爲這些值是浮點值(double) ),默認打印小數部分。要僅打印整數部分,您需要將該值轉換爲整數或對double使用格式。

鑄造:

double a = 0.0; 
System.out.print("'a' as integer: " + ((int)a)); 

格式:

System.out.format("Here is a number: %.0f", a); 

.f之間的數字指定多少個小數位輸出。

+0

我之前試過一個掃描儀,但它給了我兩行輸出,而不是單獨輸出。我搜索了谷歌,它告訴我做一個新的掃描儀,因爲nextInt()有一些事情要做。它只是暫時固定程序,然後再次發生相同的輸出後,我增加了更多的編程東西> _ < 我也認爲這是println或\ n的問題,我嘗試了兩個,但它仍然輸出相同的東西!:( (抱歉,如果我沒有太大的意義> _ <) – xGiraffe

+0

謝謝您的幫助^ __^ – xGiraffe