如何在读取用户输入后重新运行java程序

时间:2017-02-25 21:01:05

标签: java netbeans-8

我正在开发一个java程序,它显示两个具有固定宽度和长度,当前日期的矩形的区域和周长,并从用户读取输入以求解线性方程。我能够询问用户是否要重新运行程序。问题是,如果他们输入y或Y,程序就会运行,但如果用户输入任何els,程序将退出。我想检查一下这个输入,看看是否:

1-这是y或Y,重新运行

2-它是n或N,退出

3-既不是1也不是2,再次询问用户是否要重新运行程序。

这是我的代码:

    public static void main(String[] args) {

    char ch = 'y'; 

    GregorianCalendar calendar = new GregorianCalendar();

    LinearEquation le = new LinearEquation(1.0,1.0,1.0,1.0,1.0,1.0);

    Rectangle rec1 = new Rectangle(4.0,40.0);
    Rectangle rec2 = new Rectangle(3.5,35.9);        

    Scanner input = new Scanner(System.in);

    Double a, b, c, d, e,f;

do{

    System.out.println("First rectangle info is: ");
    System.out.print(" width is: "+ rec1.getWidth() + 
                   "\n height is: " + rec1.getHeight()+ 
                   "\n Area is: "+ rec1.getArea() +
                   "\n Perimeter is: " + rec1.getPerimeter());

    System.out.println();
    System.out.println();

    System.out.println("Second rectangle info is: ");
    System.out.print(" width is: "+ rec2.getWidth() + 
                   "\n height is: " + rec2.getHeight()+ 
                   "\n Area is: "+ rec2.getArea() +
                   "\n Perimeter is: " + rec2.getPerimeter());

    System.out.println();

    System.out.println("Current date is: " + calendar.get(GregorianCalendar.DAY_OF_MONTH) +   
    "-" + (calendar.get(GregorianCalendar.MONTH) + 1)+ 
    "-" + calendar.get(GregorianCalendar.YEAR));

    System.out.println("Date after applying 1234567898765L to setTimeInMillis(long) is: ");

    calendar.setTimeInMillis(1234567898765L);
    System.out.println(calendar.get(GregorianCalendar.DAY_OF_MONTH) +   
    "-" + (calendar.get(GregorianCalendar.MONTH) + 1)+ 
    "-" + calendar.get(GregorianCalendar.YEAR));

    System.out.println();
    System.out.println("Please, enter a, b, c, d, e and f to solve the equation:");

    try{

        System.out.println("a: ");        
        a = input.nextDouble();

        System.out.println("b: ");
        b = input.nextDouble();

        System.out.println("c: ");
        c = input.nextDouble(); 

        System.out.println("d: ");
        d = input.nextDouble();

        System.out.println("e: ");
        e = input.nextDouble();

        System.out.println("f: ");
        f = input.nextDouble();

        le.setA(a);
        le.setB(b);
        le.setC(c);
        le.setD(d);
        le.setE(e);
        le.setF(f);

        if(le.isSolvable()){

            System.out.println("x is: "+ le.getX() + "\ny is: "+ le.getY());

        }else { 

            System.out.println("The equation has no solution.");
        }   

        //System.out.println("Would you like to re-run the program( y or n)");
        //ch = input.next().charAt(0);
}
catch (Exception ee) {

 System.out.println("Invalid input");
 //System.out.println("Would you like to re-run the program( y or n)");
 ch = input.next().charAt(0);     
  }

     System.out.println("Would you like to re-run the program( y or any other       input to quit)");
 ch = input.next().charAt(0);

  }while(ch == 'y' || ch == 'Y');

  }

2 个答案:

答案 0 :(得分:1)

当您询问用户是否要重复该计划时,您可以添加do while

此外,您可以使用while(ch == 'y' || ch == 'Y');方法减少要执行的测试次数,而不是在语句中指定大写和小写:Character.toLowerCase()

    do {
        System.out.println("Would you like to re-run the program( y or any other input to quit)");
        ch = input.next().charAt(0);
        ch = Character.toLowerCase(ch);
    } while (ch != 'y' && ch != 'n');

现在你的代码看起来像这样:

do {
      ....
        do {
            System.out.println("Would you like to re-run the program( y or any other input to quit)");
            ch = input.next().charAt(0);
            ch = Character.toLowerCase(ch);

           } while (ch != 'y' && ch != 'n');

   } while (ch == 'y');

答案 1 :(得分:0)

在循环结束时(但仍处于循环中),您可以执行类似

的操作
ch = 'x'; // Just a dummy value

while (ch != 'y' && ch != 'n') {
    System.out.prinln("Enter y or n:");
    ch = input.next().charAt(0);
}

if (ch == 'n') {
    break;
}