I'm getting this error when trying to set the value of an int to a specific user input with the scanner. here's my code:
package coinflip; import java.util.Scanner; public class CoinFlip { public int userinput; //----------------------------------------------------------------- // Creates first coin object, and flips it. //----------------------------------------------------------------- public void main (String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter the desired number of flips."); int userinput = scan.nextInt(); } { Coin c1 = new Coin(); c1.flip(); c1. getvalue(); System.out.println(c1.getvalue()); Coin c2 = new Coin(); c2.getvalue(); System.out.println(c2.getvalue()); if (c1.getvalue() > c2.getvalue()) { System.out.println ("Tails! Player 2 Wins!"); } else if (c1.getvalue() < c2.getvalue()) { System.out.println("Heads! Player 1 Wins"); } else if (c1.getvalue() == c2.getvalue()) { System.out.println("Tie!"); } } }
//******************************************************************** // Coin.java Author: Lewis/Loftus // // Represents a coin with two sides that can be flipped. //******************************************************************** package coinflip; public class Coin { private final int HEADS = 0; private final int TAILS = 1; private int face; private int p1wins = 0; private int p2wins =0; private int ties = 0; //----------------------------------------------------------------- // Sets up the coin by flipping it initially. //----------------------------------------------------------------- public Coin () { flip(); } public int p1; //increment p1win each time player 1 wins, must be tied into flip { p1wins++; } public int p2; //incremnt p2win each time players 1 wins, must be tied into flip { p2wins++; } public int tie; { ties++; } public int getvalue () { return face; } //----------------------------------------------------------------- // Flips the coin by randomly choosing a face value. //----------------------------------------------------------------- public void flip () { face = (int) (Math.random() * 2); } { System.out.println("Face is: "+ face); } }
I also need advice as for how to tell the program to repeat the coinflip a certain number of times. Would this be through a conditional statement? I think it is, but I can;t find one that seems to work.
also posted on java-forums.com