【问题标题】:Cannot get my while statement to work properly无法让我的 while 语句正常工作
【发布时间】:2012-09-26 05:07:03
【问题描述】:

这个程序的错误是main方法中的while语句。从某种意义上说,当我输入无效的输入值时,它会停止并退出程序,当我输入正确的值时,它会循环一次 while 语句,但在完成更多工作后,我的代码突然循环无穷大,任何输入低于 1025。忽略程序输出的草率,因为我仍在编辑它,主要问题是为什么它不会停止在 while 语句中循环。

import java.util.Scanner;

public class DNS
{
    public static void main(String[] args)
    {
        int y;
        Scanner input = new Scanner( System.in);

        System.out.println("java DisplayNumberSystems");
        System.out.println("Enter a decimal value to display to: ");
        y = input.nextInt();

        while(y !=1025)
        {

            for(int x=0; x <=y; x++)
            {
                System.out.println("Decimal");
                System.out.println(x);
            }


            for(int i=0; i <=y; i++)
            {
                System.out.println("");
                System.out.println("Binary");
                convertToBinary(i);
            }

            System.out.println("");             

            for(int z=0; z <=y; z++)
            {
                System.out.println("Hex");
                convertToHex(z);
            }

            System.out.println("");

            for(int d=0; d <=y; d++)
            {
                System.out.println("Octal");
                convertToOctal(d);
                System.out.println("");
            }

        }
    }

    public static void convertToBinary(int x)
    {

        if(x >0)
        {

            convertToBinary(x/2);
            System.out.printf(x%2 + "");

        }

    }

    public static void convertToOctal(int x)
    {
        int rem; 

        while(x >0)
        {
            rem = x%8;
            System.out.printf("%d", rem);
            x=x/8;
        }
    }

    public static void convertToHex(int x)
    {
        int rem;

        while(x >0)
        {

        rem = x%16;
            switch(rem)
            {
                case 1: 
                    System.out.printf("1");
                    break;

                case 2: 
                    System.out.printf("2");
                    break;

                case 3:
                    System.out.printf("3");
                    break;

                case 4: 
                    System.out.printf("4");
                    break;

                case 5:
                    System.out.printf("5");
                    break;

                case 6:
                    System.out.printf("6");
                    break;

                case 7: 
                    System.out.printf("7");
                    break;

                case 8:
                    System.out.printf("8");
                    break;

                case 9:
                    System.out.printf("9");
                    break;

                case 10: 
                    System.out.printf("A");
                    break;
                case 11: 
                    System.out.printf("B");
                    break;

                case 12: 
                    System.out.printf("C");
                    break;

                case 13: 
                    System.out.printf("D");
                    break;

                case 14: 
                    System.out.printf("E");
                    break;

                case 15: 
                    System.out.printf("F");
                    break;
            }
            x=x/16;
        }
        System.out.println("");

    }

}

【问题讨论】:

  • 圣牛代码重复

标签: java loops while-loop infinite


【解决方案1】:

如果只执行一次while 循环,为什么需要它?请考虑使用if

没关系,打破while 循环怎么样? 您可以在适用的情况下使用break; 来停止执行循环。

例如:

while (y != 1025) {

    for (int x = 0; x <= y; x++) {
        System.out.println("Decimal");
        System.out.println(x);
    }

    for (int i = 0; i <= y; i++) {
        System.out.println("");
        System.out.println("Binary");
        convertToBinary(i);
    }

    System.out.println("");

    for (int z = 0; z <= y; z++) {
        System.out.println("Hex");
        convertToHex(z);
    }

    System.out.println("");

    for (int d = 0; d <= y; d++) {
        System.out.println("Octal");
        convertToOctal(d);
        System.out.println("");
    }
    break;
}

【讨论】:

    【解决方案2】:

    你没有在 while 内的任何地方增加 y 的值。 因此,对于低于 1025 的 y 值,假设 100.y 始终为 100,并且每次检查条件 y!= 1025 时为真。因此,请输入增量 y = y + 1 或任何内容,它会做。

    【讨论】:

    • 我不能增加 y 因为它是我的 for 函数在它里面的计数。我也将其更改为
    【解决方案3】:
    while(y !=1025)
    

    在这一行中,当 y 为 lower than 1025higher than 1025 时,您的循环工作。
    你也没有改变y value

    【讨论】:

    • 我现在将它从 != 更改为
    【解决方案4】:

    正如上面的人所说,它会一直循环。除非 y 发生变化,否则它将始终不等于 1025,除非它的原始值为 1025。

    您到底想达到什么目的?你应该包括 y = input.nextInt();在循环内部,如果条件为真,循环就会中断?

    【讨论】:

      【解决方案5】:

      如果在 while 循环的末尾添加一个 break,它实际上会使 while 循环变得多余。它会循环一次然后退出。如果您只想对 1025 以外的值运行循环,则应使用 if then 语句...

      【讨论】:

        【解决方案6】:

        while 循环运行无限次的原因是因为您没有更新(可能增加)y 的值。您可能想在循环中添加类似以下语句的内容:

        y++;
        

        这将在每次循环迭代时将 y 的值增加 1

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-11-23
          • 1970-01-01
          • 1970-01-01
          • 2016-04-05
          • 1970-01-01
          • 1970-01-01
          • 2016-03-05
          • 1970-01-01
          相关资源
          最近更新 更多