// Elliot Mersch
// 10/3/12
// Dice.java
import java.util.Scanner;
import java.util.*;
public class Dice
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
int guess;
int rollA, rollB, sum;
int count = 0;
String play;
boolean playBool = true;
Roll myRoll = new Roll();
do{
System.out.println("Dice Game");
System.out.println("Enter a number 2-12: ");
guess = keyboard.nextInt();
do{
count++;
rollA = myRoll.getRollA();
rollB = myRoll.getRollB();
sum = rollA + rollB;
System.out.println("Roll 1: " + rollA);
System.out.println("Roll 2: " + rollB);
System.out.println("Sum: " + sum);
if (sum == guess)
{
System.out.println("Match!");
}
else
{
System.out.println("No match.");
}
}
while(count < 3 || guess != sum);
System.out.println("Do you want to play again? (yes/no): ");
play = keyboard.nextLine();
switch (play){
case "yes": playBool = true;
case "no": playBool = false;
default: System.out.println("Invalid input.");
}
}
while(playBool == true);
}
}
Do While loop won't pause for input
Page 1 of 13 Replies - 325 Views - Last Post: 04 October 2012 - 09:02 AM
#1
Do While loop won't pause for input
Posted 04 October 2012 - 08:44 AM
At the end of my initial do while loop, I want it to prompt the user if they want to play again, but instead of pausing the loop and allowing the user to input yes or no, it automatically inputs nothing and outputs "Invalid input." (default case on my switch).
Replies To: Do While loop won't pause for input
#2
Re: Do While loop won't pause for input
Posted 04 October 2012 - 08:51 AM
That's because you made these tow consecutive calls:
The problem is because you used nextInt(), which does not also "eat up" the "enter" character (\n or \r or \n\r) that you get when submitting you answer. You need to do this:
And then it will work.
scanner.nextInt() ... scanner.nextLine()
The problem is because you used nextInt(), which does not also "eat up" the "enter" character (\n or \r or \n\r) that you get when submitting you answer. You need to do this:
guess = keyboard.nextInt(); keyboard.nextLine(); // Eat up the linefeed
And then it will work.
#3
Re: Do While loop won't pause for input
Posted 04 October 2012 - 08:59 AM
Worked like a charm. Thanks for the help and quick response man.
#4
Re: Do While loop won't pause for input
Posted 04 October 2012 - 09:02 AM
You're very welcome. Glad I could help.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote






|