当前位置: 首页 > 知识库问答 >
问题:

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

费辰阳
2023-03-14

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

你知道是什么导致了这一切吗?

共有1个答案

饶铭
2023-03-14

以下行给出了一个错误:

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

你可以把它们包起来:

if (b < shopunits - 1) {
    ...
}
 类似资料:
  • 有人能帮我找出为什么Instachat:Stick\u out\u舌头\u winking\u eye:'和'Docs To Go'的代码没有返回False吗™ 免费办公套房'?它们包含Unicode大于127的字符(分别为emoji和TM),因此从技术上讲,这两个字符都应该返回False。 我不明白为什么else条款在这里不起作用。 下面应该是我的代码的预期输出:True False 然而,实际

  • 问题内容: 目前无法打破此循环。如果要在此列表中找不到变量,我想破坏它,以便它可以移动另外两个循环。它期望在循环的顶部有一个缩进的块,但是如果更改循环的开头或开头的位置,它将无法正常工作。救命! 问题答案: 正如人们在对您的问题的评论中提到的那样,您需要分别打破每个循环,仅停止其所在的循环 两个 循环都需要这样做 如果您也想打破while循环,那么您也可以添加该循环。

  • 我有一个380行20列的表格。我想从该表中删除符合特定条件的行。 为了澄清问题,假设我有以下列表: 我想删除在列表中找到姓名的所有人员的数据。 例如,假设我的数据如下所示: 我想删除、和的数据。所以输出应该是: 我的代码正在运行,但数据没有从我的原始数据中删除。当我打开新的测试.txt文件时,我可以看到数据没有被删除。 我可以肯定错误在< code > if data[row][0]= =(I f

  • 问题内容: 现在,父级for循环()在第一个findOne触发之前完成,因此,这仅循环通过repliesIDsArray..asynchronous。的最后一个元素。 此代码集的承诺版本的正确语法是什么?Promisification的新手,想知道如何开始Promisify +遍历数组+解释if语句。 蓝鸟是必需的,并且被调用。 问题答案: 正如本杰明所说,不要使用循环,而应使用(或) 在此处查看

  • C++11中引入了序列for循环以实现区间遍历的简化。这里所谓的区间可以是任一种能用迭代器遍历的区间,例如STL中由begin()和end()定义的序列。所有的标准容器,例如std::string、 初始化列表、数组,甚至是istream,只要定义了begin()和end()就行。 这里是一个序列for循环语句的例子: void f(const vector& v) { for (auto

  • 本文向大家介绍JavaScript for循环 if判断语句(学习笔记),包括了JavaScript for循环 if判断语句(学习笔记)的使用技巧和注意事项,需要的朋友参考一下 今天学习了JavaScript里面的for循环以及if的判断语句 for(初始值;循环条件;操作){   满足条件要执行的代码语句 } 初始值:循环前的初始化变量,通常为赋值表达式:建议用var赋值,可以加快运行速度。