Results 1 to 2 of 2

Thread: need help with math.random

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Posts
    1

    need help with math.random

    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.

    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");
            }
         }
    }

    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

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: need help with math.random

    Use the java.util.Scanner class
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width