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
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.
我无法弄清楚程序无法运行的原因。第一部分代码是我创建的类,而第二部分是使用创建的类的程序。该程序的基本点是有一个抛硬币类,然后在程序中多次使用该类来玩游戏。游戏涉及将硬币翻转至季度镍币和硬币。如果硬币是头部,那么硬币的值将被添加到总数中,如果它是尾部,则不添加任何东西。当总数达到$ 1.00或更高时,游戏停止。如果总数正好是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!");
}
}
}
2 个解决方案
#1
1
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()方法进行比较......
#2
1
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";
}
.00 or greater the game stops. If the total is exactly
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.
我无法弄清楚程序无法运行的原因。第一部分代码是我创建的类,而第二部分是使用创建的类的程序。该程序的基本点是有一个抛硬币类,然后在程序中多次使用该类来玩游戏。游戏涉及将硬币翻转至季度镍币和硬币。如果硬币是头部,那么硬币的值将被添加到总数中,如果它是尾部,则不添加任何东西。当总数达到$ 1.00或更高时,游戏停止。如果总数正好是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!");
}
}
}
2 个解决方案
#1
1
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()方法进行比较......
#2
1
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";
}
.00 the user wins if not the user loses. I am having trouble figuring out why the progra