Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.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
为什么这个javaclass会得到这个异常?_Java_Class - Fatal编程技术网

为什么这个javaclass会得到这个异常?

为什么这个javaclass会得到这个异常?,java,class,Java,Class,我从一个网站上下载了这段代码,以适应其他用途,但当我在Netbeans上试用时,它总是给我带来问题。我看了看,它看起来很坚固,但是,我显然没有。。。 例外情况如下: “{”应为“类、接口或枚举应为” 我检查了所有的支架和支架,我不知道有什么问题 public class Coin() { private String sideUp; /** * Default constructor */ public Coin() { // ini

我从一个网站上下载了这段代码,以适应其他用途,但当我在Netbeans上试用时,它总是给我带来问题。我看了看,它看起来很坚固,但是,我显然没有。。。 例外情况如下:

“{”应为“类、接口或枚举应为”

我检查了所有的支架和支架,我不知道有什么问题

public class Coin() 
{
    private String sideUp;

    /**
    * Default constructor
    */
    public Coin() 
    {
    // initialize sideUp
        toss();
    }

    /**
    * This method will simulate the tossing of a coin. It should set 
    * the   
    * sideUp field to either "heads" or "tails".
    */
    public void toss() 
    {

        Random rand = new Random();

        // Get a random value, 0 or 1.
        int value = rand.nextInt(2);

        if (value == 0) 
        {
            this.sideUp = "heads";
        } 
        else 
        {
        this.sideUp = "tails";
        }
    }

    /**
    * 
    * @return The side of the coin facing up.
    */
    public String getSideUp() 
    {
        return sideUp;
    }
}

是否有我遗漏的大括号?

您需要从类名中删除括号,它应该是:

public class Coin 
{
    ...
}

标识符中只允许使用“Java字母”或“Java数字”,不包括Java语言规范中的括号cf。

您需要从类名中删除括号,而应该是:

public class Coin 
{
    ...
}
标识符中只允许使用“Java字母”或“Java数字”,不包括Java语言规范中的括号cf。

从类名中删除()并执行所需的导入。

可行的代码如下所示:

import java.util.Random;

public class Coin {
    private String sideUp;

    /**
     * Default constructor
     */
    public Coin() {
        // initialize sideUp
        toss();
    }

    /**
     * This method will simulate the tossing of a coin. It should set the sideUp
     * field to either "heads" or "tails".
     */
    public void toss() {

        Random rand = new Random();

        // Get a random value, 0 or 1.
        int value = rand.nextInt(2);

        if (value == 0) {
            this.sideUp = "heads";
        } else {
            this.sideUp = "tails";
        }
    }

    /**
     * 
     * @return The side of the coin facing up.
     */
    public String getSideUp() {
        return sideUp;
    }
}
从类名中删除()并执行所需的导入。

可行的代码如下所示:

import java.util.Random;

public class Coin {
    private String sideUp;

    /**
     * Default constructor
     */
    public Coin() {
        // initialize sideUp
        toss();
    }

    /**
     * This method will simulate the tossing of a coin. It should set the sideUp
     * field to either "heads" or "tails".
     */
    public void toss() {

        Random rand = new Random();

        // Get a random value, 0 or 1.
        int value = rand.nextInt(2);

        if (value == 0) {
            this.sideUp = "heads";
        } else {
            this.sideUp = "tails";
        }
    }

    /**
     * 
     * @return The side of the coin facing up.
     */
    public String getSideUp() {
        return sideUp;
    }
}

这个程序也给了我一个关于兰德的问题。这个程序也给了我一个关于兰德的问题。