Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to use scanner to use user\'s input \"yes\" or \"no\" to continue or

ID: 3645454 • Letter: I

Question

I'm trying to use scanner to use user's input "yes" or "no" to continue or abort the program...
What I am keep getting is regardless of what I type the program continue.
Can someone help me make the nessesary corrections?
Thanks!




import java.util.Scanner;

public class FortyOne{
public static void main(String [] args){
Scanner scanIn = new Scanner(System.in);
boolean Continue = true;
while (Continue)
{
Die die1, die2;

//Create a pair of dice
die1 = new Die();
die2 = new Die(6);

//Rolls dice
die1.roll();
die2.roll();
int rollCount;
rollCount = 0;
rollCount++;


//Initiate game
System.out.println("Let's Play 41!!!");
System.out.print("Roll #"+ rollCount+" ");
rollCount++;

// After the roll, prints out the value of die1 & 2.
System.out.print(die1.getUpValue() + " ");
System.out.print(die2.getUpValue() + " ");

//if you get doubles
if (die1.getUpValue() == die2.getUpValue())
System.out.print("YOU GOT DOUBLES!!! ");


//Gives turn total
int turnTotal = die1.getUpValue()+die2.getUpValue();

//prints out sum of die1 and die2
System.out.print("Your total is: " + turnTotal+ " ");

//Adds turnTotal to the next ?????
int playerTotal = 0;


//Limits the amount of rolls to 6.
if(rollCount >=7)
System.out.println("YOU LOSE!!");

//Tells player they lose if their roll amount or total equals 10,20,30,or 40
if(turnTotal == 10 || turnTotal == 20 || turnTotal == 30 || turnTotal == 40){
System.out.println(" " +" You lose...");}
else if(playerTotal == 10 || playerTotal == 20 || playerTotal == 30 || playerTotal == 40){
System.out.println(" " +" You lose...");}


else

do
{
System.out.print(" " + "Roll #"+ rollCount+" ");
rollCount++;
die1.roll();
die2.roll();
int pTotal = die1.getUpValue() + die2.getUpValue()+ playerTotal;
playerTotal = pTotal;
System.out.print(die1.getUpValue() + " ");
System.out.print(die2.getUpValue() + " ");
System.out.print("Your total is: " + (playerTotal)+ " ");


}


while (rollCount<=6);
rollCount++;
if(playerTotal >= 41)
{
System.out.println(" " + " You are the winner!");
}
else if(playerTotal <= 41)
{
System.out.println(" " + "You lose...");
}



System.out.print(" " + "Do You Want to Continue?(Type Yes or No): ");

String Continue1 = scanIn.nextLine();


if(Continue1=="yes")
{
return;
}
else if(Continue1=="no")
{
Continue = false;

}
}
}

}


Explanation / Answer

change these lines
if(Continue1=="yes")
{
return;
}
else if(Continue1=="no")
{
Continue = false;
}

TO

if ( Continue1.equalsIgnoreCase("yes")){                       // u can use equals also .
                  // equalsIgnoreCase ignores the case of characher .
              Continue = true;                /*if yes then loop again,you can use continue keyword to go to next iteration,*/
              /* continue ;                    you can also use this. */
}
else {
              Continue = false;     // if no ,negate the loop condition ,or simply break from loop.
             /* break;            you can also use this. */
}


The reason we can can't comapare two different string objects ( although there content may be same ) by == operator ,because == checks for references pointing to same objects.
for example :

s1 = "I love my country";
s2 = "I love my country";
s3 = s2;
now conditions :

s1 == s2              //false
s1.equals(s2)         // true
s2 == s3             //true.