It's hard trying to get around Java as an old guy. I've been tinkering at this for several hours. I'm trying to call the private methods I've set up earlier but with no success. It's a simple program that generates random numbers and display messages. Anyone wants to lend a hand? Sorry if the code is bad, I've only been learning this for less than month.
import java.util.Random;
import java.util.Scanner;
public class project2 {
private static int rollDie() {
int num_random2 = (int) (Math.random() * 6) 1;
System.out.println("Die 2: " num_random2);
return num_random2;
}
private static int rollDie2() {
int num_random = (int) (Math.random() * 6) 1;
System.out.println("Die 1: " num_random);
return num_random;
}
private static void printDice(int num_random, int num_random2) {
total = num_random num_random2;
System.out.println("Total: " total);
}
int total = num_random num_random2;
private static void printSpecialMessage(int total) {
String message1 = "Snake Eyes";
String message2 = "Box cars";
if (total = 12) {
System.out.println(message2);
} else if (total = 2) {
System.out.println(message1);
} else {
break;
}
}
public static void main(String[] args) {
System.out.println("Welcome to my app!");
System.out.println();
System.out.println("Dice Roller");
System.out.println();
Scanner sc = new Scanner(System.in);
String choice = "y";
do {
rollDie();
rollDie2();
printDice();
printSpecialMessage();
System.out.print("Roll Again? (y/n): ");
choice = sc.next();
System.out.println();
}
while (choice.equalsIgnoreCase("y"));
}
}
I keep getting these errors:
The method printDice(int, int) in the type project2 is not applicable for the arguments ()
The method printSpecialMessage(int) in the type project2 is not applicable for the arguments ()
at project2.main(project2.java:51)
At this point I'm about to just give up. Hopefully someone can help me out!
CodePudding user response:
Few errors in your code I found.
if (total = 12)
should be replaced withif (total == 12)
.Make sure you always use two equals in if condition. One = is for assignment and two = for condition check.
printDice() -> The signature is not matching. Pass two values to this function.
total should be declared as static.
printSpecialMessage -> The signature is not matching. Pass one values to this function.
I suggest you start with writing simple code and then gradually go ahead with writing complex codes.
CodePudding user response:
Maybe you could consider having a single rollDie
method and just calling it twice:
import java.util.Scanner;
public class Project2 {
private static int SNAKE_EYES_TOTAL = 12;
private static int BOX_CARS_TOTAL = 2;
private static String SNAKE_EYES = "Snake Eyes";
private static String BOX_CARS = "Box cars";
private static String ROLL_AGAIN_CHOICE = "y";
private static int rollDie() {
return (int) (Math.random() * 6) 1;
}
private static void printSpecialMessage(int total) {
if (total == SNAKE_EYES_TOTAL) {
System.out.println(SNAKE_EYES);
} else if (total == BOX_CARS_TOTAL) {
System.out.println(BOX_CARS);
}
}
public static void main(String[] args) {
System.out.println("Welcome to my Dice Roller program!");
System.out.println();
Scanner scanner = new Scanner(System.in);
String choice = ROLL_AGAIN_CHOICE;
while (choice.equalsIgnoreCase(ROLL_AGAIN_CHOICE)) {
int dice1Roll = rollDie();
System.out.println("Die 1: " dice1Roll);
int dice2Roll = rollDie();
System.out.println("Die 2: " dice2Roll);
int diceTotal = dice1Roll dice2Roll;
System.out.println("Total: " diceTotal);
printSpecialMessage(diceTotal);
System.out.print("Roll Again? (y/n): ");
choice = scanner.next();
System.out.println();
}
}
}
Example Usage:
Welcome to my Dice Roller program!
Die 1: 4
Die 2: 5
Total: 9
Roll Again? (y/n): y
Die 1: 3
Die 2: 4
Total: 7
Roll Again? (y/n): y
Die 1: 1
Die 2: 1
Total: 2
Box cars
Roll Again? (y/n): n
CodePudding user response:
Your code does not compile. The following code does. Compare it with your code.
import java.util.Scanner;
public class Project2 {
private static int rollDie() {
int num_random2 = (int) (Math.random() * 6) 1;
System.out.println("Die 2: " num_random2);
return num_random2;
}
private static int rollDie2() {
int num_random = (int) (Math.random() * 6) 1;
System.out.println("Die 1: " num_random);
return num_random;
}
private static void printDice(int num_random, int num_random2) {
int total = num_random num_random2;
total = num_random num_random2;
System.out.println("Total: " total);
}
private static void printSpecialMessage(int total) {
String message1 = "Snake Eyes";
String message2 = "Box cars";
if (total == 12) {
System.out.println(message2);
}
else if (total == 2) {
System.out.println(message1);
}
}
public static void main(String[] args) {
System.out.println("Welcome to my app!");
System.out.println();
System.out.println("Dice Roller");
System.out.println();
Scanner sc = new Scanner(System.in);
String choice = "y";
do {
int one = rollDie();
int two = rollDie2();
printDice(one, two);
printSpecialMessage(one two);
System.out.print("Roll Again? (y/n): ");
choice = sc.next();
System.out.println();
} while (choice.equalsIgnoreCase("y"));
}
}
The things I changed (in no particular order).
- Method
printSpecialMessage
requires an argument. You call that method from methodmain
without an argument. Hence the following [compiler] error.The method printSpecialMessage(int) in the type Project2 is not applicable for the arguments ()
- Similarly for method
printDice
. That method requires two arguments but you call it with no arguments, which gives the following [compiler] error.The method printDice(int, int) in the type Project2 is not applicable for the arguments ()
- You need to save the value returned from method
rollDie
in a variable. You also need to save the value returned from methodrollDie2
in another variable. Then you call methodprintDice
with those two variables as arguments. In the code above I named the variablesone
andtwo
. break
is not used inif
statements. See methodprintSpecialMessage
.- When comparing integers, use
==
(double equals sign). Again see methodprintSpecialMessage
. - The following line of code is not inside the body of a method. It belongs in method
printDice
.
int total = num_random num_random2;
Here is a sample run of the above code.
Welcome to my app!
Dice Roller
Die 2: 5
Die 1: 3
Total: 8
Roll Again? (y/n): y
Die 2: 6
Die 1: 3
Total: 9
Roll Again? (y/n): n