简体   繁体   中英

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

    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 . In your case , as you have to execute the code at least once , do while is a perfect choice for your program

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.

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

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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