1. Member
Join Date
Sep 2008
Posts
29
Rep Power
0

## Help!

play three games of this and the compiler somehow subtracts Winner and Winner2, making them 1 game off or so...

Java Code:
```import java.util.Random;
import java.util.Scanner;

public class DiceGame
{
public static void main( String args[] )
{

Random randomNumbers = new Random();
int face;
int face2;
int face3;
int face4;
int sum;
int sum2;
int sum3;
int sum4;
@SuppressWarnings("unused")
int sum5;
int NumberRoll;
int Go;
int Games;
int Winner;
int Winner2;
int Ties;
String Name;
String Name2;

Games = 1;
Winner = 0;
Winner2 = 0;
Ties = 0;

for ( int counter = 1; counter <= 1; counter++ )
{
System.out.printf( "Welcome to a game of Big Dice!\nHave Fun!\n\n" );

Scanner input = new Scanner( System.in );

System.out.printf( "Player1 please enter your first name:\n" );
Name = input.next();

System.out.printf( "Please enter your first name Player2:\n" );
Name2 = input.next();

System.out.printf( "\nWhats the highest number you want your dice to roll(or 926620 for rules):\n" );
NumberRoll = input.nextInt();

if ( NumberRoll == 926620 )
System.out.println( "Rules List!\n\n1. Have Fun\n2. Show your friends this game\n" +
"3. Listen to Linkin Park\n4. Win and Have Fun!\n" );

if ( NumberRoll == 444444 )
System.out.println( "Merry Christmas!\n\nI LOVE YOU!\n" );

System.out.printf( "\n%s's Rolls\n", Name );

face = 1 + randomNumbers.nextInt( NumberRoll );

System.out.printf( "%d\n", face );

face2 = 1 + randomNumbers.nextInt( NumberRoll );

System.out.printf( "%d\n", face2 );

System.out.printf( "%s's Rolls\n", Name2 );

face3 = 1 + randomNumbers.nextInt( NumberRoll );

System.out.printf( "%d\n", face3 );

face4 = 1 + randomNumbers.nextInt( NumberRoll );

System.out.printf( "%d\n\n", face4 );

sum = face + face2;
sum2 = face3 + face4;
sum3 = Winner - Winner2;
sum4 = Winner2 - Winner;
sum5 = Winner + Winner2;

if ( sum2 == sum )
{
Ties = Ties + 1;
}
if ( sum > sum2 )
{
Winner = Winner + 1;
}

if ( sum2 > sum )
{
Winner2 = Winner2 + 1;
}

if ( sum == 24 )
sum = face + face2 * 2;
if ( sum == 24 )
System.out.printf( "%s, has rolled a lucky number, now %d multiplies by two!\n", Name, sum );
if ( sum == 62 )
sum = face2 + face2 * 2;
if ( sum == 62 )
System.out.printf( "%s, has rolled a lucky number, now %d multiplies by two!\n", Name, sum );
if ( sum2 == 17 )
sum2 = face3 + face4 * 2;
if ( sum2 == 17 )
System.out.printf( "%s, has rolled a lucky number, now %d multiplies by two!\n", Name2, sum2 );
if ( sum2 == 85 )
sum2 = face3 + face4 * 2;
if ( sum2 == 85 )
System.out.printf( "%s, has rolled a lucky number, now %d multiplies by two!\n", Name2, sum2 );

System.out.printf( "The sum for %s is %d\n", Name, sum );
System.out.printf( "The sum for %s is %d\n", Name2, sum2 );

if ( sum > sum2 )
System.out.printf( "%s has won the game!\n", Name );
if ( sum2 > sum )
System.out.printf( "%s has won the game!\n\n", Name2 );
if ( sum == sum2 )
System.out.println( "Tie!" );
if ( face == face2 )
System.out.printf( "%s has rolled the same two numbers, %d, and %d,\n The chances of doing that are %d to 1!\n", Name, face, face2, NumberRoll);
if ( face3 == face4 )
System.out.printf( "%s has rolled the same two numbers, %d, and %d,\n The chances of doing that are %d to 1!\n\n", Name2, face3, face4, NumberRoll );
if ( sum > sum2 )
System.out.printf( "Congradulations %s, you have won a game of Big Dice!\n\n", Name );
if ( sum < sum2 )
System.out.printf( "Congradulations %s, you have won a game of Big Dice!\n\n", Name2 );

System.out.printf( "Enter 1 if you would like to play again, enter 2 if you want to quit:\n" );
Go = input.nextInt ();

if ( Go == 4 )
System.out.println( "You are in luck, that is my lucky number! " );

if ( Go == 2 )
System.out.printf( "\nGoodebye %s and %s, it was enjoyable watching you two play together. ", Name, Name2 );

while ( Go != 2 )
{
System.out.printf( "\n\nWelcome back %s, and, %s!\nHave Fun!\n\n", Name, Name2 );

new Scanner( System.in );

System.out.printf( "Whats the highest number you want your dice to roll:\n" );
NumberRoll = input.nextInt();

System.out.printf( "\n%s's Rolls\n", Name );

face = 1 + randomNumbers.nextInt( NumberRoll );

System.out.printf( "%d\n", face );

face2 = 1 + randomNumbers.nextInt( NumberRoll );

System.out.printf( "%d\n", face2 );

System.out.printf( "%s's Rolls\n", Name2 );

face3 = 1 + randomNumbers.nextInt( NumberRoll );

System.out.printf( "%d\n", face3 );

face4 = 1 + randomNumbers.nextInt( NumberRoll );

System.out.printf( "%d\n\n", face4 );

sum = face + face2;
sum2 = face3 + face4;
sum3 = Winner - Winner2;
sum4 = Winner2 - Winner;
sum5 = Winner + Winner2;

if ( sum == sum2 )
{
Ties = Ties + 1;
}
if ( sum2 > sum )
{
Winner2 = Winner2 + 1;
}
if ( sum > sum2 )
{
Winner = Winner + 1;
}

if ( sum == 1 )
{
sum = face + face2 * 2;
}
if ( sum == 1 )
System.out.printf( "%s, has rolled a lucky number, now %d multiplies by two!\n", Name, sum );
if ( sum == 62 )
sum = face2 + face2 * 2;
if ( sum == 62 )
System.out.printf( "%s, has rolled a lucky number, now %d multiplies by two!\n", Name, sum );
if ( sum2 == 17 )
sum2 = face3 + face4 * 2;
if ( sum2 == 17 )
System.out.printf( "%s, has rolled a lucky number, now %d multiplies by two!\n", Name2, sum2 );
if ( sum2 == 85 )
sum2 = face3 + face4 * 2;
if ( sum2 == 85 )
System.out.printf( "%s, has rolled a lucky number, now %d multiplies by two!\n", Name2, sum2 );

System.out.printf( "The sum for %s is %d\n", Name, sum );
System.out.printf( "The sum for %s is %d\n", Name2, sum2 );

if ( sum > sum2 )
System.out.printf( "%s has won the game!\n", Name );
if ( sum2 > sum )
System.out.printf( "%s has won the game!\n\n", Name2 );
if ( sum == sum2 )
System.out.println( "Tie!" );
if ( face == face2 )
System.out.printf( "%s has rolled the same two numbers, %d, and %d,\n The chances of doing that are %d to 1!\n", Name, face, face2, NumberRoll);
if ( face3 == face4 )
System.out.printf( "%s has rolled the same two numbers, %d, and %d,\n The chances of doing that are %d to 1!\n\n", Name2, face3, face4, NumberRoll );
if ( sum > sum2 )
System.out.printf( "Congradulations %s, you have won a game of Big Dice!\n\n", Name );
if ( sum < sum2 )
System.out.printf( "Congradulations %s, you have won a game of Big Dice!\n\n", Name2 );

System.out.printf( "Enter 1 if you would like to play again, enter 2 if you want to quit:\n" );
Go = input.nextInt ();

Games = Games + 1;

if ( Go == 2 )
System.out.printf( "\nYou have played %s Games of Big Dice!", Games );
if ( Go == 2 )
System.out.printf( "\n\n%s has won %d games of Big Dice!\n", Name, Winner );
if ( Go == 2 )
System.out.printf( "%s has won %s games of Big Dice!\n", Name2, Winner2 );

if ( Go == 2 )
if ( Ties < 1 )
System.out.printf( "%s has Tied %s %d times!\n", Name, Name2, Ties );
if ( Ties == 1 )
System.out.printf( "%s has Tied %s %d time!\n", Name, Name2, Ties );

if ( Go == 2 )
System.out.printf( "" );

if ( Go == 2 )
System.out.printf( "" );
if ( Winner > Winner2 )
System.out.printf( "\n%s has won %d more games than %s!\n ", Name, sum3, Name2 );
if ( Go == 2 )
System.out.println( "" );
if ( Winner2 > Winner )
System.out.printf( "\n%s has won %d more games than %s!\n ", Name2, sum4, Name );
if ( Go == 2 )
System.out.println( "" );
if ( Winner == Winner2 )
System.out.printf( "Tie!" );

}

if ( NumberRoll != 0 )
{
System.out.println( "" );
}
else

if ( counter % 5 == 0 )
System.out.println();
}
}
}```

[Compiler]
Welcome to a game of Big Dice!
Have Fun!

Jerry
CPU

Whats the highest number you want your dice to roll(or 926620 for rules):
50

Jerry's Rolls
4
25
CPU's Rolls
9
4

The sum for Jerry is 29
The sum for CPU is 13
Jerry has won the game!
Congradulations Jerry, you have won a game of Big Dice!

Enter 1 if you would like to play again, enter 2 if you want to quit:
1

Welcome back Jerry, and, CPU!
Have Fun!

Whats the highest number you want your dice to roll:
50

Jerry's Rolls
36
19
CPU's Rolls
14
32

The sum for Jerry is 55
The sum for CPU is 46
Jerry has won the game!
Congradulations Jerry, you have won a game of Big Dice!

Enter 1 if you would like to play again, enter 2 if you want to quit:
1

Jerry has won 1 more games than CPU!

Welcome back Jerry, and, CPU!
Have Fun!

Whats the highest number you want your dice to roll:
50

Jerry's Rolls
12
2
CPU's Rolls
47
6

The sum for Jerry is 14
The sum for CPU is 53
CPU has won the game!

Congradulations CPU, you have won a game of Big Dice!

Enter 1 if you would like to play again, enter 2 if you want to quit:
2

You have played 3 Games of Big Dice!

