Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在主菜单和测验之间链接课程?_Java - Fatal编程技术网

Java 如何在主菜单和测验之间链接课程?

Java 如何在主菜单和测验之间链接课程?,java,Java,我有一个主菜单类和其他几个类,每个类都有一个小测验。每一个的代码都被进一步复制下来。我想让我的主菜单类MainClass.java能够在主菜单中选择测验类的情况下运行每个测验类。正如我现在所拥有的,每个类都可以自己运行而不会出现任何错误,但是当我尝试导入我的类标点符号.java时,例如在MainClass.java中,我失败了。我希望这是有道理的 指出我还有许多其他的测验文件可能会有所帮助,但它们几乎都与下面复制的Puncutation.java相同 MainClass.java: public

我有一个主菜单类和其他几个类,每个类都有一个小测验。每一个的代码都被进一步复制下来。我想让我的主菜单类MainClass.java能够在主菜单中选择测验类的情况下运行每个测验类。正如我现在所拥有的,每个类都可以自己运行而不会出现任何错误,但是当我尝试导入我的类标点符号.java时,例如在MainClass.java中,我失败了。我希望这是有道理的

指出我还有许多其他的测验文件可能会有所帮助,但它们几乎都与下面复制的Puncutation.java相同

MainClass.java:

public class MainClass{

    public static void main(String[] args) throws IOException {

    final Object[] options = {"(A)", "(B)", "(C)", "(D)", "Cancel"}; {


    int n = JOptionPane.showOptionDialog(null,
    "Select a shortcut that corresponds to the following text:" + "\n" + "(A): See a list of ersatz grammar rules" + "\n" + "(B): See a list of the grammar rules on which this program tests you" + "\n" + "(C): Read a tutorial about this program" + "\n" + "(D): Select a quiz",
    "What Would You Like to Do?",
    JOptionPane.YES_NO_CANCEL_OPTION,
    JOptionPane.QUESTION_MESSAGE,
    null,
    options,
    options[0]);

    switch (n)
    {
        case 0: {

        BufferedReader br = new BufferedReader(new FileReader("rules.txt"));
            for (String line; (line = br.readLine()) != null;)
                {
                    System.out.println(line);
                }
        } break;

        case 1: {   

        BufferedReader brq = new BufferedReader(new FileReader("rulesfortesting.txt"));
            for (String line; (line = brq.readLine()) != null;)
                    {
                        System.out.println(line);
                    }
        } break;

        case 2: {

        BufferedReader bqr = new BufferedReader(new FileReader("tutorial.txt"));
            for (String line; (line = bqr.readLine()) != null;)
                {
                    System.out.println(line);
                }
        } break;

        case 3: {

        final Object[] quizzes = {"Reading", "Writing"};
        {

            int f = JOptionPane.showOptionDialog(null,
            "What type of quiz would you like to take?",
            "Select a Type of Quiz",
            JOptionPane.YES_NO_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE,
            null,
            quizzes,
            quizzes[0]);

            switch (f)
            {
                case 0: 
                {
                    final Object[] option = {"Sentence Completions", "Definitions", "Vocabulary in Context"}; 

                    int q = JOptionPane.showOptionDialog(null, "Select a type of Reading quiz.", "Reading", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, option, option[0]);

                } break;

                case 1:
                {
                    final Object[] option = {"Capitalization", "Commas", "Italicization", "Miscellaneous", "Punctuation"}; 

                    int j = JOptionPane.showOptionDialog(null, "Select a type of Writing quiz.", "Writing", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, option, option[0]);

                    switch (j)
                    {
                        case 0: break;

                        case 1: break;

                        case 2: break;

                        case 3: break;
                    }

                } break;                    
            }

        }

        } break;

    }
    }   
    }
}
标点符号.java:

