简体   繁体   English

可以编译但无法运行的Java程序

[英]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. 当总数达到$ 1.00或更高时,游戏停止。 If the total is exactly $1.00 the user wins if not the user loses. 如果总计恰好是$ 1.00,则用户获胜;否则,用户将输。

//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 使用String equals()方法比较字符串

 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... 如上所述,使用.equals()或.equalsIgnoreCase()方法比较字符串...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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