When I run my program and ask if you want to play chance or guess my if statements for it are skipped over and it goes straight to the end of the program. I'm not sure what the problem is, I have tested it without the if statements and it works fine. Here's my code:
import java.util.Scanner;
public class coinflip23
{
public static void main (String args[])
{
System.out.println ("Welcome to coin flip game");
Scanner s = new Scanner(System.in);
System.out.print("Please enter the number of rounds you want to play");
int numofgames = Integer.parseInt(s.nextLine());
//numofgames
Scanner t = new Scanner(System.in);
System.out.print("Please enter the amount of money you want to play");
int moneyent = Integer.parseInt(t.nextLine());
//moneyent
Scanner f = new Scanner (System.in);
System.out.print("do wnat to play a game of chance or guess?");
String play = (f.nextLine());
//choice
int lose=moneyent;
int gain=0;
int result;
int totalmoney = moneyent;
/////////////////////////////////space break//////////////////////////////////////
if (play == "chance")
{
System.out.println(" a coin is fliped for everytime the coin lands on heads i'll double your profit");
System.out.println("but if the coin lands on tails you lose.");
for(int i=0; i<numofgames; i++)
{
if (Math.random()<0.5)
{
System.out.println("");
System.out.println ("Heads");
gain = totalmoney;
totalmoney = totalmoney*2;
System.out.println("");
System.out.println("you won $" +gain);
System.out.println("");
}
else if (Math.random()<1.0)
{
System.out.println("");
System.out.println ("Tails");
System.out.println("");
System.out.println("You lose $" +lose);
lose = totalmoney;
//System.exit(0);
}
}//forloop
}//end of if statment
/////////////////////////////////space break//////////////////////////////////////
else if (play == "guess")
{
System.out.println("To win the games guess if the coin will land on tails or heads");
System.out.println("If you guess correctly you win but if you guess wrong you lose");
//System.out.println("you will get three lives. If you lose all three lives you lose the game");
for(int i=0; i<numofgames; i++)
{
Scanner w = new Scanner (System.in);
System.out.print("What is your guess? Heads or Tails");
String ht = (w.nextLine());
String coin = "Tails";
if (Math.random()<0.5)
{
System.out.println("");
System.out.println ("Heads");
coin = "Heads";
}
else if (Math.random()<1.0)
{
coin = "Tails";
}
if (ht.equalsIgnoreCase(coin))
{
System.out.println("you are correct");
System.out.println("");
gain = totalmoney;
totalmoney = totalmoney*2;
System.out.println("you won $" +gain);
System.out.println("");
}
else
{
System.out.println("you are wrong");
System.out.println("");
System.out.println ("Tails");
lose = totalmoney;
System.out.println("");
System.out.println("You lose $" +lose);
}
}//forloop
}// end of if statment
/////////////////////////////////space break//////////////////////////////////////
result = gain-lose;
System.out.println("The game is done you won $"+gain + " you lost $"+lose);
System.out.println("");
System.out.println("You now have $" +result);
System.exit(0);
} }