"Write a
DieTester
class with amain()
method that creates two die objects, rolls both dice, print the value of each die, store the sum of the roll in a variable, and prints the variable.If the die have an equal value, then call a method named
getMessage()
, with the sum as input (see below) and print the returned String.Write a
static
method calledgetMessage()
that takes the sum (an integer) as input. The method returns a string corresponding to the table that corresponds to the given sum."
My question is, how do I create a static method inside of a non-static class? I cannot get this to compile for the life of me and I know it has to be something simple that I am just not understanding.
I am only referring to the getMessage()
method, not the rest of the code.
UPDATE: Fixed and working perfectly. Had to seperate the getMessage()
method from the main()
method and then fix a few mistakes with integers and strings. Also added an if statement to print the output as a string depending on if the die equaled each other.
public class DieTester2
{
public static void main(String[] args)
{
Die die1 = new Die();
Die die2 = new Die();
die1.setSides(6);
die2.setSides(6);
die1.roll();
System.out.println("Die 1: " + die1.getValue());
die2.roll();
System.out.println("Die 2: " + die2.getValue());
int sum = (die1.getValue() + die2.getValue());
System.out.println("Sum: " + sum);
if(die1.getValue() == die2.getValue())
{
System.out.println(getMessage(sum));
}
}
public static String getMessage(int sum) {
String message;
switch (sum)
{
case 2:
message = "Snake Eyes!";
break;
case 4:
message = "Four, the hard way!";
break;
case 6:
message = "Hard Six!";
break;
case 8:
message = "Eight, the hard way!";
break;
case 10:
message = "Hard Ten";
break;
case 12:
message = "Box Cars!";
break;
default:
message = "Something went wrong";
break;
}
return message;
}
}
}
Just keep the public static int getMessage()
outside main
. And also you should use String
as return type and return string.
public class DieTester2
{
public static void main(String[] args)
{
Die die1 = new Die();
Die die2 = new Die();
die1.setSides(6);
die2.setSides(6);
die1.roll();
System.out.println("Die 1: " + die1.getValue());
die2.roll();
System.out.println("Die 2: " + die2.getValue());
int sum = (die1.getValue() + die2.getValue());
System.out.println("Sum: " + sum);
}
public static int getMessage() {
int message = 0;
switch (message)
{
case 2:
System.out.println("Snake Eyes!");
break;
case 4:
System.out.println("Four, the hard way!");
break;
case 6:
System.out.println("Hard Six!");
break;
case 8:
System.out.println("Eight, the hard way!");
break;
case 10:
System.out.println("Hard Ten");
break;
case 12:
System.out.println("Box Cars!");
break;
default:
System.out.println("Something went wrong");
break;
}
return message;
}
}
You can follow the following structure: Just put the getMessage()
outside main()
.
public class DieTester2{
public static void main(String[] args) {
// write your code here
if(die1.getValue() == die2.getValue()){
String message = getMessage(die1.getValue() + die2.getValue());
// do whatever you need to do with message
}
}
public static String getMessage(int sum) {
String message;
switch (sum){
case 2:
message = "Snake Eyes!";
break;
case 4:
message = "Four, the hard way!";
break;
case 6:
message = "Hard Six!";
break;
case 8:
message = "Eight, the hard way!";
break;
case 10:
message = "Hard Ten";
break;
case 12:
message = "Box Cars!";
break;
default:
message = "Something went wrong";
break;
}
return message;
}
}
Write a static method called getMessage
Alright, you got that public static int getMessage()
that takes the sum (an integer) as input
Oops, not quite... You need a parameter for the input int.
The method returns a string
Returns String
, not int
. Change your method type.
print the String returned
You can just return "something";
instead of break
, and then you can actually "print the message returned"
System.out.println(getMessage(sum))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.