简体   繁体   English

Java询问用户是否要继续程序

[英]Java ask user if they want to continue a program

Hello i was curious is there any alternative way to make this code short but also it will loop the program when the input is met in "Enter 1 continue" i want the code reading faster because its too long and i want to make it short because so the program will run faster您好,我很好奇是否有任何其他方法可以缩短此代码,但在“输入 1 继续”中满足输入时它会循环程序所以程序会运行得更快

    System.out.print("Enter first number: ");
    fnum = sc.nextInt();
    System.out.print("Enter second number: ");
    snum = sc.nextInt();
    
    if (fnum <= snum) {
        while (fnum <= snum) {
            System.out.println(fnum);
            fnum++;
        }
    } else {
        while (fnum >= snum) {
            System.out.println(fnum);
            fnum--;
        }
    }
    do {
    System.out.print("Enter 1 to continute, enter any number to end: ");
    enter = sc.nextInt();
    if (enter == 1) {
        System.out.print("Enter first number: ");
        fnum = sc.nextInt();
        System.out.print("Enter second number: ");
        snum = sc.nextInt();
    
        if (fnum <= snum) {
            while (fnum <= snum) {
                System.out.println(fnum);
                fnum++;
            }
        } else {
            while (fnum >= snum) {
                System.out.println(fnum);
                fnum--;
            }
        }
      } else {
        System.out.println("************************************************");
        System.out.println("************************************************");
        System.out.println("******************Thank you*********************");
        System.out.println("************************************************");
        System.out.println("************************************************");
        System.exit(0);
    }
   } while (enter == 1);

} } } }

    int enter=0;
        do {
            if(enter==0){
                System.out.print("Enter first number: ");
                int fnum = sc.nextInt();
                System.out.print("Enter second number: ");
                int snum = sc.nextInt();
                if (fnum <= snum) {
                    while (fnum <= snum) {
                        System.out.println(fnum);
                        fnum++;
                    }
                } else {
                    while (fnum > snum) {
                        System.out.println(fnum);
                        fnum--;
                    }
                }
            }
            System.out.print("Enter 0 to continue, enter any number to end: ");
            enter = sc.nextInt();

        } while (enter==0);

Try this way ,your duplicated code can be removed .试试这种方式,你的重复代码可以被删除。

So whenever you are trying to run the code based on the condition that is met by the user input , make sure that you always terminate the program inside the while loop , because do while and while are mostly used for the purpose of executing a code indefinitely until a certain condition is met .因此,每当您尝试根据用户输入满足的条件运行代码时,请确保始终在 while 循环内终止程序,因为do whilewhile主要用于无限期执行代码的目的。直到满足某个条件。 In your case , as you have to execute the code at least once , do while is a perfect choice for your program就您而言,由于您必须至少执行一次代码,因此 do while 是您程序的完美选择

The first step would be to think about what it is you want to repeat.第一步是考虑你想重复什么。 Regardless of what the user wants to do, you want to execute the loop at least once, so a do-while is perfect.无论用户想要做什么,您都希望至少执行一次循环,因此do-while是完美的。

Once the user has input the first two values, you then want to prompt the user to determine if they want to continue or not, this forms part of your exit condition用户输入前两个值后,您希望提示用户确定他们是否要继续,这构成了退出条件的一部分

Scanner sc = new Scanner(System.in);
String response = "Y";
do {
    System.out.print("Enter first number: ");
    int fnum = sc.nextInt();
    sc.nextLine();
    System.out.print("Enter second number: ");
    int snum = sc.nextInt();
    sc.nextLine();

    if (fnum <= snum) {
        while (fnum <= snum) {
            System.out.println(fnum);
            fnum++;
        }
    } else {
        while (fnum >= snum) {
            System.out.println(fnum);
            fnum--;
        }
    }

    System.out.println("Do you wish to try again [Y/N]?");
    response = sc.nextLine();
} while ("Y".equalsIgnoreCase(response));
System.out.println("************************************************");
System.out.println("************************************************");
System.out.println("******************Thank you*********************");
System.out.println("************************************************");
System.out.println("************************************************");

nb: For education reasons, I've made use of response as a exit variable, you could use sc.nextLine() directly注意:出于教育原因,我使用response作为退出变量,您可以直接使用sc.nextLine()

So, basically, this will continue to loop until the user inputs anything other then Y to the "continue" question (you could check for N as well, but that's up to you)因此,基本上,这将继续循环,直到用户在“继续”问题中输入Y任何内容(您也可以检查N ,但这取决于您)

I just do this我只是这样做

    do {
        // your code
        System.out.print("Press [1] to continue: ");
    } while (sc.nextInt() == 1);

put your code inside, it will repeat if you input 1 with the do while method把你的代码放在里面,如果你用 do while 方法输入 1,它会重复

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

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