如何实现询问用户是否要再次运行我的Java程序?

时间:2019-07-09 16:53:41

标签: java

我尝试了多种方法,但仍然想不出方法,因此我的代码询问用户是否要在Java中再次运行它。这是我两天后要完成的大学项目。

我已经尝试了一些do-while语句,但仍然无法正常工作。

public class Project_2_1
{
   public static void main(String args[])
   {

     System.out.println("My project");

     System.out.println("Project_2 Problem_1\n");

     System.out.println("This program computes both roots of a quadratic equation,\n");
     System.out.println("Given the coefficients A,B, and C.\n");

      double secondRoot = 0, firstRoot = 0;
      Scanner sc = new Scanner(System.in);


      System.out.println("Enter the value of a ::");
      double a = sc.nextDouble();

      System.out.println("Enter the value of b ::");
      double b = sc.nextDouble();

      System.out.println("Enter the value of c ::");
      double c = sc.nextDouble();

      double determinant = (b*b)-(4*a*c);
      double sqrt = Math.sqrt(determinant);

      if(determinant>0)
      {
         firstRoot = (-b + sqrt)/(2*a);
         secondRoot = (-b - sqrt)/(2*a);
         System.out.println("Roots are :: "+ firstRoot +" and "+secondRoot);
      }else if(determinant == 0){
         System.out.println("Root is :: "+(-b + sqrt)/(2*a));

      }
    }

  }

3 个答案:

答案 0 :(得分:1)

所以您要寻找的是循环的想法。循环有三种基本类型。

  • 一个for循环。通常用于loop上的Collection,例如ArrayListMapArray通常的语法如下:

    for (int i = 0; i < someSize; i++){ }
    
  • 一个while循环。当您不知道何时退出时,通常用于循环。一个简单循环的语法如下所示:

    boolean condition = true;
    while(condition) {  
        //Code that will make condition false in a certain scenario
    }
    
  • 一个do while循环。当您确定希望代码块至少运行一次时,这是while循环上的一种变体。这样的示例如下:

    boolean condition = //can be set default to true or false, whichever fits better
    do{
    //Any code you want to execute
    //Your code that will determine if the condition is true or false
    
    } while (condition);
    

do while循环最适合您的程序,因为您希望在每次运行程序时至少运行一次。因此,您需要做的就是将其粘贴在循环中并创建条件。

我让您开始使用以下骨架:

    Scanner sc = new Scanner(System.in);
    int choice = 0;
    do{
        System.out.println("Hi, I am being repeated until you tell me stop!"); //Replace this with your code

        System.out.println("Enter 1 to run the program again, 0 to exit.");
        choice = sc.nextInt();

    }while (choice == 1);
    sc.close();

答案 1 :(得分:0)

您可以在main方法中添加类似内容,但是将所有内容复制到此方法中,现在您的代码将运行,直到您终止该过程为止。

  while(true){
  System.out.println("My project");

     System.out.println("Project_2 Problem_1\n");

     System.out.println("This program computes both roots of a quadratic equation,\n");
     System.out.println("Given the coefficients A,B, and C.\n");

      double secondRoot = 0, firstRoot = 0;
      Scanner sc = new Scanner(System.in);


      System.out.println("Enter the value of a ::");
      double a = sc.nextDouble();

      System.out.println("Enter the value of b ::");
      double b = sc.nextDouble();

      System.out.println("Enter the value of c ::");
      double c = sc.nextDouble();

      double determinant = (b*b)-(4*a*c);
      double sqrt = Math.sqrt(determinant);

      if(determinant>0)
      {
         firstRoot = (-b + sqrt)/(2*a);
         secondRoot = (-b - sqrt)/(2*a);
         System.out.println("Roots are :: "+ firstRoot +" and "+secondRoot);
      }else if(determinant == 0){
         System.out.println("Root is :: "+(-b + sqrt)/(2*a));

      }

    }

我并没有给您完整的答案,但是请注意,如果您修改此代码以询问用户的一些输入,则可以为while循环设置不同的条件,并根据用户输入启动和停止程序。

答案 2 :(得分:0)

将所有代码放入单独的方法中,然后提示用户并询问用户是否要再次运行该程序。重复调用同一方法称为递归。

让方法运行。

public static void run() {
    ** put all the code from your main method here **

     Scanner s = new Scanner(System.in);
     System.out.println("Would you like to run program again? (Y for yes, N for no)");
     String decision = s.next();
     if(decision.equals("Y")) {
         run();
     } else {
         System.out.println("Finished");
     }
}

public static void main(String args[]) {
   run();
}