Results 1 to 2 of 2
Thread: Generate a random number
- 07-17-2007, 03:04 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 26
- Rep Power
- 0
Generate a random number
I am doing a program and i ahve some problems here.
this is what i have so far.
after i press "n" it should automatically be the computers turn.Java Code:import java.util.Scanner; import java.util.*; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; public class Test { public static void main(String[] args) throws IOException { int total = 0; int number = 0; int numbera = 0; while (total < 12) { for (int i = 1; i < 2; i++) { number = 1 + (int) (Math.random() * 6); } System.out.println("You want to roll again, y or n"); Reader r = new InputStreamReader(System.in); char c = (char) r.read(); String s = new Character(c).toString(); if (s.equalsIgnoreCase("y")) { total += number; } else if (s.equalsIgnoreCase("n")) { total = number; } System.out.println("The total number of spots are " + total); } System.out.println("YOU LOSE!!!"); { } } }
this is are rules of the computer.
- Should generate a random number between 1 and 6 if it generates anything that =>8 it should stop and compare with my score.
- If it generates anything > 11 it losses (if i dont generate anything >11)
- Compares who wins at the end (player or computer).
Thanks
- 08-07-2007, 05:23 AM #2
Member
- Join Date
- Jul 2007
- Posts
- 40
- Rep Power
- 0
It seems like your program is running, what's wrong with it?
Java Code:import java.util.Scanner; import java.util.*; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; public class Test { public static void main(String[] args) throws IOException { int total = 0; int totala = 0; int number = 0; int numbera = 0; while (total < 12) { for (int i = 1; i < 2; i++) { number = 1 + (int) (Math.random() * 6); numbera = 1 + (int) (Math.random() * 6); } System.out.println("You want to roll again, y or n"); Reader r = new InputStreamReader(System.in); char c = (char) r.read(); String s = new Character(c).toString(); total += number; totala += numbera; if (s.equalsIgnoreCase("y")) { System.out.println ("The total number of spots are " + total); } else if(total > 10) { System.out.println("YOU LOSE!!!"); } if (s.equalsIgnoreCase("n")) { System.out.println("The total the computer has generated are " + totala); } else if (total > totala) { System.out.println("Well done you Win "); } else if (totala > total) { System.out.println("You have lost Better luck next time"); } else if (total==totala) { System.out.println("Game Is Drawn"); } } } }
If you're unsure if it's working or not, put in some System.out.println(); printing out variable names as they come up to make sure it works.
Basic Debugging. I know in some IDE's you can even step through a program and it will show you what variables are currently in use or have been assigned.
Greetings.
Similar Threads
-
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 10:44 PM -
how to generate dynamic pdf
By valery in forum Advanced JavaReplies: 1Last Post: 08-06-2007, 10:01 PM -
how to generate xml with Dom4j
By leonard in forum XMLReplies: 1Last Post: 08-06-2007, 04:39 PM -
How to generate random number in java
By fernando in forum New To JavaReplies: 1Last Post: 08-01-2007, 07:32 PM -
Random Image on Refresh (FROM Folder not set number)
By QuinnMal in forum Java ServletReplies: 1Last Post: 07-06-2007, 07:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks