help please!!
i am trying to do a dicegame program and i have some promblems can someone help please. This is what i have so far
import java.util.Scanner;
import java.util.*;
public class Test {
public static void main( String[] args) {
for( int i = 1; i < 2; i++) {
int number = 1 + (int) (Math.random() * 6);
int numbera = 1 + (int) (Math.random() * 6);
System.out.println( "The total number of spots are "
+ number);
}
}
}
I want it to ask " do you want to roll again, y or n"
after it rolls a random number.
can someone give us a code to work from
ty
[647 byte] By [
999128a] at [2007-10-3 2:50:51]

Please use the [url> http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url].Do you have a specific question?
zadoka at 2007-7-14 20:39:42 >

Basically you want to do this:do {
playOneRound();
} while (userConfirmed("play again y/n?"));
This begs for at least two methods: the playOneRound() method and
the prompting method userConfirmed(String prompt).
You already (sort of) implemented the first method. The second one
can't be that difficult.
kind regards,
Jos
JosAHa at 2007-7-14 20:39:42 >

yes i want it to ask me " Do you want to roll again"then i should be able to type " y or n"y= roll againn= don't roll again and that keeps the highest roll
Check out the JOptionPane class in the API: http://java.sun.com/j2se/1.5.0/docs/api/JOptionPane.showConfirmDialog(null, "choose one", "choose one", JOptionPane.YES_NO_OPTION);
zadoka at 2007-7-14 20:39:42 >

> yes i want it to ask me
> " Do you want to roll again"
> then i should be able to type " y or n"
> y= roll again
> n= don't roll again and that keeps the highest roll
Well, go for it then: build that userConfirmed(String prompt) method.
If you're using Java 1.5 you can even use a Scanner for the actual
input process. Note that this method must accept three types of input:
1) a 'y'
2) a 'n'
3) something else.
In case of 3) it should repeat printing the prompt and read again.Otherwise
it should return true if a 'y' was typed or false when a 'n' was typed.
kind regards,
Jos
JosAHa at 2007-7-14 20:39:42 >

sorry to bother you again but i have tried and am not sure how to build the userConfirmed(String prompt). Can you help again please
public class Test
{
public static void main(String[] args) throws IOException
{
String s = "y";
while (s.equalsIgnoreCase("y"))
{
for (int i = 1; i < 2; i++)
{
int number = 1 + (int) (Math.random() * 6);
int numbera = 1 + (int) (Math.random() * 6);
System.out.println("The total number of spots are " + number);
}
System.out.println("You want to roll again, y or n");
Reader r = new InputStreamReader(System.in);
char c = (char) r.read();
s = new Character(c).toString();
}
}
}
> sorry to bother you again but i have tried and am not> sure how to build the userConfirmed(String prompt). > Can you help again pleasePost code and error message please.
sorry but what Hanneke just posted in doesn't work. i'm not sure why. im using textpad if that helps.Thanks for your help anyway
> sorry but what Hanneke just posted in doesn't work.1) It might still give you an idea about what to do2) You could check the error message and try to fix it
In the future please be more description about what does not work.
You need to add imports at the top:
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
zadoka at 2007-7-14 20:39:42 >

This is what i have tried
import java.util.Scanner;
import java.util.*;
public class Test {
public static void main( String[] args) {
for( int i = 1; i < 2; i++) {
int number = 1 + (int) (Math.random() * 6);
int numbera = 1 + (int) (Math.random() * 6);
System.out.println( "The total number of spots are "
+ number);
int a = number+numbera;
var question = prompt("Do you want to roll again?", "Type yes or no");
if (question == "yes"){
alert ("The number of spots are "+ a);
}
}
}
}
the error messages is
H:\java resit\Test.java:17: cannot find symbol
symbol : class var
location: class Test
var question = prompt("Do you want to roll again?", "Type yes or no");
^
H:\java resit\Test.java:17: cannot find symbol
symbol : method prompt(java.lang.String,java.lang.String)
location: class Test
var question = prompt("Do you want to roll again?", "Type yes or no");
^
H:\java resit\Test.java:20: cannot find symbol
symbol : method alert(java.lang.String)
location: class Test
alert ("The number of spots are "+ a);
^
3 errors
Tool completed with exit code 1
You are mixing Javascript and Java. They are not the same thing.
zadoka at 2007-7-14 20:39:42 >

Thanks for everones help.
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.*;
public class Test
{
public static void main(String[] args) throws IOException
{
String s = "y";
while (s.equalsIgnoreCase("y"))
{
for (int i = 1; i < 2; i++)
{
int number = 1 + (int) (Math.random() * 6);
int numbera = 1 + (int) (Math.random() * 6);
System.out.println("The total number of spots are " + number);
}
System.out.println("You want to roll again, y or n");
Reader r = new InputStreamReader(System.in);
char c = (char) r.read();
s = new Character(c).toString();
}
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Scanner;
public class Test
{
public static void main(String[] args) throws IOException
{
String s = "y";
while (s.equalsIgnoreCase("y"))
{
for (int i = 1; i < 2; i++)
{
int number = 1 + (int) (Math.random() * 6);
int numbera = 1 + (int) (Math.random() * 6);
System.out.println("The total number of spots are " + number);
}
System.out.println("You want to roll again, y or n");
Reader r = new InputStreamReader(System.in);
char c = (char) r.read();
s = new Character(c).toString();
}
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Scanner;
public class Test
{
public static void main(String[] args) throws IOException
{
String s = "y";
while (s.equalsIgnoreCase("y"))
{
for (int i = 1; i < 2; i++)
{
int number = 1 + (int) (Math.random() * 6);
int numbera = 1 + (int) (Math.random() * 6);
System.out.println("The total number of spots are " + number);
}
System.out.println("You want to roll again, y or n");
Reader r = new InputStreamReader(System.in);
char c = (char) r.read();
s = new Character(c).toString();
}
}
}
Ehh, sorry. I didn't saw the next page and thought my reply wasn't post yet...
its my fault i didnt describe it correctly sorry.
i should have said if i want to roll again and if "yes" then it should add the 1st and the 2nd roll together.
i know that you use an if statement but am not sure how to go around it.
sorry that im asking again but i really need help.
ty
> its my fault i didnt describe it correctly sorry.
> i should have said if i want to roll again and if
> "yes" then it should add the 1st and the 2nd roll
> together.
> i know that you use an if statement but am not sure
> how to go around it.
> sorry that im asking again but i really need help.
> ty
Store the value of the roll in a separate variable and add rolls to it each time.
how do i go about doing that zadok. im sorry to bother you again
Declare the total somewhere:int total=0;and add the number result each time:total += number; //shortcut for total = total + number
999128, I believe you should better look for someone to tutor you one on one. You seem still to have some insecucrities when dealing with code, and I think someone you can actually talk to you will be better suited to explain things to you than we are at this forum.
i do know that and i dont have time left.im very sorry again
and if the roll say "no", what then?
> i do know that and i dont have time left.> im very sorry againNo need to be sorry. This is just well-meant (if unsolicited) advice. It will serve you better in the long run.
it should give you the total of first roll1st roll Hanneke.you could say yes how many times you want but if the rolls total are > 11 then you lose
> it should give you the total of first roll1st roll
> Hanneke.
> you could say yes how many times you want but if the
> rolls total are > 11 then you lose
Basic rules about loops:
- If you don't want it to happen more than once (like creation and initialization of a sum or counter), don't put it into a loop.
- If you want it to happen repeatedly (like adding to a sum or increasing a counter), put it into the loop.
is there a code i can work from that
> is there a code i can work from thatCeciNEstPasUnProgrammeur, is right, you should look into a tutor because it seems you need step by step help and are having trouble with general coding issues.
Something like....?
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);
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();
if (s.equalsIgnoreCase("y"))
{
total += number;
total += numbera;
}
else if (s.equalsIgnoreCase("n"))
{
total = number;
}
System.out.println("The total number of spots are " + total);
}
System.out.println("YOU LOSE!!!");
}
}
im sorry to bother you again but i really need help i have tried lots of things and nothing is working.help please
Hanneke,
when you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting ]Formatting tips[/url] on the message entry page. It makes it much easier to read.
BTW: Spoon feeding the OP doesn't help him at all.
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);
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();
if (s.equalsIgnoreCase("y"))
{
total += number;
total += numbera;
}
else if (s.equalsIgnoreCase("n"))
{
total = number;
}
System.out.println("The total number of spots are " + total);
}
System.out.println("YOU LOSE!!!");
}
}
thank you Hanneke but when i press n it should automatically the computers turn. the rules of the computer are simple it should roll automatically after i say n and it should generate random numbers too. if it genrates anything > 8 it should stop and compare with the player to see who wins.
im sorry PhHein but i really need help cause i dont have time left.
But I think you are able to finish the code now? Because the rules are changing/extend all the time...
> im sorry PhHein but i really need help cause i dont
> have time left.
To be brutally honest: I think you fscked it up. So Hanneke's going to solve your homework for you because you had to put it away until the last moment, just to figure out you don't know enough to do it yourself? If you want someone to do stuff for you, at least go to www.rentacoder.com and offer to pay them.
So have you learned to start earlier next time? Giving you the code doesn't help you at all to improve your skills(apart from leeching).