java - For 循环错误 -> if 语句不产生输出

标签 java loops if-statement

这是一些我更愿意自己弄清楚的工作,所以如果可能的话,请你不要直接给出答案,但也许会指出正确的方向,说明为什么我从循环输出中收到错误以及为什么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;
    }

} shop unit 打印出来没问题,但是输出后就报错,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)

知道是什么原因造成的吗?

最佳答案

以下几行给你一个错误:

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

您可以将它们包装在:

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

关于java - For 循环错误 -> if 语句不产生输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20223811/

相关文章:

java - 用于 SaaS 预订应用程序的 Drupal 或框架

java - 设计一个 Spring 批处理应用程序从不同的资源(平面文件)读取数据

javascript - 在循环中使用 native 浏览器模式对话框是否会导致潜在的无限执行?

java - 在java中访问while循环内的值?

java - 如何通过循环小时来返回时间对象列表

java - Java中多个线程访问同一个对象

java - 在 VS Code 中运行的 Spring Boot 应用程序中接收空对象

javascript - 选项标签内的重定向到另一个页面按钮始终重定向到同一页面(JavaScript)

c# - ? : Operator Vs. If语句性能

jQuery 如果点击