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
Re: PLEASE HELP ME!!!!!!!!
Code :
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
Re: PLEASE HELP ME!!!!!!!!
Hello Jane,
Welcome to the Java Programming Forums :D
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:
Code :
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();
}
}
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
Re: PLEASE HELP ME!!!!!!!!
Quote:
Originally Posted by
Freaky Chris
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 ;)