Reputation: 3
I'm trying to figure out a way to prevent the statement printing out way too many times.
Output is supposed to look like this:
Enter Seed: 100
rolls: 6 6 2
You rolled a pair
rolls: 6 6 1
You rolled a pair
You are lucky
I'm not sure how to move around the boolean method or only call the boolean result so it doesn't print out the type of roll multiple times.
import java.util.Scanner;
import java.util.Random;
public class Triple {
public static boolean rollType(int x, int y, int z){
boolean lucky = false;
if(x == y && y == z){
System.out.println("You rolled a triple");
lucky = true;
}
else if(x == y || y == z || x == z){
System.out.println("You rolled a pair");
lucky = true;
}
else{
System.out.println("You rolled nothing");
lucky = false;
}
return lucky;
}
public static void main (String[] args) {
Scanner scnr = new Scanner(System.in);
Random randGen = new Random(); // New random number generator
int seedVal = 0;
final int MAX_DICE = 8;
//User input
System.out.println("Enter Seed: ");
seedVal = scnr.nextInt();
randGen.setSeed(seedVal);
int a1 = (randGen.nextInt(MAX_DICE)+1);
int b1 = (randGen.nextInt(MAX_DICE)+1);
int c1 = (randGen.nextInt(MAX_DICE)+1);
int a2 = (randGen.nextInt(MAX_DICE)+1);
int b2 = (randGen.nextInt(MAX_DICE)+1);
int c2 = (randGen.nextInt(MAX_DICE)+1);
// randGen.nextInt(MAX_DICE) yields 0, 1, 2, 3, 4, 5, or 7
// so + 1 makes that 1, 2, 3, 4, 5, 6, 7, or 8
System.out.println("rolls: " + a1 + " " + b1 + " " + c1);
rollType(a1, b1, c1);
System.out.println("rolls: " + a2 + " " + b2 + " " + c2);
rollType(a2, b2, c2);
if (rollType(a1, b1, c1) && rollType(a2, b2, c2)){ //work on how to fix it from printing twice THEN the boolean. should only print boolean.
System.out.println("You are lucky");
}
else{
System.out.println("You are NOT lucky");
}
return;
}
}
Upvotes: 0
Views: 200
Reputation: 4464
Delete the lines rollType(a1, b1, c1);
and rollType(a2, b2, c2);
that you have before your if
. They are repeats since your if
method calls them both no matter what.
This should fix your issue completely: now you only have two calls to the rollType
function so only two sets of outputs
Update: Since you want a particular order, this fix will solve your problem. Add a boolean
to your rollType
function as such:
public static boolean rollType(int x, int y, int z, boolean print) {
boolean lucky = false;
if (x == y && y == z) {
if (print) System.out.println("You rolled a triple");
lucky = true;
} else if (x == y || y == z || x == z) {
if (print) System.out.println("You rolled a pair");
lucky = true;
} else {
if (print) System.out.println("You rolled nothing");
lucky = false;
}
return lucky;
}
Then use rollType(a1, b1, c1, true)
and rollType(a2, b2, c2, true)
the first time and rollType(a1, b1, c1, false)
and rollType(a2, b2, c2, false)
the second.
Upvotes: 1