简体   繁体   中英

Java Program that compiles but doesn't run

I am having trouble figuring out why the program won't run. The first bit of code is the class i created while the second part is the program that uses the created class. The basic point of the program is to have a coin toss class and then use the class multiple times in the program to play a game. The game involves flipping the coin for quarters nickels and dimes. If the coin is heads then the value of the coin is added to the total and if it is tails nothing is added. When the total reaches a $1.00 or greater the game stops. If the total is exactly $1.00 the user wins if not the user loses.

//CoinToss Class
import java.util.Random;
public class Coin
{
   private String sideUp;

   public void toss()
   {
      Random flip = new Random();
      if (flip.nextInt(2) == 0)
      {
         String Up;
         Up = "Tails";
         Up = sideUp;
      }
      else
      {
         String Up;
         Up = "Heads";
         Up = sideUp;
      }
     }

   public String getSideUp()
   {
      return sideUp;
   }
}

The Program that won't run:

public class CoinToss
{
   public static void main(String[] args)
   {
     double total = 0.00;
     while (total <= 1.00)
     {
         Coin quarter = new Coin();
         quarter.toss();
         String side1 = quarter.getSideUp();
         if (side1 == ("Heads"))
         {
        total += 0.25;
         }
         else
         {
            total += 0;
         }
         Coin dime = new Coin();
         dime.toss();
         String side2;
         side2 = dime.getSideUp();
         if (side2 == ("Heads"))
         {
            total += 0.10;
         }
         else 
         {
         total += 0;
         }
         Coin nickel = new Coin();
         nickel.toss();
         String side3;
         side3 = nickel.getSideUp();
         if (side3 == ("Heads"))
         {
            total += .05;
         }
         else
         {
            total += 0;
         }

         }
         if (total == 1.00)
         {
         System.out.printf("Balance: %$,.2f\n", total);
         System.out.println("You win!");
         }
         else 
         {
         System.out.printf("Balance: %$,.2f\n", total);
         System.out.println("You lose!");
         } 
      }
     }

use String equals() method to compare strings

 Instead of side1 == ("Heads")

use

side1.equals("Heads") and side2.equals("Heads")

and correct as below:

  if (flip.nextInt(2) == 0)
  {        
     sideUp = "Tails";
  }
  else
  {        
     sideUp = "Heads";
  }

Change your Coin class

public class Coin
{
private String sideUp;

public void toss()
{
    Random flip = new Random();
    if (flip.nextInt(2) == 0)
    {
        sideUp = "Tails";
    }
    else
    {
        sideUp = "Heads";
    }
}

public String getSideUp()
{
    return sideUp;
}
}

That works!!

As suggested above, Compare Strings with .equals(), or .equalsIgnoreCase() methods...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM