Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Program to write a Java program to take two names and randomly generate winner

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Program to write a Java program to take two names and randomly generate winner

    I have a programming final and i desperately need some help.

    My Program--
    I want to write a program that tells the user to enter two names,(for example basketball team names). The program then has to generate two random numbers and assign the number to each name. A second method must compare the two numbers and tell the user which name is the winner

    For example- user enters Lakers and Magic, the program generates 2 numbers(90,98) then assigns one number to Lakers and another number to Magic and tell the user which team wins.

    Thank you to anyone that tries this, really appreciate it

    PS- if the numbers are equal the program should return "Tie"

    THANK YOU


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: PLEASE HELP ME!!!!!!!!

    import java.lang.Math
     
    public class Jane{
         public static void main(String[] args){
              Scanner sin = new Scanner(System.in);
     
              System.out.print("Please enter a name : ");
              String name1 = sin.next();
              System.out.print("And another: ");
              String name2 = sin.next();
     
              int a = (int)Math.random()*100;
              int b = (int)Math.random()*100;
              if( a < b ) System.out.println("Team 2 wins!");
              else if(a > b ) System.out.println("Team 1 wins!");
              else System.out.println("Tie!");
         }
    }

    I'm sure you get the idea from that and can split it into any methods you need etc.

    Regards,
    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: PLEASE HELP ME!!!!!!!!

    Hello Jane,

    Welcome to the Java Programming Forums

    Good example there Chris although you didn't import the Scanner class and the result is always Tie because int a & b returns 0. Just a slight error with the Math.random method.

    Seeing as this is my first post of the day I'll help you fix this and add the other method:

    import java.lang.Math;
    import java.util.Scanner;
     
    public class Jane{
     
        /**
         * JavaProgrammingForums.com
         */
     
        public static String name1, name2;
     
        public static void compare(){
     
            int a = (int)(Math.random()*100);
            int b = (int)(Math.random()*100);
     
            if( a < b ) System.out.println(name2 + " wins!");
            else if(a > b ) System.out.println(name1 + " wins!");
            else System.out.println(name1 + " & " + name2 + " Tie!");
     
        }
     
         public static void main(String[] args){
     
             Jane j = new Jane();
             Scanner sin = new Scanner(System.in);
     
              System.out.print("Please enter a name : ");
              name1 = sin.next();
              System.out.print("And another: ");
              name2 = sin.next();
     
              j.compare();
     
         }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: PLEASE HELP ME!!!!!!!!

    Hehe thanks JavaPF. Thats not an error with the Math.random() at all, thats me missing brakets out. Also was just typing it in the reply box...i'm use to just have eclipse virtually manage imports for me and it was first thing in the morning too, I wasn't thinking straight and I'm a Senile Half-Wit xD

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: PLEASE HELP ME!!!!!!!!

    Quote Originally Posted by Freaky Chris View Post
    Hehe thanks JavaPF. Thats not an error with the Math.random() at all, thats me missing brakets out. Also was just typing it in the reply box...i'm use to just have eclipse virtually manage imports for me and it was first thing in the morning too, I wasn't thinking straight and I'm a Senile Half-Wit xD

    Chris
    Yeah OK you was missing brackets, thats what I ment by a slight error
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers