简体   繁体   English

Java do-while循环中无限嵌套的for循环的问题

[英]Issue with Endless nested for-loops in do-while loop in java

I am coding a lab for my high school comp. 我正在为我的高中班级编写实验室代码。 sci. 科学 class. 类。 The objective of the program is to read data from a file, one integer at a time and print an inverted triangle. 该程序的目的是从文件中读取数据,一次读取一个整数并打印一个倒三角形。

The data is a txt file with: 3 3 7 4 (in the file the data is arranged vertically with 3 at the top, but I can't format that here for some reason) 数据是一个txt文件,其内容为:3 3 7 4(在文件中,数据垂直排列,顶部为3,但由于某种原因我无法在此处进行格式化)

I then changed the .txt extension to .dat 然后,我将.txt扩展名更改为.dat

It should print an inverted triangle with the top row being 3 and each successive row having 1 less asterisk (again, I cant figure out how to format this. I tried and it didnt work). 它应该打印一个倒三角形,第一行为3,每行连续的星号少1(同样,我不知道该如何设置格式。我尝试了,但没有成功)。 If the int is 3 then the top row is 3, if the int is 7 then the top row is 7 etc. 如果int为3,则第一行为3;如果int为7,则第一行为7,依此类推。

I am getting stuck in a nested for-loop somehow even though I have my decrement operator that should eventually get the loop to terminate, however it does not. 即使我有我的减量运算符应该最终使循环终止,我还是以某种方式陷入了嵌套的for循环中,但是事实并非如此。 You will see the total weirdness. 您将看到总的怪异。

public class Triangle2_PR31 
{

    public static void main(String[] args) throws IOException 
    { 
        Scanner triScan = new Scanner(new File("pr31.dat"));//reads file
        int a = triScan.nextInt();
        System.out.println(a);//prints number for verification 
        int b;//instantiate

        do
        {
            //a = triScan.nextInt();
            for(int x = 1; x<=a; x--)//main for-loop
            {
                for(b = a; b>=0; b--)//nested for-loop to print asterisks 
                {
                    System.out.print("*");
                    System.out.println(b);//print to see what happened to b
                }
                System.out.println(b);//print to see if loops gets out here
                a--;//decrease a so loop eventually terminates
                //Doesnt get out here
            }
        }       
        while(triScan.hasNext());
        triScan.close();
    }       
}

When the compiler gets to the SOP(b); 当编译器到达SOP(b); line in the nested for-loop, the thing goes all the way into negative 50,000 before I manually terminate the program, I let it go to -200,000 once for fun. 嵌套的for循环中的一行,在手动终止程序之前,事情一路变成负50,000,我让它一次达到-200,000,这很有趣。 I do not understand why it does not simply terminate. 我不明白为什么它不能简单地终止。 I stared at eclipse for 10 minutes trying all sorts of different stuff but I cant figure out what is wrong. 我凝视着日食10分钟,尝试各种不同的东西,但我不知道出了什么问题。

You have this evaluation in the first "for": 您在第一个“ for”中具有此评估:

for (int x = 1; x <= a; x--)

"X" will always be less than a. “ X”将始终小于a。

It must be for(int x = 1; x <= a; x++) without the double quotes. 它必须为for(int x = 1; x <= a; x++)且不带双引号。

您的主要for循环以与a减少相同的速率减少x,因此x永远不会比引起无限for循环的增长大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM