exiting a do while loop using a string - java

Okay.
Here is my question. I am having some difficulty while making a dice game.
The part I am having issue with is making it exit using a do while loop.
Here is my code. and I'll explain what I mean further in
import java.util.Scanner;
public class Dice
{
public static void main (String[] args)
{
int diceGuess;
int rollNum, roll1, roll2, roll3;
String playAgain = "yes";
Scanner sc = new Scanner(System.in);
// Create two separate dice objects to create a pair of dice
Roll die1 = new Roll();
Roll die2 = new Roll();
// get from the user the number of dice rolls
System.out.print ("Please enter a number between 2 and 12 to begin: ");
diceGuess = sc.nextInt();
do
{
// loop to show rolls
for (rollNum = 1; rollNum <=3; rollNum++)
{
System.out.println("**********Roll #: " + rollNum + " ************");
roll1 = die1.roll();
roll2 = die2.roll();
//if statement to display you win if you win, and to make the loop break.
if(diceGuess==roll1+roll2)
{
System.out.println("You win!");
rollNum = 4;
}
if(rollNum == 3)
{
System.out.println("You lose.\n");
System.out.println("Would you like to play again?");
playAgain = sc.nextLine();
rollNum ++;
}
}
}while(playAgain == "yes");
}
}
This is my utility class.
I want it to ask the user if they want to play again, and then if the user inputs no, I want it to quit.
I'm sure this is just a minor misunderstanding on my part.
Thank you very much stackoverflow.
and heres the other class
//Justin Le
//Dice class
import java.util.Random;
public class Roll
{
//variables used in class
private Random randomRoll = new Random();
private int roll;
boolean playAgain = false;
//constructor
public Roll()
{
roll = 0;
}
//method named roll to return roll number and inside, calls a method to display the roll.
public int roll()
{
roll = randomRoll.nextInt(6) + 1;
showRoll(roll); //accesses showRoll to output in driver class.
return roll;
}
public boolean test()
{
playAgain = !playAgain;
return playAgain;
}
//displays picture of dice roll
private void showRoll(int r)
{
switch(r)
{
case 1:
System.out.println("One: \n" +
" \n " +
" * \n" +
" \n ");
break;
case 2:
System.out.println("Two: \n" +
"* \n" +
" \n" +
" *\n");
break;
case 3:
System.out.println("Three:\n" +
"* \n" +
" * \n" +
" *\n");
break;
case 4:
System.out.println("Four:\n" +
"* *\n" +
" \n" +
"* *\n");
break;
case 5:
System.out.println("Five:\n" +
"* *\n" +
" * \n" +
"* *\n");
break;
case 6:
System.out.println("Six: \n" +
"* *\n" +
"* *\n" +
"* *\n");
break;
default:
System.out.println("error\n");
}
}
}

In your while, use equals() method to compare Strings.. : -
playAgain.equals("yes")
See this excellent post for difference in comparison using equals() and ==..
In your first code: -
if(rollNum == 3)
{
System.out.println("You lose.\n");
System.out.println("Would you like to play again?");
//playAgain = sc.nextLine();
// Change your nextLine() to next()
playAgain = sc.next();
rollNum ++;
}

change
playAgain == "yes"
to
"yes".equals(playAgain)

use while(plyaAgain.equals("yes"));
Follow this link for details on string comparision :
How do I compare strings in Java?

Change this:
while(playAgain == "yes");
to this:
while(playAgain.equals("yes"));

Related

logical bug in my code in java... in switch case

package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
int operator;
double number1, number2, result;
boolean ask = true;
while (ask) {
System.out.println("please select your operator:\n"
+ "1 for +\n" +
"2 for -\n" +
"3 for *\n" +
"4 for %\n" +
"");
operator = myScanner.nextInt();
System.out.println("you chose " + operator + " operator babe");
System.out.println("please enter your first number");
Scanner numberScanner = new Scanner(System.in);
number1 = numberScanner.nextDouble();
System.out.println("please enter your second number");
Scanner numberScanner2 = new Scanner(System.in);
number2 = numberScanner2.nextDouble();
switch (operator) {
case 1:
result = number1 + number2;
System.out.println("result is:" + result);
break;
case 2:
result = number1 - number2;
System.out.println("result is:" + result);
break;
case 3:
result = number1 * number2;
System.out.println("result is:" + result);
break;
case 4:
result = number1 / number2;
System.out.println("result is:" + result);
break;
default:
System.out.println("you chosen the wrong operator babe :)");
break;
}
System.out.println("do yo want to continue?\n" +
"y for yes\n" +
"n for no\n");
char askInput = myScanner.next().charAt(0);
if (askInput=='n') ask=false;
}
}
}
i got trouble in my switch case
if i press any number or letter somthing like 5 or 6 or... it should print you chose wrong operator.
i think problem is in my default but i don't know where is it?
Just reorder your code like this
`public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
int operator;
double number1, number2, result;
boolean ask = true;
while (ask) {
System.out.println("please enter your first number");
Scanner numberScanner = new Scanner(System.in);
number1 = numberScanner.nextDouble();
System.out.println("please enter your second number");
Scanner numberScanner2 = new Scanner(System.in);
number2 = numberScanner2.nextDouble();
System.out.println("please select your operator:\n"
+ "1 for +\n"
+ "2 for -\n"
+ "3 for *\n"
+ "4 for %\n"
+ "");
operator = myScanner.nextInt();
switch (operator) {
case 1:
result = number1 + number2;
System.out.println("result is:" + result);
break;
case 2:
result = number1 - number2;
System.out.println("result is:" + result);
break;
case 3:
result = number1 * number2;
System.out.println("result is:" + result);
break;
case 4:
result = number1 / number2;
System.out.println("result is:" + result);
break;
default:
System.out.println("you chosen the wrong operator babe :)");
break;
}
System.out.println("you chose " + operator + " operator babe");
System.out.println("do yo want to continue?\n"
+ "y for yes\n"
+ "n for no\n");
char askInput = myScanner.next().charAt(0);
if (askInput == 'n') {
ask = false;
}
}
}`
and you'll be fine
as for my comment, if you want to validate the input the user does (for the option) before having the user input another 2 numbers, than, yeah you should actually programm it that way that the validation goes RIGHT AFTER the first userinput. HereĀ“s a slightly corrected version of your code.
public static void main(String[] args) {
int operator;
double result;
boolean ask = true;
Scanner numberScanner = new Scanner(System.in);
while (ask) {
System.out.println(
"please select your operator:\n" + "1 for +\n" + "2 for -\n" + "3 for *\n" + "4 for %\n" + "");
operator = numberScanner.nextInt();
System.out.println("you chose " + operator + " operator babe");
// Here was your "Mistake". You instantly started asking the user for another input,
// but actually wanted to ahve the switch statment here
switch (operator) {
case 1:
result = get_num1(numberScanner) + get_num2(numberScanner);
System.out.println("result is:" + result);
break;
case 2:
result = get_num1(numberScanner) - get_num2(numberScanner);
System.out.println("result is:" + result);
break;
case 3:
result = get_num1(numberScanner) * get_num2(numberScanner);
System.out.println("result is:" + result);
break;
case 4:
result = get_num1(numberScanner) % get_num2(numberScanner);
System.out.println("result is:" + result);
break;
default:
System.out.println("you chosen the wrong operator babe :)");
break;
}
System.out.println("do yo want to continue?\n" + "y for yes\n" + "n for no\n");
char askInput = numberScanner.next().charAt(0);
if (askInput == 'n')
ask = false;
}
}
public static double get_num1(Scanner scanner) {
System.out.println("please enter your first number");
return scanner.nextDouble();
}
public static double get_num2(Scanner scanner) {
System.out.println("please enter your second number");
return scanner.nextDouble();
}
simply you could validate the operator while you assign it with the input.
for example use if condition and check whether its between 1 and 5 and if not print whatever you want
2 things:
you dont need 2 scanners using only one will be enough
the code is behaving so because you go into the switch case AFTER asking the numbers you want to operate...
some condition like:
operator = myScanner.nextInt();
if (operator < 1 || operator > 4) {
}
may help....

How can i add the random number to my total for java(blackjack)?

This is my code:
import java.util.*;
import java.util.Scanner;
public class Assignment2 {
public static void main(String args[]){
Scanner stdin = new Scanner(System.in);
Random random = new Random();
int ran2 = (random.nextInt(10));
int ran1 = (random.nextInt(10));
int total = ran1 + ran2;
char exit = 'y';
System.out.println("First cards: " + ran1 + ", " + ran2);
System.out.println("Total: " + total);
while(exit != 'n' && total < 21){
System.out.println("Do you want another card? (y/n): ");
exit = stdin.next().charAt(0);
System.out.println("Card: "+ (random.nextInt(10)));
total = total + (random.nextInt(10));
System.out.println("Total: "+ total);
}
}
}
When I enter n, how can I make it so the program exit, instead of printing out the total again?
Check this out:
public class Assignment2 {
public static void main(String args[]){
int next = 0;
Scanner stdin = new Scanner(System.in);
Random random = new Random();
int ran2 = (random.nextInt(10));
int ran1 = (random.nextInt(10));
int total = ran1 + ran2;
char exit = 'y';
System.out.println("First cards: " + ran1 + ", " + ran2);
System.out.println("Total: " + total);
while(exit != 'n' && total < 21){
System.out.println("Do you want another card? (y/n): ");
exit = stdin.next().charAt(0);
next = random.nextInt(10);
System.out.println("Card: "+ next);
total = total + next;
System.out.println("Total: "+ total);
}
if (exit.equals('n'))
system.exit(0);
}
}
Now the program exists after you enter n by calling system.exit(0).
You need to call nextInt just once, so you won't create 2 different random numbers. So I put the first call into a variable next so you could use it as many times as you please without having to call nextInt again.
If you want the program to exit immediately after the user enters n, you will want to put the if statement right after the exit = stdin.next().charAt(0);
If you want to exit the loop, you can break from it. Basically you have to do this(I have written comments to highlight the alterations)-
import java.util.*;
import java.util.Scanner;
public class Assignment2 {
public static void main(String args[]){
Scanner stdin = new Scanner(System.in);
Random random = new Random();
int ran2 = (random.nextInt(10));
int ran1 = (random.nextInt(10));
int total = ran1 + ran2;
char exit = 'y';
System.out.println("First cards: " + ran1 + ", " + ran2);
System.out.println("Total: " + total);
while(exit != 'n' && total < 21){
System.out.println("Do you want another card? (y/n): ");
exit = stdin.next().charAt(0);
//you need to check here if the user entered 'n'. I have used a break opertion
//to break from the loop and print the total outside the loop. But if you want
//to exit the program altogether, just replace break with exit(0) :)
if(total >= 21 or exit == 'y') {
break;
}
//As Idos correctly pointed out that by calling random.nextInt(10) two
//times you have a very big chance of creating two different random numbers.
//So it makes sense to put the first call into a variable nextNumber.
int nextNumber = random.nextInt(10);
total = total + (nextNumber);
//Now we should again check for total. If it is greater than or equal to 21
//I am again breaking from the loop. Feel free to replace break with exit(0).
if(total >= 21) {
break;
}
System.out.println("Total: "+ total);
}
System.out.println("Your total- "+ total);
}
}

Java Lottery Array Program

I am designing a program for my class that is supposed to simulate a lottery game. I am supposed to design a method that generates random lottery numbers, a method that asks and stores the user for their number choices, a method that compares the arrays to find how many numbers are the same, and then I am supposed to call them all back up to the main method, and create my output statement that contains some if statements that determine which prize is awarded for the particular amount of matches.
Here is what I have thus far
import java.util.*;
public class LotteryGame {
/**
The main method is the program's starting point
*/
public static void main(String[] args){
int NUM_DIGITS = 5;
int[] userDigits = new int[5];
int[] lotteryNumbers = new int[5];
int sameNum;
generateNumbers(lotteryNumbers);
getUserData(userDigits);
compareArrays();
System.out.println("Lottery numbers: " + lotteryNumbers[0] + " " +
lotteryNumbers[1] + " " + lotteryNumbers[2] + " " + lotteryNumbers[3] +
" " + lotteryNumbers[4] + " ");
System.out.println("Player numbers: " + userDigits[0] + " " + userDigits[1] + " " + userDigits[2] + " " + userDigits[3] + " " + userDigits[4] + " ");
System.out.println("Number of matching digits: " + sameNum);
if (sameNum == 5){
System.out.println("GRAND PRIZE WINNER - $5 MILLION!!");
}
if (sameNum == 4){
System.out.println("SUPER PRIZE WINNER - $500,000!!");
}
if (sameNum == 3){
System.out.println("GOOD PRIZE WINNER - $5,000!!");
}
if (sameNum == 2){
System.out.println("NICE PRIZE WINNER - $500!!");
}
if (sameNum == 1){
System.out.println("WINNER - $5!!");
}
if (sameNum ==0){
System.out.println("No matching numbers - better luck next time");
}
}
public static int generateNumbers(int [] lotteryNumbers){
Random randNum = new Random();
lotteryNumbers[0] = randNum.nextInt(10);
lotteryNumbers[1] = randNum.nextInt(10);
lotteryNumbers[2] = randNum.nextInt(10);
lotteryNumbers[3] = randNum.nextInt(10);
lotteryNumbers[4] = randNum.nextInt(10);
return lotteryNumbers[4];
}
public static int getUserData (int [] userDigits){
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter digit 1: ");
userDigits[0] = keyboard.nextInt();
System.out.print("Enter digit 2: ");
userDigits[1] = keyboard.nextInt();
System.out.print("Enter digit 3: ");
userDigits[2] = keyboard.nextInt();
System.out.print("Enter digit 4: ");
userDigits[3] = keyboard.nextInt();
System.out.print("Enter digit 5: ");
userDigits[4] = keyboard.nextInt();
return userDigits[4];
}
public static int compareArrays (int [] userDigits,
int [] lotteryNumbers){
int sameNum = 0;
for (int i = 0; i < 5; i++){
for (int x = 0; x < 5; x++){
if (lotteryNumbers[i] == userDigits[x]){
sameNum++;
}
return sameNum;
}
return sameNum;
}
return sameNum;
}
}
I am very new to arrays (and Java at that) so my problems are in my return/call statements. Please excuse my spacey coding style and any blatant mistakes that I have made. Any tips,advice, solutions, or if you notice anything wrong with what I have please let me know. Thanks!
Keep in mind that randNum.nextInt(10) will give you lottery numbers that range from 0 to 9. You can use a for loop to assign the random numbers to the lotteryNumbers array easier. Also, you should make sure that the random lottery numbers don't repeat.
In your compareArrays function, just put one return sameNum call after the outermost for loop, otherwise it won't update with the correct number of matching numbers. You need to give compareArrays() the correct parameters (userDigits and lotteryNumbers) and set sameNum equal to this result.
Several points you might find useful:
You might want to use NUM_DIGITS for the initiation of the arrays since that's the whole point of having named constant:
int[] userDigits = new int[NUM_DIGITS];
int[] lotteryNumbers = new int[NUM_DIGITS];
You can use Arrays.toString() to output arrays, e.g.:
System.out.println(Arrays.toString(lotteryNumbers));
Instead of multiple ifs use switch, plus do not repeat the whole print statement, only assign the part that differs:
String prize = "";
switch (sameNum) {
case 5: prize = "GRAND PRIZE WINNER - $5 MILLION!!";
break;
case 4: prize = "SUPER PRIZE WINNER - $500,000!!";
break;
case 3: prize = "GOOD PRIZE WINNER - $5,000!!";
break;
case 2: prize = "NICE PRIZE WINNER - $500!!";
break;
case 1: prize = "WINNER - $5!!";
break;
case 0: prize = "No matching numbers - better luck next time";
break;
default: prize = "Something weird happened";
}
System.out.println(prize);
I've updated your code with the changes you'll need.
Added NUM_DIGITS to your initializer
Closed your Scanner
Removed the early returns in your comparison method
Assigned the return value of the comparison method to sameNum
Set the return value of the generation and get methods to void
The other suggestions might be something you want to incorporate (such as the switch/case).
import java.util.Random;
import java.util.Scanner;
public class App {
public static void main(String[] args) {
int NUM_DIGITS = 5;
int[] userDigits = new int[NUM_DIGITS];
int[] lotteryNumbers = new int[NUM_DIGITS];
int sameNum;
generateNumbers(lotteryNumbers);
getUserData(userDigits);
sameNum = compareArrays(lotteryNumbers, userDigits);
System.out.println("Lottery numbers: " + lotteryNumbers[0] + " "
+ lotteryNumbers[1] + " " + lotteryNumbers[2] + " "
+ lotteryNumbers[3] + " " + lotteryNumbers[4] + " ");
System.out.println("Player numbers: " + userDigits[0] + " "
+ userDigits[1] + " " + userDigits[2] + " " + userDigits[3]
+ " " + userDigits[4] + " ");
System.out.println("Number of matching digits: " + sameNum);
if (sameNum == 5) {
System.out.println("GRAND PRIZE WINNER - $5 MILLION!!");
}
if (sameNum == 4) {
System.out.println("SUPER PRIZE WINNER - $500,000!!");
}
if (sameNum == 3) {
System.out.println("GOOD PRIZE WINNER - $5,000!!");
}
if (sameNum == 2) {
System.out.println("NICE PRIZE WINNER - $500!!");
}
if (sameNum == 1) {
System.out.println("WINNER - $5!!");
}
if (sameNum == 0) {
System.out.println("No matching numbers - better luck next time");
}
}
public static void generateNumbers(int[] lotteryNumbers) {
Random randNum = new Random();
lotteryNumbers[0] = randNum.nextInt(10);
lotteryNumbers[1] = randNum.nextInt(10);
lotteryNumbers[2] = randNum.nextInt(10);
lotteryNumbers[3] = randNum.nextInt(10);
lotteryNumbers[4] = randNum.nextInt(10);
return lotteryNumbers[4];
}
public static void getUserData(int[] userDigits) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter digit 1: ");
userDigits[0] = keyboard.nextInt();
System.out.print("Enter digit 2: ");
userDigits[1] = keyboard.nextInt();
System.out.print("Enter digit 3: ");
userDigits[2] = keyboard.nextInt();
System.out.print("Enter digit 4: ");
userDigits[3] = keyboard.nextInt();
System.out.print("Enter digit 5: ");
userDigits[4] = keyboard.nextInt();
keyboard.close();
return userDigits[4];
}
public static int compareArrays(int[] userDigits, int[] lotteryNumbers) {
int sameNum = 0;
for (int i = 0; i < 5; i++) {
for (int x = 0; x < 5; x++) {
if (lotteryNumbers[i] == userDigits[x]) {
sameNum++;
}
}
}
return sameNum;
}
}

Java- Issue with Return Data's accuracy inside of a loop

my previous questions have been taken care of. However I'm having an issue with the accuracy of data I'm pulling from another class. I'm aware that currentScore.finalScore only updates when used. However, I have finalScore = currentScore.finalScore. The print line I have shows that it updates, but by seemingly random numbers and even when this number causes the while statement to become false, it continues infinitely.
public static void main(String[] args) {
Scanner keyboard = new Scanner (System.in);
String buddyName;
String userChoice;
int maxScore = 10;
int minScore = 0;
System.out.print("Nurse: The Patient's first name is ");
buddyName = keyboard.nextLine();
System.out.print("You: Let's see, I should (C)hange " + buddyName + "'s bandages," +
"(G)ive " + buddyName + " pain medication, " +
"(A)dd antibiotics to " + buddyName + "'s I.V," +
" or (D)ischarge " + buddyName + " ");
userChoice = keyboard.nextLine();
Buddy score = new Buddy(userChoice);
BuddyScore currentScore = new BuddyScore(score);
int finalScore;
do {
System.out.print("You: Let's see, I should (C)hange " + buddyName + "'s"
+ " bandages, (G)ive " + buddyName + " pain medication, " +
"(A)dd antibiotics to " + buddyName + "'s I.V," +
" or (D)ischarge " + buddyName + " ");
userChoice = keyboard.nextLine();
score.setUserChoice(userChoice);
finalScore = currentScore.getFinalScore();
System.out.println(currentScore.getFinalScore());
}while (currentScore.getFinalScore() < maxScore && currentScore.getFinalScore() >= minScore);
System.out.println("Curren score: " + currentScore.getFinalScore());
}
}
Below is my class that handles the finalScore, and is the class I'm suspect of.
public class BuddyScore {
private Buddy buddyScore;
public int finalScore;
public BuddyScore(Buddy buddyScore){
this.buddyScore = buddyScore;
this.finalScore = finalScore;
}
public void setFinalScore(int finalScore){
this.finalScore = finalScore;
}
public int getFinalScore(){
return finalScore += buddyScore.getBuddyScore();
}
}
Below is the class that handles the buddyScore that the finalScore pulls from.
public class Buddy {
private String userChoice;
public int buddyScore;
public Buddy(String userChoice){
this.userChoice = userChoice;
}
public void setUserChoice(String userChoice){
this.userChoice = userChoice;
}
public String getUserChoice(){
return userChoice;
}
public void setBuddyScore(int buddyScore){
this.buddyScore = buddyScore;
}
public int getBuddyScore(){
switch (userChoice){
case "C":
buddyScore = 1;
break;
case "G":
buddyScore = -2;
break;
case "A":
buddyScore = 3;
break;
case "D":
buddyScore = 7;
break;
default:
buddyScore = 0;
break;
};
return buddyScore;
}
Thank you in advance.
The side effect of your getFinalScore method is that it alters the finalScore with the return value of getBuddyScore before finally returning. As a result, each call to getFinalScore will increase the value. In your loop, I see you calling that method four times; if you step through the code, you should see four completely different values within the same iteration.
I am guessing you want to do this instead?
return finalScore + buddy.getBuddyScore();
your while loop is wrong,
the operator should be && not ||
while (currentScore.getFinalScore() < maxScore && currentScore.getFinalScore() >= minScore);
causes this to always be true with a value of 0 and above.

Can someone tell me why this is not printing the correct score; it just exits out of the loop

/*The program's purpose is to test the user's ability to correctly identify the mascots of four
universities. Users will be awarded one point for each correct response, one point will be
deducted for an incorrect response, and zero points will be deducted if the user responds "don't know."
This means that your program will need to keep score as the user responds
to the prompts from the quiz and print that score to the console at the end of the program's
execution.*/
import java.util.Scanner;
public class MascotQuiz {
public static void main(String[] args) {
int score = 0;
String greeting =
"In this game, I ask you four questions about mascots for "
+"US collegiate sports teams." +
"\nYou get 1 point for each correct answer, "
+"0 points if you type don't know, "
+"and you lose a point for wrong answers.";
final String schoolOptions = "University of Michigan, "
+"University of Nebraska, "
+"University of Oklahoma, "
+"University of Wisconsin";
final String mascotOptions =
"Badgers, Cornhuskers, Sooners, Wolverines";
String prompt1 =
"\nType 1 and I'll give you the mascot and "
+"you give give the school. \n"
+"Type 2 and I'll give you the school and "
+"you give me the mascot. \n"
+"Type 3 and I'll quit.";
System.out.println( greeting );
/*************************************************************
* Do NOT delete, move, or change the lines of code above this:
* All of your code should appear between these comments.
************************************************************/
System.out.println( prompt1 );
Scanner userInput = new Scanner(System.in);
int x = userInput.nextInt();
do{
if (x==1){
System.out.println("Answer with one of: " + schoolOptions);
System.out.println("Badgers ? ");
String answer1 = userInput.nextLine();
if ("don't know".equalsIgnoreCase(answer1)){
score = score + 0;
}
else if ("University of Wisconsin".equalsIgnoreCase(answer1)){
score++;
}
else {
score--;
}
break;
}
else if (x==2){
System.out.println("Answer with one of: " + mascotOptions);
System.out.println("University of Wisconsin ? ");
String answer2 = userInput.nextLine();
if ("don't know".equalsIgnoreCase(answer2)){
score = score + 0;
}
else if ("Bagders".equalsIgnoreCase(answer2)){
score = score + 1;
}
else{
score = score - 1;
}
break;
}
System.out.print( "\nWant to play again? (type yes or no): " );
String play = userInput.next();
if (play.equalsIgnoreCase("no")){
x = 3;
}
else{
x = 0;
}
}
while (x!=3);
/*************************************************************
* Do NOT delete, move, or change this next line of code:
* This should be the last line of code in your program!
************************************************************/
System.out.println( "\nBye. Your score is " + score );
}
}
You use break operator in each if-else statment, because of your loop exited after:
if (x==1){
...
break;
}else if (x==2){
...
break;
}
Remove that, and your loop will work as you expect.
The problem is that you break out of your loop with break no matter what happens. The break instruction is executed in both cases (X==0 and X==1). SO remove this (if you want the question to try again) or replace it with a continue. That way the loop restarts and does not quit.
You could try this, Try this (not tested):
/*************************************************************
* Do NOT delete, move, or change the lines of code above this:
* All of your code should appear between these comments.
************************************************************/
System.out.println( prompt1 );
Scanner userInput = new Scanner(System.in);
int x = userInput.nextInt();
do{
if (x==1){
System.out.println("Answer with one of: " + schoolOptions);
System.out.println("Badgers ? ");
String answer1 = userInput.nextLine();
if ("don't know".equalsIgnoreCase(answer1)){
score = score + 0;
}
else if ("University of Wisconsin".equalsIgnoreCase(answer1)){
score++;
}
else {
score--;
}
continue; // <---- here
}
else if (x==2){
System.out.println("Answer with one of: " + mascotOptions);
System.out.println("University of Wisconsin ? ");
String answer2 = userInput.nextLine();
if ("don't know".equalsIgnoreCase(answer2)){
score = score + 0;
}
else if ("Bagders".equalsIgnoreCase(answer2)){
score = score + 1;
}
else{
score = score - 1;
}
continue; // <---- and here
}
System.out.print( "\nWant to play again? (type yes or no): " );
String play = userInput.next();
if (play.equalsIgnoreCase("no")){
x = 3;
}else{
x = 0; // this should be 1 or 2
}
}
while (x!=3);
/*************************************************************
* Do NOT delete, move, or change this next line of code:
* This should be the last line of code in your program!
************************************************************/

Categories

Resources