当我运行我的程序并询问你是否想要发挥机会或猜测我的if语句被跳过并直接到程序结束时。我不确定问题是什么,我在没有if语句的情况下进行了测试,它运行正常。 这是我的代码:
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);
} }