Needyboy
Needyboy

Reputation: 43

How do I end the loop completely after I input "q"?

The program does not stop after I input "q"; it still prints the random number and the sum of it. I want the program to completely stop right after I input "q".

import java.util.Scanner;
import java.util.Random;
public class lab7
{
public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    Random rand = new Random();
    String letter = " ";
    int num = 0;
    System.out.print("Push your luck");
      */the program prints push your luck! 
    while(!letter.equals("q")){
        int sum = rand.nextInt(12);
        System.out.println(" ");
        letter = input.nextLine();
         */ the program should not print random number or sum when input "q" 
         */but in this case it does
        System.out.println("Random number: " + sum);
        System.out.println("Updated number: " + num);
        num = num + sum;
}
}
}

Upvotes: 1

Views: 159

Answers (5)

lewa9
lewa9

Reputation: 41

You can make an if clause which checks if q was entered and use the break; statement to stop it. The continue statement would also work for this case.

Upvotes: 0

kai
kai

Reputation: 905

A for loop should do.

for(String aletter = input.nextLine();!aletter.equals("q");aletter=input.nextLine()) { 
    int sum = rand.nextInt(12);
    System.out.println(" ");
    System.out.println("Random number: " + sum);
    System.out.println("Updated number: " + num);
    num = num + sum;
}

And well as you dont use the input it can be simplified:

while(!input.nextLine().equals("q")) { 
    int sum = rand.nextInt(12);
    System.out.println(" ");
    System.out.println("Random number: " + sum);
    System.out.println("Updated number: " + num);
    num = num + sum;
}

And if you wanted at least one output before you are able to stop it with "q":

do { 
    int sum = rand.nextInt(12);
    System.out.println(" ");
    System.out.println("Random number: " + sum);
    System.out.println("Updated number: " + num);
    num = num + sum;
}while(!input.nextLine().equals("q"));

Upvotes: 0

forpas
forpas

Reputation: 164089

First give your variables meaningful names. So num should be sum and sum shoud be num.
Next rearrange your code inside the loop like this:

Scanner input = new Scanner(System.in);
Random rand = new Random();
String letter = " ";
int sum = 0;
System.out.print("Push your luck");
while (!letter.equals("q")) {
    int num = rand.nextInt(12);
    System.out.println(" ");
    letter = input.nextLine();
    if (!letter.equals("q")) {
        sum = sum + num;
        System.out.println("Random number: " + num);
        System.out.println("Updated number: " + sum);
    }
}

The if statement inside the loop prevents any further code from being executed when entered q.

Upvotes: 1

Owen Pauling
Owen Pauling

Reputation: 11841

Just move the input to the last line of the loop, and add an input just before. That way your loop condition is always checked immediately after the input has been received.

letter = input.nextLine();
while(!letter.equals("q")){
        int sum = rand.nextInt(12);
        System.out.println(" ");
        System.out.println("Random number: " + sum);
        System.out.println("Updated number: " + num);
        num = num + sum;
        letter = input.nextLine();
}

Upvotes: 0

dave
dave

Reputation: 64657

Looks like you want to do:

letter = input.nextLine();
while(!letter.equals("q")) {
    int sum = rand.nextInt(12);
    System.out.println(" ");
    System.out.println("Random number: " + sum);
    System.out.println("Updated number: " + num);
    num = num + sum;
    letter = input.nextLine();
}

Upvotes: 0

Related Questions