简体   繁体   English

如何从Java类读取字符串?

[英]How do I read a String from a class, Java?

When I try to compile this, I get tons of error, as I have "illegal start of type", and expected semicolons, parentheses, etc. I have got no idea how to read what's behind each door. 当我尝试对此进行编译时,由于出现“非法的类型开始”以及预期的分号,括号等信息,我遇到了很多错误。我不知道如何阅读每扇门后面的内容。 >< > <

import java.lang.Math.*;
import java.util.Random;

public class Paradox {
public static void main(String[] args){
System.out.println("You have got three doors, there is a price inside each of them. Pick a door: A, B, or C");
    Door A = new Door();
    Door B = new Door();
    Door C = new Door();

    Random rand = new Random();
    int i = rand.nextInt(3);
    System.out.println(i);

    if(i==0) {
        A.goatorcar="Car";
        B.goatorcar="Goat";
        C.goatorcar="Goat";
    }
    else if (i==1){
        A.goatorcar="Goat";
        B.goatorcar="Car";
        C.goatorcar="Goat";
    }
    else {
        A.goatorcar="Goat";
        B.goatorcar="Goat";
        C.goatorcar="Car";
    }
}

System.out.println(A.goatorcar);
System.out.println(B.goatorcar);
System.out.println(C.goatorcar);

}

class Door {
String goatorcar;
}

You have an extra closing brace } : 您有一个额外的右括号}

} <<<<

System.out.println(A.goatorcar);
System.out.println(B.goatorcar);
System.out.println(C.goatorcar);

Your problem is with the System.out.println() statements. 您的问题出在System.out.println()语句上。 In Java, you cant just "do" things. 在Java中,您不能“做”事情。 Statements like that (function calls) must be inside of a function, in this case your main function. 诸如此类的语句(函数调用)必须在函数内部,在这种情况下,您是主函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM