有阵列的麻烦

时间:2014-04-26 11:59:52

标签: java arrays

我目前正在尝试编写基于数组的程序。我们必须创建一个员工类,然后是一个测试人员主类,它包含五个用户输入员工姓名,工资和绩效评级的数组。性能等级用于确定提供的提升量。我基本上已经完成了,但是当我运行程序时,即使java虚拟机正在运行也没有任何反应。我一直在寻找错误,任何人都可以指出我做错了什么?

员工类

public class Employee
{
  private String employeeName;
  private int salary;
  private int performanceRating;

  public Employee()
  {
    employeeName = "";
    salary = 0;
    performanceRating = 0;
  }   
  public String getEmployeeName()
  {
      return employeeName;
  }
  public int getSalary()
  {
      return salary;
  }
  public int getPerformanceRating()
  {
      return performanceRating;
  }
}

这是测试者主类,其中错误出现在某处

import java.util.Scanner;
public class Tester
{
   public static void main(String[] args)
   {
       Employee[] work = new Employee[5];
       Scanner scanString = new Scanner(System.in);
       Scanner scanInt = new Scanner(System.in);


       String employeeName = "";
       double salary = 0;
       double performanceRating = 0;

       String choice = scanString.nextLine();
       while(choice.equals("yes"))
       {
            for(int i = 0; i < 5 ;i++)
            {
                System.out.println("What is the employee's name?");
                employeeName = scanString.nextLine();

                System.out.println("Enter Employee's salary");
                salary = scanInt.nextInt();

                System.out.println("Performance? 1 = excellent, 2 = good, 3 = poor");
                performanceRating = scanInt.nextInt();

                work[i] = new Employee();       


            }
            for(int j = 0; j < 5;  j ++)
                if(work[j].getPerformanceRating() == 1)
                {
                    salary = salary * 0.06;
                    System.out.println("Employee " + employeeName + " salary raised to   " + salary);
            }
            else if(performanceRating == 2)
            {
                salary = salary * 0.04;
                System.out.println("Employee " + employeeName + " salary raised to " + salary);
                }
                else if(performanceRating == 3)
                {
                    salary = salary * 0.015;
                    System.out.println("Employee " + employeeName + " salary raised to " + salary);
                }
                else if(performanceRating < 5)
                {
                    salary = salary;
                    System.out.println("Rating is off scale. No raise for emplyee " + employeeName);
                }
                System.out.println("Enter more employees? type 'yes' if you want to go again");
                choice = scanString.nextLine();
            }
        System.out.println("Done");
    }

   }

2 个答案:

答案 0 :(得分:0)

该程序从System.in读取。如果你不输入任何东西,什么都不会发生。

不确定为什么你有2个扫描仪而不只是一个。

答案 1 :(得分:0)

你有while(choice.equals("yes")),除非你从不提示用户做出选择。该程序确实做了一些事情(其中一些可能都不能正常工作),但你必须提供程序输入。您可以做的是在行String choice = scanString.nextLine();之前询问用户一个问题。

作为旁注,您可以使用switch代替ifelse if,并且可能更容易阅读和理解。