要求是/否回到循环

0 java loops

如果用户回答Y/y,我该如何回到try循环?我知道下面的代码不要求用户你想再试一次(是/)?.我目前只能使用bufferedReader.

import java.io.*;

public class Num10 {
    public static void main(String[] args){
        String in="";
        int start=0, end=0, step=0;

        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));


        try{
            System.out.print("Input START value = ");
            in=input.readLine();
            start=Integer.parseInt(in);
            System.out.print("Input END value = ");
            in=input.readLine();
            end=Integer.parseInt(in);
            System.out.print("Input STEP value = ");
            in=input.readLine();
            step=Integer.parseInt(in);
        }catch(IOException e){
            System.out.println("Error!");
        }

        if(start>=end){
            System.out.println("The starting number should be lesser than the ending number");
            System.exit(0);
        }else
        if(step<=0){
            System.out.println("The step number should always be greater than zero.");
            System.exit(0);
        }

        for(start=start;start<=end;start=start+step){
            System.out.println(start);
        }       

        System.out.println("\nDo you want to try again (Y/N)?");
    }
}
Run Code Online (Sandbox Code Playgroud)

Chr*_*rle 7

一个do ... while循环将是适宜的.

boolean again = false;
do
{
     // your code here
     again = askQuestion();
} while(again);
Run Code Online (Sandbox Code Playgroud)

或者你可以只使用常规while循环:

boolean again = true;
while(again)
{
    // your code here
    again = askQuestion();
}
Run Code Online (Sandbox Code Playgroud)

对于这两者,askQuestion是另一种方法,询问用户一个问题并返回truefalse.