Gateux
Dec 22nd, 2009, 08:51 AM
Hiya I need to develop a program whereby the coin flip 3 times and has two players, one is a real person user while the other is just a computer.
The user have to enter Head or Tail and if user guess correctly, 10 points awarded to the user and if computer wins each round, 10 points each will be given.
This is what I have so far.
import java.util.Scanner;
public class Assignment2
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int point = 0;
int computer = 0;
//round 1:
//Let Coin Head = 1 and Coin Tail = 2
System.out.print("Round 1" + ":\n");
System.out.print("Guess 1 or 2");
int guess1 = sc.nextInt(); sc.nextLine();
//Computer's Coin Flip
int num1 = 0;
//generate a random number between 1 and 2
num1 = (int)((Math.random()*2)+1);
if (num1 == guess1){
point = point + 10;
} else {
computer = computer + 10;
}
if (num1 == guess1){
System.out.println("You win 10 points");
} else{
System.out.println("Computer win 10 points");
}
//round 2:
//Let Coin Head = 1 and Coin Tail = 2
System.out.print("Round 2 " + ":\n");
System.out.print("Guess ? 1 or 2 ? ");
int guess2= sc.nextInt(); sc.nextLine();
//Computer's Coin Flip
int num2 = 0;
//generate a random number between 1 and 2
num2 = (int)((Math.random()*2)+1);
if (num2 == guess2){
point = point + 10;
} else {
computer = computer + 10;
}
if (num2 == guess2){
System.out.println("You win 10 points");
} else{
System.out.println("Computer win 10 points");
}
//round 3:
//Let Coin Head = 1 and Coin Tail = 2
System.out.print("Round 3" + ":\n");
System.out.print("Guess ? 1 or 2 ? ");
int guess3 = sc.nextInt(); sc.nextLine();
//Computer's Coin Flip
int num3 = 0;
//generate a random number between 1 and 2
num3 = (int)((Math.random()*2)+1);
if (num3 == guess3){
point = point + 10;
} else {
computer = computer + 10;
}
if (num3 == guess3){
System.out.println("You win 10 points");
} else{
System.out.println("Computer win 10 points");
}
if(computer > point){
System.out.print("Computer has won the game\n");
System.out.print("Computer has received " + computer + " point");
} else {
System.out.print("You have won the game\n");
System.out.print("You have received " + point + " point");
}
}
}
However I want the user to be able to enter the String Head or Tail instead of numbers, but I do not know how to achieve this using Math.Random and also I must do this without any Loop.
Anyone can help me with this? Thanks
The user have to enter Head or Tail and if user guess correctly, 10 points awarded to the user and if computer wins each round, 10 points each will be given.
This is what I have so far.
import java.util.Scanner;
public class Assignment2
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int point = 0;
int computer = 0;
//round 1:
//Let Coin Head = 1 and Coin Tail = 2
System.out.print("Round 1" + ":\n");
System.out.print("Guess 1 or 2");
int guess1 = sc.nextInt(); sc.nextLine();
//Computer's Coin Flip
int num1 = 0;
//generate a random number between 1 and 2
num1 = (int)((Math.random()*2)+1);
if (num1 == guess1){
point = point + 10;
} else {
computer = computer + 10;
}
if (num1 == guess1){
System.out.println("You win 10 points");
} else{
System.out.println("Computer win 10 points");
}
//round 2:
//Let Coin Head = 1 and Coin Tail = 2
System.out.print("Round 2 " + ":\n");
System.out.print("Guess ? 1 or 2 ? ");
int guess2= sc.nextInt(); sc.nextLine();
//Computer's Coin Flip
int num2 = 0;
//generate a random number between 1 and 2
num2 = (int)((Math.random()*2)+1);
if (num2 == guess2){
point = point + 10;
} else {
computer = computer + 10;
}
if (num2 == guess2){
System.out.println("You win 10 points");
} else{
System.out.println("Computer win 10 points");
}
//round 3:
//Let Coin Head = 1 and Coin Tail = 2
System.out.print("Round 3" + ":\n");
System.out.print("Guess ? 1 or 2 ? ");
int guess3 = sc.nextInt(); sc.nextLine();
//Computer's Coin Flip
int num3 = 0;
//generate a random number between 1 and 2
num3 = (int)((Math.random()*2)+1);
if (num3 == guess3){
point = point + 10;
} else {
computer = computer + 10;
}
if (num3 == guess3){
System.out.println("You win 10 points");
} else{
System.out.println("Computer win 10 points");
}
if(computer > point){
System.out.print("Computer has won the game\n");
System.out.print("Computer has received " + computer + " point");
} else {
System.out.print("You have won the game\n");
System.out.print("You have received " + point + " point");
}
}
}
However I want the user to be able to enter the String Head or Tail instead of numbers, but I do not know how to achieve this using Math.Random and also I must do this without any Loop.
Anyone can help me with this? Thanks