如何实现询问用户是否要再次运行我的 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));


      }

    }


  }


呼啦一阵风
浏览 105回答 3
3回答

慕少森

所以你正在寻找的是循环的想法。存在三种基本类型的循环。一个for循环。常用于loopa 之上Collection,例如 an ArrayList, Map, or Array。其语法通常如下所示:for (int i = 0; i < someSize; i++){ }一个while循环。当您不知道循环何时退出时,通常用于循环。简单循环的语法如下所示:boolean condition = true;while(condition) {&nbsp;&nbsp;&nbsp; &nbsp; //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 betterdo{//Any code you want to execute//Your code that will determine if the condition is true or false} while (condition);循环do while最适合您的程序,因为您希望每次运行程序时至少运行一次。所以你需要做的就是将它放在一个循环中并创建你的条件。我让你从下面的骨架开始:&nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; int choice = 0;&nbsp; &nbsp; do{&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Hi, I am being repeated until you tell me stop!"); //Replace this with your code&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter 1 to run the program again, 0 to exit.");&nbsp; &nbsp; &nbsp; &nbsp; choice = sc.nextInt();&nbsp; &nbsp; }while (choice == 1);&nbsp; &nbsp; sc.close();

叮当猫咪

您可以在 main 方法中添加类似的内容,但将所有内容复制到此 while 中,现在您的代码将一直运行,直到您终止该进程。&nbsp; while(true){&nbsp; System.out.println("My project");&nbsp; &nbsp; &nbsp;System.out.println("Project_2 Problem_1\n");&nbsp; &nbsp; &nbsp;System.out.println("This program computes both roots of a quadratic equation,\n");&nbsp; &nbsp; &nbsp;System.out.println("Given the coefficients A,B, and C.\n");&nbsp; &nbsp; &nbsp; double secondRoot = 0, firstRoot = 0;&nbsp; &nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; &nbsp; System.out.println("Enter the value of a ::");&nbsp; &nbsp; &nbsp; double a = sc.nextDouble();&nbsp; &nbsp; &nbsp; System.out.println("Enter the value of b ::");&nbsp; &nbsp; &nbsp; double b = sc.nextDouble();&nbsp; &nbsp; &nbsp; System.out.println("Enter the value of c ::");&nbsp; &nbsp; &nbsp; double c = sc.nextDouble();&nbsp; &nbsp; &nbsp; double determinant = (b*b)-(4*a*c);&nbsp; &nbsp; &nbsp; double sqrt = Math.sqrt(determinant);&nbsp; &nbsp; &nbsp; if(determinant>0)&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;firstRoot = (-b + sqrt)/(2*a);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;secondRoot = (-b - sqrt)/(2*a);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Roots are :: "+ firstRoot +" and "+secondRoot);&nbsp; &nbsp; &nbsp; }else if(determinant == 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Root is :: "+(-b + sqrt)/(2*a));&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }我没有给你完整的答案,但请注意,如果你修改此代码以询问你的用户的一些输入,你可以为 while 循环设置不同的条件并根据用户输入启动和停止你的程序。

三国纷争

将所有代码放入一个单独的方法中,然后提示用户并询问用户是否要再次运行该程序。这种对同一个方法的重复调用称为递归。让我们调用方法运行。public static void run() {&nbsp; &nbsp; ** put all the code from your main method here **&nbsp; &nbsp; &nbsp;Scanner s = new Scanner(System.in);&nbsp; &nbsp; &nbsp;System.out.println("Would you like to run program again? (Y for yes, N for no)");&nbsp; &nbsp; &nbsp;String decision = s.next();&nbsp; &nbsp; &nbsp;if(decision.equals("Y")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;run();&nbsp; &nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Finished");&nbsp; &nbsp; &nbsp;}}public static void main(String args[]) {&nbsp; &nbsp;run();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java