Jerry has won 2 games of Big Dice!
CPU has won 1 games of Big Dice!
Jerry has Tied CPU 0 times!

Jerry has won 2 more games than CPU!
[Compiler]

2. Member
Join Date
Sep 2008
Posts
29
Rep Power
0
the end of the compiler, when it says that 1 - 2 = 2
thats the problem.

3. Since you don't have specify the code where it happens, sorry I can't say anything. Because your code is too long and lazy to run it and see. Can you at least send the code segment that cause to have this result?

4. Member
Join Date
Sep 2008
Posts
29
Rep Power
0
the stuff in bold is what you should look at...

Java Code:
```import java.util.Random;
import java.util.Scanner;

public class DiceGame
{
public static void main( String args[] )
{

Random randomNumbers = new Random();
int face;
int face2;
int face3;
int face4;
int sum;
int sum2;
i[B]nt sum3;
int sum4;[/B]
@SuppressWarnings("unused")
[B]int sum5;[/B]
int NumberRoll;
int Go;
int Games;
[B]int Winner;
int Winner2;[/B]
int Ties;
String Name;
String Name2;

Games = 1;
Winner = 0;
Winner2 = 0;
Ties = 0;

for ( int counter = 1; counter <= 1; counter++ )
{
System.out.printf( "Welcome to a game of Big Dice!\nHave Fun!\n\n" );

Scanner input = new Scanner( System.in );

System.out.printf( "Player1 please enter your first name:\n" );
Name = input.next();

System.out.printf( "Please enter your first name Player2:\n" );
Name2 = input.next();

System.out.printf( "\nWhats the highest number you want your dice to roll(or 926620 for rules):\n" );
NumberRoll = input.nextInt();

if ( NumberRoll == 926620 )
System.out.println( "Rules List!\n\n1. Have Fun\n2. Show your friends this game\n" +
"3. Listen to Linkin Park\n4. Win and Have Fun!\n" );

if ( NumberRoll == 444444 )
System.out.println( "Merry Christmas!\n\nI LOVE YOU!\n" );

System.out.printf( "\n%s's Rolls\n", Name );

face = 1 + randomNumbers.nextInt( NumberRoll );

System.out.printf( "%d\n", face );

face2 = 1 + randomNumbers.nextInt( NumberRoll );

System.out.printf( "%d\n", face2 );

System.out.printf( "%s's Rolls\n", Name2 );

face3 = 1 + randomNumbers.nextInt( NumberRoll );

System.out.printf( "%d\n", face3 );

face4 = 1 + randomNumbers.nextInt( NumberRoll );

System.out.printf( "%d\n\n", face4 );

sum = face + face2;
sum2 = face3 + face4;
[B]sum3 = Winner - Winner2;
sum4 = Winner2 - Winner;[/B]
sum5 = Winner + Winner2;

if ( sum2 == sum )
{
Ties = Ties + 1;
}
if ( sum > sum2 )
{
Winner = Winner + 1;
}

if ( sum2 > sum )
{
Winner2 = Winner2 + 1;
}

if ( sum == 24 )
sum = face + face2 * 2;
if ( sum == 24 )
System.out.printf( "%s, has rolled a lucky number, now %d multiplies by two!\n", Name, sum );
if ( sum == 62 )
sum = face2 + face2 * 2;
if ( sum == 62 )
System.out.printf( "%s, has rolled a lucky number, now %d multiplies by two!\n", Name, sum );
if ( sum2 == 17 )
sum2 = face3 + face4 * 2;
if ( sum2 == 17 )
System.out.printf( "%s, has rolled a lucky number, now %d multiplies by two!\n", Name2, sum2 );
if ( sum2 == 85 )
sum2 = face3 + face4 * 2;
if ( sum2 == 85 )
System.out.printf( "%s, has rolled a lucky number, now %d multiplies by two!\n", Name2, sum2 );

System.out.printf( "The sum for %s is %d\n", Name, sum );
System.out.printf( "The sum for %s is %d\n", Name2, sum2 );

if ( sum > sum2 )
System.out.printf( "%s has won the game!\n", Name );
if ( sum2 > sum )
System.out.printf( "%s has won the game!\n\n", Name2 );
if ( sum == sum2 )
System.out.println( "Tie!" );
if ( face == face2 )
System.out.printf( "%s has rolled the same two numbers, %d, and %d,\n The chances of doing that are %d to 1!\n", Name, face, face2, NumberRoll);
if ( face3 == face4 )
System.out.printf( "%s has rolled the same two numbers, %d, and %d,\n The chances of doing that are %d to 1!\n\n", Name2, face3, face4, NumberRoll );
if ( sum > sum2 )
System.out.printf( "Congradulations %s, you have won a game of Big Dice!\n\n", Name );
if ( sum < sum2 )
System.out.printf( "Congradulations %s, you have won a game of Big Dice!\n\n", Name2 );

System.out.printf( "Enter 1 if you would like to play again, enter 2 if you want to quit:\n" );
Go = input.nextInt ();

if ( Go == 4 )
System.out.println( "You are in luck, that is my lucky number! " );

if ( Go == 2 )
System.out.printf( "\nGoodebye %s and %s, it was enjoyable watching you two play together. ", Name, Name2 );

while ( Go != 2 )
{
System.out.printf( "\n\nWelcome back %s, and, %s!\nHave Fun!\n\n", Name, Name2 );

new Scanner( System.in );

System.out.printf( "Whats the highest number you want your dice to roll:\n" );
NumberRoll = input.nextInt();

System.out.printf( "\n%s's Rolls\n", Name );

face = 1 + randomNumbers.nextInt( NumberRoll );

System.out.printf( "%d\n", face );

face2 = 1 + randomNumbers.nextInt( NumberRoll );

System.out.printf( "%d\n", face2 );

System.out.printf( "%s's Rolls\n", Name2 );

face3 = 1 + randomNumbers.nextInt( NumberRoll );

System.out.printf( "%d\n", face3 );

face4 = 1 + randomNumbers.nextInt( NumberRoll );

System.out.printf( "%d\n\n", face4 );

sum = face + face2;
sum2 = face3 + face4;
[B]sum3 = Winner - Winner2;
sum4 = Winner2 - Winner;[/B]
sum5 = Winner + Winner2;

if ( sum == sum2 )
{
Ties = Ties + 1;
}
if ( sum2 > sum )
{
Winner2 = Winner2 + 1;
}
if ( sum > sum2 )
{
Winner = Winner + 1;
}

if ( sum == 1 )
{
sum = face + face2 * 2;
}
if ( sum == 1 )
System.out.printf( "%s, has rolled a lucky number, now %d multiplies by two!\n", Name, sum );
if ( sum == 62 )
sum = face2 + face2 * 2;
if ( sum == 62 )
System.out.printf( "%s, has rolled a lucky number, now %d multiplies by two!\n", Name, sum );
if ( sum2 == 17 )
sum2 = face3 + face4 * 2;
if ( sum2 == 17 )
System.out.printf( "%s, has rolled a lucky number, now %d multiplies by two!\n", Name2, sum2 );
if ( sum2 == 85 )
sum2 = face3 + face4 * 2;
if ( sum2 == 85 )
System.out.printf( "%s, has rolled a lucky number, now %d multiplies by two!\n", Name2, sum2 );

System.out.printf( "The sum for %s is %d\n", Name, sum );
System.out.printf( "The sum for %s is %d\n", Name2, sum2 );

if ( sum > sum2 )
System.out.printf( "%s has won the game!\n", Name );
if ( sum2 > sum )
System.out.printf( "%s has won the game!\n\n", Name2 );
if ( sum == sum2 )
System.out.println( "Tie!" );
if ( face == face2 )
System.out.printf( "%s has rolled the same two numbers, %d, and %d,\n The chances of doing that are %d to 1!\n", Name, face, face2, NumberRoll);
if ( face3 == face4 )
System.out.printf( "%s has rolled the same two numbers, %d, and %d,\n The chances of doing that are %d to 1!\n\n", Name2, face3, face4, NumberRoll );
if ( sum > sum2 )
System.out.printf( "Congradulations %s, you have won a game of Big Dice!\n\n", Name );
if ( sum < sum2 )
System.out.printf( "Congradulations %s, you have won a game of Big Dice!\n\n", Name2 );

System.out.printf( "Enter 1 if you would like to play again, enter 2 if you want to quit:\n" );
Go = input.nextInt ();

Games = Games + 1;

if ( Go == 2 )
System.out.printf( "\nYou have played %s Games of Big Dice!", Games );
if ( Go == 2 )
System.out.printf( "\n\n%s has won %d games of Big Dice!\n", Name, Winner );
if ( Go == 2 )
System.out.printf( "%s has won %s games of Big Dice!\n", Name2, Winner2 );

if ( Go == 2 )
if ( Ties < 1 )
System.out.printf( "%s has Tied %s %d times!\n", Name, Name2, Ties );
if ( Ties == 1 )
System.out.printf( "%s has Tied %s %d time!\n", Name, Name2, Ties );

if ( Go == 2 )
System.out.printf( "" );

if ( Go == 2 )
System.out.printf( "" );
if ( Winner > Winner2 )
[B]	System.out.printf( "\n%s has won %d more games than %s!\n ", Name, sum3, Name2 );[/B]
if ( Go == 2 )
System.out.println( "" );
if ( Winner2 > Winner )
[B]System.out.printf( "\n%s has won %d more games than %s!\n ", Name2, sum4, Name );[/B]
if ( Go == 2 )
System.out.println( "" );
if ( Winner == Winner2 )
System.out.printf( "Tie!" );

}

if ( NumberRoll != 0 )
{
System.out.println( "" );
}
else

if ( counter % 5 == 0 )
System.out.println();
}
}
}```

5. Member
Join Date
Sep 2008
Posts
29
Rep Power
0
oh my gosh do you know it or not...

6. Originally Posted by whosadork
oh my gosh do you know it or not...
Looking in your code is terrible to me. Just see, in a single method, actually in the main method you have hundreds on lines. Not easy to read and see what happen. Don't do this next time, bad practice in coding. Use many number of methods, with few lines, it's really nice.

Sorry pal, I can't test your code because I can't find the way I should start look at.

7. To figure out what you code is doing, you should debug it. Use the println() statement everywhere that you do calculations to see how the variables' values are changing. Then look at the output and you should see where the problem is with your logic.

sum3 = Winner - Winner2;
sum4 = Winner2 - Winner;
Your variable names are confusing here. Sums are when + is used. Differences use -

8. Lots of things are confusing here. As said said, next try to write your code in much clearly. Try to use 10-15 lines as much as possible in a single method. It's easy to read as well.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•