Terryy
12-22-2009, 05:26 AM
Source Code:
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");
}
}
}
I need to have the player enter the word Head or Tail instead of just 1 or 2, any idea anyone? how do i achieve this?
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");
}
}
}
I need to have the player enter the word Head or Tail instead of just 1 or 2, any idea anyone? how do i achieve this?
