Java Program that compiles but doesn't run -
I'm having trouble finding out why the program will not run. The first bit of code is this class that I have created while the second part is a program that uses the built-in class. The original point of the program is a coin toss class and then uses the class several times in the program to play a game. This game involves flipping the coin for Quarter Nickels and Dimes. If the coin is a head then the value of the coin is added to the total and if the tail is nothing then it is added. When the game closes for a total of $ 1.00 or more, if the user is actually $ 1.00, the user wins, the user does not lose.
// CONNESS class import java.util.Random; Public square coin {private string sideup; Public zero toss () {random flip = new random (); If (flip.nxit (2) == 0) {string up; Up = "tail"; Up = sideup; } And {string up; Up = "major"; Up = sideup; }} Public string getSideUp () {return sideUp; }}
Program that will not:
public class CoinToss {public static zero main (string [] args) {double total = 0.00; While (total & lt; = 1.00) {coin quarter = new coin (); Quarter.toss (); String Side 1 = Quarter .getSideUp (); If (side 1 == ("major")) {total + = 0.25; } And {total + = 0; } Coin dime = new coin (); Dime.toss (); String Side 2; Side2 = dime.getSideUp (); If (side 2 == ("major")) {total + = 0.10; } And {total + = 0; } Coin Nickel = new coin (); Nickel.toss (); String side 3; Side 3 = nickel.Tetidup (); If (side3 == ("heads")} {total + = .05; } And {total + = 0; }} If (total == 1.00) {System.out.printf ("balance:% $, 2f \ n", total); System.out.println ("You Win!"); } And {System.out.printf ("balance:% $, 2f \ n", total); System.out.println ("You Lost!"); }}
Change your coin category
public Class coin {private string sideup; Public zero toss () {random flip = new random (); If (flip.nxit (2) == 0) {sideUp = "tail"; } And {sideUp = "Head"; }} Public string getSideUp () {return sideUp; /}>
This works!
As suggested above, .equals (), or .equalsIgnoreCase () methods with strings ...
Comments
Post a Comment