public static void main(String[] args) throws IOException
{

//Declaration of variables

int x = 0;
String[] quizQuestion = {""};
int score = 0;
String correct = "You are correct";
String incorrect = "You are incorrect";
String playerAnswerString = " ";
char playerAnswer = playerAnswerString.charAt(0);
playerAnswerString.toUpperCase();
String[][] scored = new String[15][2];
String[][] scores = new String[15][2];
final Object[] options = {"See Correct Answers", "See Explanations", "See Your Answers", "Save Your Answers", "Cancel"};

//Sets values for the 2-dimensional array "scored"

scored[0][0] = "1";
scored[1][0] = "2";
scored[2][0] = "3";
scored[3][0] = "4";
scored[4][0] = "5";
scored[5][0] = "6";
scored[6][0] = "7";
scored[7][0] = "8";
scored[8][0] = "9";
scored[9][0] = "10";
scored[10][0] = "11";
scored[11][0] = "12";
scored[12][0] = "13";
scored[13][0] = "14";
scored[14][0] = "15";
scored[0][1] = "B";
scored[1][1] = "B";
scored[2][1] = "C";
scored[3][1] = "D";
scored[4][1] = "C";
scored[5][1] = "A";
scored[6][1] = "A";
scored[7][1] = "B";
scored[8][1] = "B";
scored[9][1] = "C";
scored[10][1] = "C";
scored[11][1] = "C";
scored[12][1] = "D";
scored[13][1] = "C";
scored[14][1] = "B";

scores[0][0] = "1";
scores[1][0] = "2";
scores[2][0] = "3";
scores[3][0] = "4";
scores[4][0] = "5";
scores[5][0] = "6";
scores[6][0] = "7";
scores[7][0] = "8";
scores[8][0] = "9";
scores[9][0] = "10";
scores[10][0] = "11";
scores[11][0] = "12";
scores[12][0] = "13";
scores[13][0] = "14";
scores[14][0] = "15";


//Instructions and introduction to the quiz

JOptionPane.showMessageDialog(null, "You chose to test your writing skills--specifically, errors with punctuation.");
JOptionPane.showMessageDialog(null, "Each question has the answer choices \"(A),\" \"(B),\" \"(C),\" and \"(D),\" each of which, except for \"(D),\" corresponds to \na part of a sentence that follows it.  Choose the letter that has text that contains an error.\nIf you believe there is no error, choose \"(D).\"");
JOptionPane.showMessageDialog(null, "Here's an example question: \n [(A) At 1912] the ship [(B) HMS _Titanic_] sank amid lofty [(C) claims made] by the White Star Line. [(D): no error] \n \n As you can see, \"(A)\" contains the error because it uses an incorrect preposition.");
JOptionPane.showMessageDialog(null, "To answer a question, simply type the letter of the answer choice that you believe is incorrect. \n Do NOT type the parentheses.");

//Counts number of questions correct
//Gives the user the questions

for(x = 0; x < 15; x++)
{

    //Displays questions and counts up the score if the user answers correctly.

    if(x==0)
    {
        playerAnswerString = JOptionPane.showInputDialog(null, "\"You [(A) are] [(B) nice; funny; and] [(C) smart,\" I told] my sister. [(D): no error]");
        if(playerAnswerString.equalsIgnoreCase("B"))
        {
            JOptionPane.showMessageDialog(null, correct);
            score++;
        }
        else if(playerAnswerString.equalsIgnoreCase("C") || playerAnswerString.equalsIgnoreCase("A") || playerAnswerString.equalsIgnoreCase("D"))
        {
            JOptionPane.showMessageDialog(null, incorrect);
            playerAnswerString = scores[0][1];
        }
    }

    if(x==1)

    {
        playerAnswerString = JOptionPane.showInputDialog(null, "I [(A) said,] [(B) \"Heaven\"s got a plan] for [(C) you. / Don't you worry,] don't you worry, child.\" [(D): no error]");
        if(playerAnswerString.equalsIgnoreCase("B"))
        {
            JOptionPane.showMessageDialog(null, correct);
            score++;
        }
        else if(playerAnswerString.equalsIgnoreCase("C") || playerAnswerString.equalsIgnoreCase("A") || playerAnswerString.equalsIgnoreCase("D"))
        {
            JOptionPane.showMessageDialog(null, incorrect);
            playerAnswerString = scores[1][1]; 
        }
    }
  if(x==2)
    {
        playerAnswerString = JOptionPane.showInputDialog(null, "I asked my [(A) mom if she] loved [(B) gardening] with [(C) her sister?][(D): no error]");
        if(playerAnswerString.equalsIgnoreCase("C"))
        {
            JOptionPane.showMessageDialog(null, correct);
            score++;
        }
        else if(playerAnswerString.equalsIgnoreCase("B") || playerAnswerString.equalsIgnoreCase("A") || playerAnswerString.equalsIgnoreCase("D"))
        {
            JOptionPane.showMessageDialog(null, incorrect);
            playerAnswerString = scores[2][1];
        }
    }

if(x==3)
    {
        playerAnswerString = JOptionPane.showInputDialog(null, "I [(A) said: \"Do] you [(B) want to meet] [(C) the president?\"] [(D): no error]");

        if(playerAnswerString.equalsIgnoreCase("D"))
        {
            JOptionPane.showMessageDialog(null, correct);
            score++;
        }
        else if(playerAnswerString.equalsIgnoreCase("C") || playerAnswerString.equalsIgnoreCase("A") || playerAnswerString.equalsIgnoreCase("B"))
        {
            JOptionPane.showMessageDialog(null, incorrect);
            playerAnswerString = scores[3][1];
        }
    }
 if(x==4)
 {
    playerAnswerString = JOptionPane.showInputDialog(null, "When the judge [(A) asked me if I] had paid [(B) the fine,] I [(C) said: yes.] [(D): no error]");

    if(playerAnswerString.equalsIgnoreCase("C"))
    {
        JOptionPane.showMessageDialog(null, correct);
        score++;
    }
    else if(playerAnswerString.equalsIgnoreCase("B") || playerAnswerString.equalsIgnoreCase("A") || playerAnswerString.equalsIgnoreCase("D"))
    {
        JOptionPane.showMessageDialog(null, incorrect);
        playerAnswerString = scores[4][1];
    }

 }
 if(x==5)
 {
    playerAnswerString = JOptionPane.showInputDialog(null, "The points we need to discuss [(A) are: the train,] [(B) the bus route,] [(C) and the airport.] [(D): no error]");

    if(playerAnswerString.equalsIgnoreCase("A"))
    {
        JOptionPane.showMessageDialog(null, correct);
        score++;
    }
    else if(playerAnswerString.equalsIgnoreCase("C") || playerAnswerString.equalsIgnoreCase("B") || playerAnswerString.equalsIgnoreCase("D"))
    {
        JOptionPane.showMessageDialog(null, incorrect);
        playerAnswerString = scores[5][1];
    }

 }
 if(x==6)
 {
    playerAnswerString = JOptionPane.showInputDialog(null, "We need two [(A) things: a] [(B) firetruck and] [(C) a policecar, for] the set. {(D): no error]");

    if(playerAnswerString.equalsIgnoreCase("A"))
    {
        JOptionPane.showMessageDialog(null, correct);
        score++;
    }
    else if(playerAnswerString.equalsIgnoreCase("C") || playerAnswerString.equalsIgnoreCase("B") || playerAnswerString.equalsIgnoreCase("D"))
    {
        JOptionPane.showMessageDialog(null, incorrect);
        playerAnswerString = scores[6][1];
    }

 }
if(x==7)
 {
    playerAnswerString = JOptionPane.showInputDialog(null, "We [(A) only need] one [(B) tool--a hammer, for] this [(C) project, sir.] [(D): no error]");

    if(playerAnswerString.equalsIgnoreCase("B"))
    {
        JOptionPane.showMessageDialog(null, correct);
        score++;
    }
    else if(playerAnswerString.equalsIgnoreCase("C") || playerAnswerString.equalsIgnoreCase("A") || playerAnswerString.equalsIgnoreCase("D"))
    {
        JOptionPane.showMessageDialog(null, incorrect);
        playerAnswerString = scores[7][1];
    }

 }

if(x==8)
 {
    playerAnswerString = JOptionPane.showInputDialog(null, "[(A) Walmart is] selling [(B) PS4's for] $5 to [(C) everyone who buys a new TV.] [(D): no error]");

    if(playerAnswerString.equalsIgnoreCase("B"))
    {
        JOptionPane.showMessageDialog(null, correct);
        score++;
    }
    else if(playerAnswerString.equalsIgnoreCase("C") || playerAnswerString.equalsIgnoreCase("A") || playerAnswerString.equalsIgnoreCase("D"))
    {
        JOptionPane.showMessageDialog(null, incorrect);
        playerAnswerString = scores[8][1];
    }

 }
 if(x==9)
 {
    playerAnswerString = JOptionPane.showInputDialog(null, "I have two [(A) kids, a boy] and [(B) a girl,] and the [(C) boys toys are always] on the floor for me to pick up. [(D): no error]");

    if(playerAnswerString.equalsIgnoreCase("C"))
    {
        JOptionPane.showMessageDialog(null, correct);
        score++;
    }
    else if(playerAnswerString.equalsIgnoreCase("B") || playerAnswerString.equalsIgnoreCase("A") || playerAnswerString.equalsIgnoreCase("D"))
    {
        JOptionPane.showMessageDialog(null, incorrect);
        playerAnswerString = scores[9][1];
    }

 }
 if(x==10)
 {
    playerAnswerString = JOptionPane.showInputDialog(null, "There are 4 [(A) words to which] you should pay [(B) close attention:] [(C) \"Khafkaesque\", \"bombastic\", \"pickle\", and \"supercilious\".] [(D): no error]");

    if(playerAnswerString.equalsIgnoreCase("C"))
    {
        JOptionPane.showMessageDialog(null, correct);
        score++;
    }
    else if(playerAnswerString.equalsIgnoreCase("B") || playerAnswerString.equalsIgnoreCase("A") || playerAnswerString.equalsIgnoreCase("D"))
    {
        JOptionPane.showMessageDialog(null, incorrect);
        playerAnswerString = scores[10][1];
    }

 }
 if(x==11)
 {
    playerAnswerString = JOptionPane.showInputDialog(null, "Dwayne [(A) Johnson's] [(B) nickname is] [(C) The Rock.] [(D): no error]");
    if(playerAnswerString.equalsIgnoreCase("C"))
    {
        JOptionPane.showMessageDialog(null, correct);
        score++;
    }
    else if(playerAnswerString.equalsIgnoreCase("B") || playerAnswerString.equalsIgnoreCase("A") || playerAnswerString.equalsIgnoreCase("D"))
    {
        JOptionPane.showMessageDialog(null, incorrect);
        playerAnswerString = scores[11][1];
    }

 }
 if(x==12)
 {
    playerAnswerString = JOptionPane.showInputDialog(null, "I [(A) sell pretty much] anything a customer [(B) wants--but I] charge a premium for highly [(C) sought-after products.] {(D): no error]");

    if(playerAnswerString.equalsIgnoreCase("D"))
    {
        JOptionPane.showMessageDialog(null, correct);
        score++;
    }
    else if(playerAnswerString.equalsIgnoreCase("C") || playerAnswerString.equalsIgnoreCase("A") || playerAnswerString.equalsIgnoreCase("B"))
    {
        JOptionPane.showMessageDialog(null, incorrect);
        playerAnswerString = scores[12][1];
    }

 }
 if(x==13)
 {
    playerAnswerString = JOptionPane.showInputDialog(null, "I love [(A) you--and] [(B) that's] [(C) what matters] [(D): no error]");

    if(playerAnswerString.equalsIgnoreCase("C"))
    {
        JOptionPane.showMessageDialog(null, correct);
        score++;
    }
    else if(playerAnswerString.equalsIgnoreCase("B") || playerAnswerString.equalsIgnoreCase("A") || playerAnswerString.equalsIgnoreCase("D"))
    {
        JOptionPane.showMessageDialog(null, incorrect);
        playerAnswerString = scores[13][1];
    }

 }
 if(x==14)
 {
    playerAnswerString = JOptionPane.showInputDialog(null, "I went up to [(A) my dad and] [(B) asked, \"Whats] [(C) up, dad?\"] [(D): no error]");

    if(playerAnswerString.equalsIgnoreCase("B"))
    {
        JOptionPane.showMessageDialog(null, correct);
        score++;
    }
    else if(playerAnswerString.equalsIgnoreCase("C") || playerAnswerString.equalsIgnoreCase("A") || playerAnswerString.equalsIgnoreCase("D"))
    {
        JOptionPane.showMessageDialog(null, incorrect);
        playerAnswerString = scores[14][1];
    }

 }
}

//Gives the option to view explanations to the correct answers to each question

int hq = JOptionPane.showOptionDialog(null, "You scored " + score + "/15" + "\n" + "What would you like to do next?", "Good Job!", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
switch (hq)
{
    case 0: {
        for(int i = 0; i < scored.length; i++)
            {
                for(int j = 0; j < scored[i].length; j++)
                {
                    System.out.print(scored[i][j]);
                    if(j < scored[i].length - 1) System.out.print(" ");
                }
                System.out.println();
            }
    } break;

    case 1: {
        BufferedReader br = new BufferedReader(new FileReader("punctuationexplanations.txt"));
        for (String line; (line = br.readLine()) != null;)
            {
                System.out.println(line);
            }
    } break;

    case 2: {
        for(int i = 0; i < scores.length; i++)
            {
                for(int j = 0; j < scores[i].length; j++)
                {
                    System.out.print(scores[i][j]);
                    if(j < scores[i].length - 1) System.out.print(" ");
                }
                System.out.println();
            }

    } break; 

}
另外值得注意的是,我尝试过继承,但我不完全知道如何使用extend。我曾尝试让我的测验类扩展MainClass.java,但这对我来说不起作用


对不起,我很无知,但我对这件事还不熟悉。非常感谢你的帮助

如果所有类都在同一个文件夹/包中,则不必导入它们即可工作。如果在不同的包中,请使用import package.class;并将导入添加到公共类MainClass之前{

标点符号类只有一个main方法。不能从另一个main方法启动main方法。因此,需要将变量和功能划分为构造函数和运行游戏的方法

public class Punctuation
{
  private int x;
  private String[] quizQuestion;
  private int score;
  ...

  public Punctuation()
  {
    this.x = 0;
    this.quizQuestion = {""}
    this.score = 0
    ...
  }

  public void startQuiz()
  {
    //Instructions and introduction to the quiz

    JOptionPane.showMessageDialog(null, "You chose to test your writing skills--specifically, errors with punctuation.");
    JOptionPane.showMessageDialog(null, "Each question has the answer choices \"(A),\" \"(B),\" \"(C),\" and \"(D),\" each of which, except for \"(D),\" corresponds to \na part of a sentence that follows it.  Choose the letter that has text that contains an error.\nIf you believe there is no error, choose \"(D).\"");
    JOptionPane.showMessageDialog(null, "Here's an example question: \n [(A) At 1912] the ship [(B) HMS _Titanic_] sank amid lofty [(C) claims made] by the White Star Line. [(D): no error] \n \n As you can see, \"(A)\" contains the error because it uses an incorrect preposition.");
    JOptionPane.showMessageDialog(null, "To answer a question, simply type the letter of the answer choice that you believe is incorrect. \n Do NOT type the parentheses.");

    //Counts number of questions correct
    //Gives the user the questions

    for(x = 0; x < 15; x++)
    {
    ...
  }
} 

一旦你能做到这一点,看看-有很多提示你可以尝试清理你的代码。这些300多行代码是问你的问题所必需的吗?我只是想彻底了解一下;在这个网站上寻找我的问题的答案很长时间后,我注意到很多人的问题无法得到回答,因为他们的问题太多了没有包含他们的代码。所以,为了避免任何混乱,我只包含了几乎所有内容。非常感谢您的帮助!
Punctuation p = new Punctuation();
p.startQuiz();