Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-17-2007, 04:04 PM
Member
 
Join Date: Jul 2007
Posts: 26
romina is on a distinguished road
Generate a random number
I am doing a program and i ahve some problems here.
this is what i have so far.
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!!!"); { } } }
after i press "n" it should automatically be the computers turn.
this is are rules of the computer.
  1. Should generate a random number between 1 and 6 if it generates anything that =>8 it should stop and compare with my score.
  2. If it generates anything > 11 it losses (if i dont generate anything >11)
  3. Compares who wins at the end (player or computer).
Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 06:23 AM
Member
 
Join Date: Jul 2007
Posts: 40
trill is on a distinguished road
It seems like your program is running, what's wrong with it?

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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
random numbers without random class` carlos123 New To Java 4 01-17-2008 11:44 PM
how to generate dynamic pdf valery Advanced Java 1 08-06-2007 11:01 PM
how to generate xml with Dom4j leonard XML 1 08-06-2007 05:39 PM
How to generate random number in java fernando New To Java 1 08-01-2007 08:32 PM
Random Image on Refresh (FROM Folder not set number) QuinnMal Java Servlet 1 07-06-2007 08:27 PM


All times are GMT +3. The time now is 11:28 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org