View Full Version : help with java
mfsuk
08-16-2006, 06:01 PM
i am doing a program and i ahve some problems here.
this is what i have so far.
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).
i really need help in this cause i dont have time left.
Aradon
08-17-2006, 10:33 PM
Hi!
First I'd like to point to this:
http://www.codingforums.com/showthread.php?t=53446
Just as a reference, we aren't going to do your assignment for you. So could you tell me the problems you're having? Error Messages? Etc?
(and why do you have an open and close curly bracket at the end? )
mfsuk
08-18-2006, 02:24 PM
Hi!
First I'd like to point to this:
http://www.codingforums.com/showthread.php?t=53446
Just as a reference, we aren't going to do your assignment for you. So could you tell me the problems you're having? Error Messages? Etc?
(and why do you have an open and close curly bracket at the end? )
This is what i have
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!!!");
}
System.exit(0);{
}
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");
}
}
}
}
This is the error message
H:\java resit\Test.java:55: incompatible types
found : int
required: boolean
else if (total=totala){
^
1 error
Tool completed with exit code 1
Phill
08-18-2006, 02:32 PM
This is the error message
H:\java resit\Test.java:55: incompatible types
found : int
required: boolean
else if (total=totala){
^
1 error
Tool completed with exit code 1
That's probably one of the oldest compiler errors in the book. You're doing an = (variable assignment) instead of an == (equality check).
It's very easy to miss, especially if you're new to programming :)
mfsuk
08-18-2006, 02:38 PM
That's probably one of the oldest compiler errors in the book. You're doing an = (variable assignment) instead of an == (equality check).
It's very easy to miss, especially if you're new to programming :)
i have changed it to == it kind of works
this is what i have now
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");
}
}
}
}
and it gives me a number then the message ("You have lost Better luck next time");
can someone give me some advice in where im worng.
mfsuk
08-18-2006, 03:20 PM
does anyone know where ive gone wrong can someone help please.
ty
Beagle
08-18-2006, 04:41 PM
It seems like your program is running, what's wrong with it?
And please, wrap your code in ]code[ brackets:
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");
}
}
}
}
Aradon
08-20-2006, 03:55 AM
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.
Or so I've seen ;)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.