1

I tried multiple methods and I still can't figure out how to make it so that my code asks the user if they want to run it again in java. This is for my college project due in two days.

I've tried a couple of do-while statements but it still doesn't work.

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));

      }
    }

  }

  • 2
    `Run again` means `repetition` => `repetition` means `loop`... You want to execute it at least for one time, so a `do-while-loop` seems to be the right choice. If the program is running again depends to the user's decision means the scanner's input. Try it yourself... `do { /* Your program */ } while(true == decision);` – 0x1C1B Jul 09 '19 at 17:01

3 Answers3

1

So what you are looking for is the idea of a loop. There are three basic types of loops.

  • A for loop. Commonly used to loop over a Collection, such as an ArrayList, Map, or Array. Syntax for this typically looks like:

    for (int i = 0; i < someSize; i++){ }
    
  • A while loop. Typically used for loops when you do not know when they will exit. Syntax for a simple loop looks like this:

    boolean condition = true;
    while(condition) {  
        //Code that will make condition false in a certain scenario
    }
    
  • A do while loop. This is a variant on a while loop when you are certain you want the block of code to run at least one time. A sample of this looks like:

    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);
    

A do while loop is what fits your program the best, because you want to run it AT least a single time, every time the program is ran. So all you need to do is stick it inside of a loop and create your condition.

I got you started with the skeleton below:

    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();
Nexevis
  • 4,647
  • 3
  • 13
  • 22
0

You could add something like into the main method, but copy all the content into this while, now your code will run until you kill the process.

  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));

      }

    }

I'm not giving you the whole answer, but notice that if you modify this code asking some input from your user, you could set a different condition for the while loop and start and stop your program depending on user input.

cehdmoy
  • 46
  • 5
0

Put all your code into a separate method then prompt the user and ask if user wants to run the program again. This repetitive calling of the same method is called recursion.

Lets call the method run.

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();
}
Hayes Roach
  • 121
  • 10
  • 2
    This is not really a good reason to use recursion in my opinion, when a simple loop could achieve the same result. – Nexevis Jul 09 '19 at 17:23
  • I agree that a loop could achieve the same result but I don't think either way is "better" than the other. Using a loop may be easier to understand but the functionality is the same. @Nexevis – Hayes Roach Jul 09 '19 at 17:28
  • 2
    Well say that the number of times run is an insane amount, you risk hitting stackoverflow using recursion, which is not a risk with `while` loop. It also adds unnecessary complexity to a situation where it shouldn't be complex. If you start doing this all over the code, the code will be a mess. [See this post](https://stackoverflow.com/a/3093/11434552) – Nexevis Jul 09 '19 at 17:33
  • As just a small add on to show they are not quite equal, try setting your `decision` to true to get an infinite loop, and watch your code crash. Then try it with a `while` loop and you can see the code will not fail. – Nexevis Jul 09 '19 at 17:37
  • I agree that using recursion could cause an overflow/infinite loop if it is implemented incorrectly. In my function it asks the user for a decision each iteration so there is no risk in an infinite loop. I tested the code and all is good @Nexevis – Hayes Roach Jul 09 '19 at 17:42
  • 2
    What if the user decides to run it 6000 times? I tested your code on my system with 6000 runs and it fails at that number, does not fail at 5999. That is not that large of a number to expect something to run, unless there was some contract stating it will never be run that many times. There are certain scenarios where recursion _should_ be used and this isn't one of them. Also teaching a beginner recursion for a simple issue is definitely not a way to send him on a good road of programming. – Nexevis Jul 09 '19 at 17:50
  • You are correct. I was not thinking about running it that many times but yes it will eventually overflow the stack using recursion. For a small sample set this could be used but using a loop would be more efficient. I have up-voted your answer @Nexevis – Hayes Roach Jul 09 '19 at 18:03