java代码只输出main方法,跳过静态方法

klsxnrf1  于 2021-07-07  发布在  Java
关注(0)|答案(1)|浏览(358)

(刚开始学习java)我试着做一个测验,但它没有输出我想要的信息,它跳过了“ask”方法,只输出main方法。我必须保持它作为公共静态字符串。
导入javax.swing.joptionpane;
公开课测验{

public static String ask(String question) { 
 question= "What is the biggest animal in the world?\n";

    String optionA = "Blue Whale\n"; //( Correct answer)
    String optionB = "Elephant\n"; 
    String optionC = "Giraffe\n";
    String optionD = "Lion\n";
    String optionE = "Rhino\n";

    // Question string
    question += "A. " + optionA;
    question += "B. " + optionB;
    question += "C. " + optionC;
    question += "D. " + optionD;
    question += "E. " + optionE;

    while(true) {
    String answer = JOptionPane.showInputDialog(question);
    answer = answer.toUpperCase();

        // Correct answer // Return Correct answer
        if(answer.equals("A")) {JOptionPane.showMessageDialog(null, "Correct!");return("A");} 

        // Incorrect answer
        else if (answer.equals("B") || answer.equals("C")|| answer.equals("D") || answer.equals("E")){JOptionPane.showMessageDialog(null,"Incorrect. Pleae try again");}

        // Invalid answer
        else {
            JOptionPane.showMessageDialog(null,"Invalid answer. Please enter A, B, C, D, or E.");}}

    }
   public static void main(String[] args) {JOptionPane.showInputDialog("TEST");
}

}
输出:测试

oxalkeyp

oxalkeyp1#

据我所知,您希望启动“ask”方法。尝试将方法调用放在main方法中。

public static void main(String[] args) {
           JOptionPane.showInputDialog("TEST");
           ask("");
    }

相关问题