2012-05-21 95 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