java - 无法让我的 while 语句正常工作

标签 java loops while-loop infinite

该程序的错误在于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("");

    }

}

最佳答案

如果您只想执行一次,为什么还需要 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;
}

关于java - 无法让我的 while 语句正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12594884/

相关文章:

php - 如何安排PHP for循环值?

c - 为什么我的 while 循环条件不起作用?

java - 从控制台运行某些内容时遇到问题

java - 为什么在泛型中需要 "? extends"

Laravel Blade 模板 - 如何在 foreach 循环中使用增量变量

Matlab:为多个序列创建循环

c++ - 为什么这个 'if' 语句循环?

c++ - 为什么空的 while 循环与其中包含某些内容的 while 循环的 react 不同?

java - 使用正则表达式重新格式化代码

java - 使用简单的模拟对象