从小程序创建类

时间:2017-06-25 19:29:09

标签: java arrays class syntax-error

我在这里看到了一个小的多项选择程序

    import java.util.Arrays;
    import java.util.Scanner;

    public class logic  {

public static void main(String[] args) {

    String question = "Welche Fabe hat der Frosch?"; //Your Question here

    //Possible Answers
    String a = "Gelb";
    String b = "Rot";
    String c = "Grün";

    String correct = "c"; //Right Answer

    String [] arr = new String[5];

    arr[0] = question;
    arr[1] = a;
    arr[2] = b;
    arr[3] = c;
    arr[4] = correct;

    System.out.println(arr[0] + "\n");
    System.out.println("Answer a: " + arr[1]);
    System.out.println("Answer b: " + arr[2]);
    System.out.println("Answer c: " + arr[3] + "\n");

    int i = 0;
    while (i != 1) {
    Scanner userin = new Scanner(System.in);

    System.out.println("Type a,b,c for correct answer.\n");

    String selected = userin.next();

    if (selected.equals(correct)) {
        i = 1;
        System.out.println("\nCorrect!");
    }else {
        System.out.println("Try again");
    }
    }
}
}

我以为我可以根据它创建一个类。这就是问题的开始。

这是我到目前为止所得到的。

import java.util.Scanner;

public class MultipleChoice {

    private String question, a, b, c, correct;

    public void setQuestion(String question) {
      this.question = question;
    }

    public void setA(String a) {
      this.a = a;
    }

    public void setB (String b) {
      this.b = b;
    }

    public void setC (String c) {
      this.c = c;
    }

    public void setCorrect (String correct) {
        this.correct = correct;
    }



    String[] arr = new String[5];

    arr[0] = question;
    arr[1] = a;
    arr[2] = b;
    arr[3] = c;
    arr[4] = correct;

    System.out.println(arr[0] + "\n");
    System.out.println("Answer a: " + arr[1]);
    System.out.println("Answer b: " + arr[2]);
    System.out.println("Answer c: " + arr[3] + "\n");

    int i = 0;
    while (i != 1) {
        Scanner userin = new Scanner(System.in);

        System.out.println("Type a,b,c for correct answer.\n");

        String selected = userin.next();

        if (selected.equals(correct)) {
            i = 1;
            System.out.println("\nCorrect!");
        }else {
            System.out.println("Try again");
        }
    }
}
}

当我尝试创建数组时,我收到错误“令牌上的语法错误”;“,{此符号之后的预期”。

我是Java的新手。

提前致谢

2 个答案:

答案 0 :(得分:0)

您的类中的以下代码不在任何方法或构造函数中:

String[] arr = new String[5];

arr[0] = question;
arr[1] = a;
arr[2] = b;
arr[3] = c;
arr[4] = correct;

System.out.println(arr[0] + "\n");
System.out.println("Answer a: " + arr[1]);
System.out.println("Answer b: " + arr[2]);
System.out.println("Answer c: " + arr[3] + "\n");

int i = 0;
while (i != 1) {
    Scanner userin = new Scanner(System.in);

    System.out.println("Type a,b,c for correct answer.\n");

    String selected = userin.next();

    if (selected.equals(correct)) {
        i = 1;
        System.out.println("\nCorrect!");
    }else {
        System.out.println("Try again");
    }
}

这会导致发生错误,因为每个语句都必须用Java包含在方法或构造函数中。

让我们尝试将此代码放在一个方法中:

public void ask() {
    String[] arr = new String[5];

    arr[0] = question;
    arr[1] = a;
    arr[2] = b;
    arr[3] = c;
    arr[4] = correct;

    System.out.println(arr[0] + "\n");
    System.out.println("Answer a: " + arr[1]);
    System.out.println("Answer b: " + arr[2]);
    System.out.println("Answer c: " + arr[3] + "\n");

    int i = 0;
    while (i != 1) {
        Scanner userin = new Scanner(System.in);

        System.out.println("Type a,b,c for correct answer.\n");

        String selected = userin.next();

        if (selected.equals(correct)) {
            i = 1;
            System.out.println("\nCorrect!");
        }else {
            System.out.println("Try again");
        }
    }
}

现在,只要调用ask,代码就会被执行。

以下是MultipleChoice方法中main类的使用方法:

MultipleChoice mc = new MultipleChoice("How many months are there in a year?");
mc.setA("11");
mc.setB("12");
mc.setC("13");
mc.setCorrect("b");
mc.ask();

答案 1 :(得分:-1)

问题是你在类的主体中有可执行语句;这些可执行语句应该只包含在方法体中。见Syntax error on token ";", { expected after this token in Random string creator

例如,对System.out.println的调用需要在此类的一个方法内,可以在别处调用。