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

问题描述:

我正在开发一个java程序,显示固定宽度的两个矩形的面积和周长&长度,当前日期并读取用户输入以求解线性方程。我可以询问用户是否要重新运行该程序。问题是,如果他们输入y或Y,程序将运行,但如果用户输入任何东西,程序将退出。我想检查该输入和查看是否:如何在读取用户输入后重新运行java程序

1-它AY或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'); 

    } 
+0

注意'do'-'while'环被认为是不好的做法,因为看到了继续条件,你必须向下滚动到它的最终目标。而是使用'while'。 –

在你的循环结束(但仍然在循环),你可以不喜欢

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; 
} 
+0

我没有尝试你的建议,但真的感谢你的评论! – user2066392

您可以添加一个do while当你问用户是否要重复该程序。

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

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'); 
+0

工作!谢谢 – user2066392

+0

你好,欢迎:) – davidxxx