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

Thread: In Desperate Need Of Help! TEACHER WONT HELP ME

1. In Desperate Need Of Help! TEACHER WONT HELP ME

The computer should randomly select 1 of 4 words: (HEARTS, DIAMONDS, CLUBS, SPADES)
1000 times.
Print out the statistics of the percentage of time each word was chosen.

import java.util.Scanner;
import java.util.Random;
public class QuestionFive {
public static void main(String[] args) {
int result;
int hnum =0, cnum, dnum, snum;
Scanner scan = new Scanner(System.in);

for (int i = 0; i < 1000; i += 1) {

// generate a random numbr between 0 - 3
Random r = new Random();
result = r.nextInt(4);
System.out.println(result);

// if the random number is zero assume it is hearts.
if (result == 1) {
System.out.println(i + " You drew Hearts");
hnum = hnum + 1;
}
// else if the random number is 1 assume it is clubs
else if (result = "CLUBS")
cnum = cnum + 1;
else if (result = "DIAMONDS")
dnum = dnum + 1;
snum = snum + 1*/;
}
System.out.println("The chances of getting HEARTS is " + (hnum / 1000.00) * 100 + "%");
System.out.println("The chances of getting CLUBS is " + (cnum / 1000) * 100 + "%");
System.out.println("The chances of getting DIAMONDS is " + (dnum / 1000) * 100 + "%");
System.out.println("The chances of getting SPADES is " + (snum / 1000) * 100 + "%")*/;
}
}