2013-03-27 197 views
0

继承人我的代码评论总结了程序的设想。什么是导致此错误消息?

// This program will read in from a file a number that tells how many 
// wages and hours worked the file contain. And then calculates the 
// salary 
import java.util.*; 
import java.text.*; 

public class Lab10 
{ 
     public static void main(String[] args) 
     { 
      // Declare variables 
      Scanner scan = new Scanner(System.in); 
      DecimalFormat fmt = new DecimalFormat("0.00"); 
      String name; 
      double wages; 
      double hoursWorked; 
      double salary; 
      int numCalculate; 
      int EmployeeNumber ; 
      int i = 0 ; 

      // Read in how many employees the file contains 
      EmployeeNumber =scan.nextInt() ; 

      // for loop that reads and records the name hoursworked and wages , and prints out the information 
      for (; i < EmployeeNumber +1; i ++) 
      { 
      name = scan.next() ; 
      wages = scan.nextDouble() ; 
      hoursWorked = scan.nextDouble() ; 

      salary = calculateSalary(wages,hoursWorked) ; 
      System.out.println(name +" worked " +fmt.format(hoursWorked) + " with a wage of " + "$" +fmt.format(wages) + " and got paid $" +fmt.format(salary)); 
      } 
      // End of control loop 

     } // End of main method 

     public static double calculateSalary(double wages, double hoursWorked) 
     { 
//Declare constants 
      final int OVERTIME_BREAK = 40; 
      final double OVERTIME_MULTIPLE = 1.5; 
      double salary = 0 ; 

      //Calculate salary 

      if(hoursWorked > OVERTIME_BREAK) 
      { 
      salary = (hoursWorked - OVERTIME_BREAK) * wages * OVERTIME_MULTIPLE + hoursWorked * wages; 
      } 
      else 
      { 
      salary = hoursWorked * wages; 
      } 
      // Return the salary 
      return salary ; 

     } // end of calculateSalary method 
} // End of Lab10 class 

当使用该输入文件:

3 
Smith 12.50 25 
Jones 25.89 60 
Brown 7.86 19.89 

我得到我想要的输出,但该消息上涨了底:

Exception in thread "main" java.util.NoSuchElementException 
    at java.util.Scanner.throwFor(Scanner.java:855) 
    at java.util.Scanner.next(Scanner.java:1364) 
    at Lab10.main(Lab10.java:32) 

可能有人请告诉我这是什么意思,以及如何要解决这个问题?

回答

2

您正循环太多次。

你有3名员工,并应为此像这只是循环:

for (; i < EmployeeNumber; i ++) 
{ 
    name = scan.next() ; 
    ... 
} 
+0

这就是它,谢谢。它现在有效 – 2013-03-27 15:13:11

0

Java的数组索引从0开始,所以,如果你有3个要素那些被保存在0,1,和2,说上面在你的for循环,如果你只是把我< EmplyeeNumber是足够的,不EmployeeNumber + 1.

相关问题