我正在完成一项任务,这是我们第一次做方法。我试图创建下面的方法,但它一直说void不是一个有效的类型。问候它的名字是一个非法的修饰符,它说我在括号后需要一个分号。我不能为我的生活弄清楚我做错了什么,正如我在所有我看过的例子中所看到的那样。
public static void greeting(){
String greet = "This program will play 3 games of rock paper scissors with you.\n\n"+
"It will ask you for your choice and then randomly generate a choice for the computer.\n\n"+
"After all 3 games you will be shown the total results.";
JOptionPane.showMessageDialog(null, greet, "Greeting",1);
}
这里是完整的代码:
import javax.swing.*;
import java.util.*;
public class RPS_Game {
public static void main(String[] args) {
// TODO Auto-generated method stub
public static void greeting(){
String greet = "This program will play 3 games of rock paper scissors with you.\n\n"+
"It will ask you for your choice and then randomly generate a choice for the computer.\n\n"+
"After all 3 games you will be shown the total results.";
JOptionPane.showMessageDialog(null, greet, "Greeting",1);
}
}
}
好的我把它改成了它并且它有效。有点让我感到困惑,因为看起来我要使用的方法应该被编写,然后在下面调用。
import javax.swing.*;
public class RPS_Game {
public static void main(String[] args) {
// TODO Auto-generated method stub
greeting();
}
public static void greeting(){
String greet = "This program will play 3 games of rock paper scissors with you.\n\n"+
"It will ask you for your choice and then randomly generate a choice for the computer.\n\n"+
"After all 3 games you will be shown the total results.";
JOptionPane.showMessageDialog(null, greet, "Greeting",1);
}
}
答案 0 :(得分:0)
你必须把所有东西放在一个类中,制作一个main方法,并从main方法调用你的方法,如下所示:
public class Greeting {
public static void main(String[] args) {
greeting();
}
public static void greeting(){
String greet = "This program will play 3 games of rock paper scissors with you.\n\n"+
"It will ask you for your choice and then randomly generate a choice for the computer.\n\n"+
"After all 3 games you will be shown the total results.";
JOptionPane.showMessageDialog(null, greet, "Greeting",1);
}
}
答案 1 :(得分:0)
我建议您将文件命名为RPSGame.java
,因为Java约定使用CamelCase作为类名。
您不能将方法嵌套在其他方法中。
import javax.swing.*;
import java.util.*;
// Avoid using '_' (underscore) in the filename/classname because of conventions.
public class RPSGame {
// Define greeting
public static void greeting() {
String greet = "This program will play 3 games of rock paper scissors with you.\n\n"+
"It will ask you for your choice and then randomly generate a choice for the computer.\n\n"+
"After all 3 games you will be shown the total results.";
JOptionPane.showMessageDialog(null, greet, "Greeting",1);
}
// The main() method is the entry point into your class, call your methods here.
public static void main(String[] args) {
greeting(); // Call greeting.
}
}
答案 2 :(得分:0)
你需要把你的方法放在main方法之外,然后从main调用它:
import javax.swing.*;
public class RPS_Game {
public static void main(String[] args) {
// TODO Auto-generated method stub
greeting();
}
public static void greeting() {
String greet = "This program will play 3 games of rock paper scissors with you.\n\n" +
"It will ask you for your choice and then randomly generate a choice for the computer.\n\n" +
"After all 3 games you will be shown the total results.";
JOptionPane.showMessageDialog(null, greet, "Greeting", 1);
}
}
此外,您的代码中不需要import java.util.*;
库。
答案 3 :(得分:0)
您无法在方法内定义方法,只能在方法内调用方法。
import javax.swing.*;
import java.util.*;
public class RPS_Game {
public static void greeting(){
String greet = "This program will play 3 games of rock paper scissors with you.\n\n"+
"It will ask you for your choice and then randomly generate a choice for the computer.\n\n"+
"After all 3 games you will be shown the total results.";
JOptionPane.showMessageDialog(null, greet, "Greeting",1);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
greeting();
}
}