Java 如何使用while循环要求程序在结束时重试?

Java 如何使用while循环要求程序在结束时重试?,java,Java,我正在尝试使用while循环将这两个单独的程序合并为一个程序,我已经尝试了一周,但仍然不知道将“您想再试一次(Y/N)”循环放在哪里。任何帮助或提示都行 第一个单独的工作程序: 它将要求用户输入三个整数。起始编号、大于起始编号的结束编号和步骤编号 import java.util.Scanner; public class sample { public static void main(String args[]){ Scanner in = new Scanner(

我正在尝试使用while循环将这两个单独的程序合并为一个程序,我已经尝试了一周,但仍然不知道将“您想再试一次(Y/N)”循环放在哪里。任何帮助或提示都行

第一个单独的工作程序: 它将要求用户输入三个整数。起始编号、大于起始编号的结束编号和步骤编号

import java.util.Scanner;
public class sample {

    public static void main(String args[]){
        Scanner in = new Scanner(System.in);

    String input;
        int first , second , third ;

        System.out.print("First number: ");
        input = in.nextLine();
        first = Integer.parseInt(input);
        System.out.print("Second number:: ");
        input = in.nextLine();
        second = Integer.parseInt(input);
        System.out.print("Third number: ");
        input = in.nextLine();
        third = Integer.parseInt(input);

        while(first <= second)
        {
            first = second + third;
            System.out.println(first);

        }
    }
}
示例输出: 是否要重试(是/否)?n



我试图得到这样的输出:

public class TheProgram {
    private static Scanner in = new Scanner(System.in);

    private static void interact() {

        String input;
        int first , second , third ;

        System.out.print("First number: ");
        input = in.nextLine();
        first = Integer.parseInt(input);
        System.out.print("Second number:: ");
        input = in.nextLine();
        second = Integer.parseInt(input);
        System.out.print("Third number: ");
        input = in.nextLine();
        third = Integer.parseInt(input);

        while(first <= second)
        {
            first = second + third;
            System.out.println(first);

        }
    }

    public static void main(String args[]) {
        char c = 'y';

        while (c == 'y') {
            interact();
            System.out.print("Do you wish to continue? ");
            c = in.next().toLowerCase().charAt(0);
        }
    }
}

起点:1 完:10 步骤:2 1. 3. 5. 7. 9 是否要重试(是/否)?n


如果我输入“y”,它会让我从一开始就返回,并要求我输入开始、结束和步骤。如果我输入“n”,它应该会终止程序。
... 仅使用while循环将两者结合起来。

为什么将其作为两个独立的程序?您应该只在一个程序中执行此操作,并使用一个布尔值设置为true的if循环,除非他们键入no,否则键入no将导致程序退出。

您只需将第一个程序包装到第二个程序的循环中即可。如果您将第一个程序的操作分离到一个单独的方法中,则会更加清晰。结果可能是这样的:

public class TheProgram {
    private static Scanner in = new Scanner(System.in);

    private static void interact() {

        String input;
        int first , second , third ;

        System.out.print("First number: ");
        input = in.nextLine();
        first = Integer.parseInt(input);
        System.out.print("Second number:: ");
        input = in.nextLine();
        second = Integer.parseInt(input);
        System.out.print("Third number: ");
        input = in.nextLine();
        third = Integer.parseInt(input);

        while(first <= second)
        {
            first = second + third;
            System.out.println(first);

        }
    }

    public static void main(String args[]) {
        char c = 'y';

        while (c == 'y') {
            interact();
            System.out.print("Do you wish to continue? ");
            c = in.next().toLowerCase().charAt(0);
        }
    }
}
公共类程序{
专用静态扫描仪in=新扫描仪(System.in);
私有静态void interactive(){
字符串输入;
第一,第二,第三;
系统输出打印(“第一个数字:”);
input=in.nextLine();
first=Integer.parseInt(输入);
系统输出打印(“第二个编号:”);
input=in.nextLine();
第二个=整数.parseInt(输入);
系统输出打印(“第三个数字:”);
input=in.nextLine();
第三个=整数.parseInt(输入);
而