Web Hosting Talk







View Full Version : need help with java


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?

YUPAPA
12-22-2009, 05:54 AM
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;

public class Coin {
public static void main(String[] args) throws IOException {
String[] guess = { "head", "tail" };
String input = new String();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

for(int round=1; ; round++) {
System.out.println("Round " + round);
System.out.println("Guess head or tail");
input = br.readLine();

int rand = (int)(Math.random()*2);

if(guess[rand].equals(input)) {
System.out.println("You win 10 points");
} else {
System.out.println("Computer win 10 points");
}
}
}
}

Terryy
12-22-2009, 09:24 AM
Due to this being a school assignment, the assignment does not allow us to use any loops but must use math.random to generate random numbers...any idea??

foobic
12-22-2009, 06:18 PM
Due to this being a school assignment, the assignment does not allow us to use any loops but must use math.random to generate random numbers...any idea??
Even if you're not allowed to use loops (ugh, what sort of stupid assignment is this?) YUPAPA gave you all the information you need. Hint:
String[] guess = { "head", "tail" };

barry[CoffeeSprout]
12-28-2009, 05:31 AM
The only reason why loops may not because this assignment 2 and assignment 3 covers looping? ;)

Why do you bring your homework here?

mine070
12-28-2009, 06:21 AM
What Exactly is this Code for? I need more detail before I can help.