-1

This is my first post.

Using a Scanner class, I'm trying to let user input to choose to repeat the program or quit. The thing is my Do loop statement repeats the program and does not exit even if the Do Loop is false and should exit the program.

// loop repeat or quit
do { 
 //initialize variable
  int integer;
  int x = 1;
  int factorial = 1;

System.out.print("Please enter an integer \n");
integer = getInt.nextInt();
//loop for factorial 
//multiple each increment until it reaches the integer
while (x <= integer) {

  factorial *= x;
  x++;

 }; // factorial=x*x
  System.out.println("the factorial of the integer " + integer + " is " + factorial);

 System.out.print("do you want to quit? y or n \n");
 quit = getString.next();


 } while(quit != yes);


 System.exit(0);
 }
Saro Taşciyan
  • 5,210
  • 5
  • 31
  • 50
user3502443
  • 1
  • 1
  • 2
  • Ignoring that this code wouldn't even compile, possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Brian Roach Apr 09 '14 at 02:36
  • @user3502443 OP please select a correct answer by clicking the checkmark on the left hand side of the chosen solution. – artifex_somnia Apr 13 '14 at 03:20

3 Answers3

0

There were a few mistakes in your code, so I rewrote it a little bit and used the correct functions where you used incorrect ones.

    public static void main(String args[])
    {
    // Scanner is used to take in inputs from user
    Scanner scan = new Scanner (System.in);
    String quit = "";

    // loop repeat or quit
    do { 
        //initialize variable
        int integer = 0;
        int x = 1;
        int factorial = 1;

        // User needs to enter integer, or it'll throw exception.
        System.out.println("Please enter an integer");
        integer = scan.nextInt();

        //loop for factorial 
        //multiple each increment until it reaches the integer
        // factorial = x!
        while (x <= integer) {
            factorial *= x;
            x++;
        }; 

        System.out.println("the factorial of the integer " + integer + " is " + factorial);
        System.out.println("do you want to quit? y or n");
        quit = scan.next();

         // if quit is NOT equal to y, we do it again
     } while(!quit.equals("y"));

     System.exit(0);
}

I hope the comments helps :)

tony
  • 88
  • 8
0

I've edited your code and it now runs.

For future reference: include more comprehensive snippets so viewers of your code can more easily discover mistakes.

Problem: There is no way to guarantee the user only inputs y without any spaces . THe easy solution to this problem is to use the string method contains(). I've modified your loop so that if the user input y the program will exit and it now works. Let me know if this works and happy coding!

public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        String quit ="";

        do { //initialize variable
            int integer; int x = 1; int factorial = 1;

             System.out.print("Please enter an integer \n");
             integer = in.nextInt();
             //loop for factorial 
             //multiple each increment until it reaches the integer
             while (x <= integer) {

               factorial *= x;
               x++;

              }; // factorial=x*x
               System.out.println("the factorial of the integer " + integer + " is " + factorial);

              System.out.print("do you want to quit? y or n \n");
              quit = in.next();


              } while(!quit.contains("y"));


              System.exit(0);


    }
artifex_somnia
  • 438
  • 9
  • 20
-1

Shouldn't it be

while(quit != "y");

I also don't understand why you use System.out.print(); and then use \n when there's a perfectly good System.out.pritnln();

Also, since we're dealing with Strings the .nextLine(); is good enough for the Scanner. (You'll have to declare String quit as well.)

acahreonn
  • 54
  • 4