2013-11-26 134 views
1

这是一些我宁愿弄清楚自己的工作,所以你可以请不要直接给出答案,但也许可以指出我正确的方向为什么我我正在从循环输出接收错误,以及为什么IF语句没有生成输出。对于循环错误 - >如果语句不产生输出

我必须允许用户输入'推荐的最高员工工资',然后从不同商店单元的文件中读取文本以计算每个商店单位的员工工资和总员工成本。

这里是我要读

Unit One 
4 
32 8 
38 6 
38 6 
16 7 

Unit Two 
0 

Unit Three 
2 
36 7 
36 7 

Unit Four 
6 
32 6.5 
32 6.5 
36 6.5 
36 6.5 
38 6.5 
38 6.5 

...continued up to 9 shop units. 

文本这是我的代码:

public class Recursion { 

    public static void main(String[] args) { 
      // TODO Auto-generated method stub 
      int count =0; 
      int n = 1; 
      int t=0; 
      int triangularNumber =0; 
      while (n<Integer.MAX_VALUE) 
      { 
        t = isTriangularNumber(n,count,triangularNumber); 
        triangularNumber=0; 
        int starNumber= ((6*n)*(n-1)) + 1; 
        if (starNumber ==t) 
        { 
          System.out.println(t); 
        } 
        n++; 
      }  
      if (n==Integer.MAX_VALUE) 
      { 
       System.exit(0); 
      } 
    } 


    public static int isTriangularNumber(int n, int count, int triangularNumber) 
    { 
      triangularNumber =triangularNumber + (n-(n-count)); 
      if (count<=n) 
      {  
        return isTriangularNumber(n,(count++), triangularNumber); 
      }  
      else return triangularNumber; 
    } 

} 为铺台的打印输出都很好,但它的输出后产生一个错误,并且if语句的输出不出来,如下所示:

Please enter the recommended maximum staff cost: 
900 
You entered: 900.0 
256.0 
484.0 
712.0 
824.0 
The total staff cost of Unit One is £824.0 
The total staff cost of Unit Two is £0.0 
252.0 
504.0 
The total staff cost of Unit Three is £504.0 
208.0 
416.0 
650.0 
884.0 
1131.0 
1378.0 
The total staff cost of Unit Four is £1378.0 
208.0 
464.0 
688.0 
944.0 
The total staff cost of Unit Five is £944.0 
266.0 
461.0 
653.0 
845.0 
1037.0 
The total staff cost of Unit Six is £1037.0 
The total staff cost of Unit Seven is £0.0 
480.0 
The total staff cost of Unit Eight is £480.0 
192.0 
348.0 
543.0 
711.0 
935.0 
The total staff cost of Unit Nine is £935.0 
Exception in thread "main" java.util.NoSuchElementException: No line found 
at java.util.Scanner.nextLine(Unknown Source) 
at TEST.main(TEST.java:48) 

任何想法可能会造成这种情况?

+0

粗略猜测:你的两个'infile.nextLine()'调用试图超过输入文件的末尾? –

回答

0

if语句的输出未到达的原因是该语句不在循环中。正确格式化您的代码应该可以帮助您更早地识别此类错误。

的花括号结束for (int b = 0; b <shopunits; b++)循环的if语句之前位于同一符合total = 0;。这就是在循环期间没有达到条件的原因。

另一个问题是,您的代码在处理当前商店后消耗商店之间的新线。只要有下一家商店,这就行得通;然而,只要你到达最后一家商店,就会发生这种情况。

要解决这个问题,之前消费新行添加的检查,像这样:

if (!inFile.hasNextLine()) break; 
Unitnum = inFile.nextLine(); 
if (!inFile.hasNextLine()) break; 
Unitnum = inFile.nextLine(); 

另外,还可以保护nextLine()当前块读取与单一if,像这样:

if (b != shopunits-1) { 
    // Skip lines only if we haven't reached the last block 
    Unitnum = inFile.nextLine(); 
    Unitnum = inFile.nextLine(); 
} 

在循环内移动if语句并修复此问题后,程序应正常运行。

+0

好的谢谢,只是想知道我会在哪里添加他们的代码行?我得到的if语句现在工作正常,只是循环的问题仍然 – user2863681

+0

@ user2863681你有两个连续的行'Unitnum = inFile.nextLine();'那里。目前,它们一直在执行。你可以在它们周围添加一个'if(b!= shopunits-1)',或者在这些行的前面添加'if(!inFile.hasNextLine())break;'。两种方式都应该正常工作。 – dasblinkenlight

1

以下行给你一个错误:

Unitnum = inFile.nextLine(); 
Unitnum = inFile.nextLine(); 

你可以在他们包裹:

if (b < shopunits - 1) { 
    ... 
} 
1

您已经达到了文件的末尾,扫描仪尝试读取nextLine时在导致此问题的文件末尾。

1

Adarsh说的很可能是正确的,你应该做的是把它包装在一个while循环中。

while(infile.hasNextLine()){ 
    ... 
} 

这种方式,当它到达最后它不会失败。 我经历并编辑了格式化代码,起初很难遵循。我认为你的if block的问题在那里没有括号。

试试这个:

if(total > recommended_max){ 
    System.out.println("Higher"); 
}else{ 
    System.out.println("Lower"); 
} 

我知道它有时无,但有时括号取决于如何或在哪里它被写入它可能不是。所以它总是很好的把它们放在上面。

0
  1. 对于在端部的误差,似乎在下面的for循环尝试之后读取线每次迭代结束:

    Unitnum = inFile.nextLine();
    Unitnum = inFile.nextLine();

因此,无论你应该有2个空行的文件的末尾,还是应该在循环的开始读取行:约

while(inFile.hasNextLine()) { 
    Unitnum = inFile.nextLine(); 
    saleassist = inFile.nextInt(); 
    ....... 
} 
  1. 二问题“如果“块在每次迭代结束时与总值设置为0有关:

    小时= 0; rate = 0; total = 0;

您需要在外部循环之外声明的额外变量来保存循环中总计的总和。

double sumOfTotal=0; 
...... 
System.out.println("The total staff cost of " + Unitnum +" is £"+ total); 
//this holds the sum of totals across loops 
sumOfTotal += total; 
...... 
total=0; 
} 
if (sumOfTotal > reccomended_max) 
    System.out.println("Higher"); 
else 
    System.out.println("Lower");