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_Random - Fatal编程技术网

Java 我的滚动垃圾代码有什么问题?

Java 我的滚动垃圾代码有什么问题?,java,random,Java,Random,所以我正在编写一个骰子游戏,它需要我掷两个骰子并返回骰子的总和。掷7或11会赢得游戏,掷2、3或12则会输掉游戏。滚动4、5、6、8、9、10将“建立一个点”,用户在该点继续滚动,直到滚动与已建立的数字相同,或者直到滚动7,在这种情况下,用户将丢失 在我的例子中,我得到一个例外 "Exception in thread "main" java.lang.NullPointerException at Craps.getSum(Craps.java:24) at Craps.playRound(C

所以我正在编写一个骰子游戏,它需要我掷两个骰子并返回骰子的总和。掷7或11会赢得游戏,掷2、3或12则会输掉游戏。滚动4、5、6、8、9、10将“建立一个点”,用户在该点继续滚动,直到滚动与已建立的数字相同,或者直到滚动7,在这种情况下,用户将丢失

在我的例子中,我得到一个例外

"Exception in thread "main" java.lang.NullPointerException
at Craps.getSum(Craps.java:24)
at Craps.playRound(Craps.java:29)
at Craps.main(Craps.java:70)
C:\Users\owner\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 2 seconds)"
我的死亡课程是这样的:

import java.util.Random;
public class Die 
{
    private int number;
    private Random generator;

    public Die()
    {
        generator = new Random();
        roll();
    }

    public int getNumber()
    {
        return number;
    }

    public void roll()
    {
        number = generator.nextInt(6)+1;
    }
}
import java.util.Scanner;

public class Craps 
{
    private Die die1;
    private Die die2;

    private void rollDice()
    {
        die1.roll();
        die2.roll();
    }

    private int getSum()
    {
        return (die1.getNumber() + die2.getNumber());
    }

    public void playRound()
    {
        if (getSum() == 7 || getSum() == 11)
        {
            System.out.println("You rolled a " +getSum()+ "! You win!!!");
        }
        else if(getSum() == 2 || getSum() == 3 || getSum() == 12)
        {
            System.out.println("You rolled a " +getSum()+ " You lose.");
        }
        else if(getSum() == 4 || getSum() == 5 || getSum() == 6 || getSum() == 8 || getSum() == 9 || getSum() == 10)
        {
            int established = getSum();
            System.out.println("Establishing the point... Re-Rolling...");
            rollDice();

            do
            {
                rollDice();
                System.out.println("You rolled a " +getSum());
            }
            while (getSum() != established && getSum() != 7);

            if (getSum() == established)
            {
                System.out.println("You rolled a " +getSum()+ " which is also the established point, You win!");
            }
            else if(getSum() == 7)
            {
                System.out.println("You rolled a 7 after establishing the point, you lose.");
            }

        }       
    }

    public static void main(String[] args)
    {
        Craps game = new Craps();
        Scanner scan = new Scanner(System.in);
        System.out.println("Welcome to the game of Craps!");
        System.out.println("Would you like to play a game? (Y/N");
        String input = scan.nextLine();
        if (input.equalsIgnoreCase("Y"))
        {
            game.playRound();
        }
        else
        {
            System.out.println("Okay see you next time!");
            System.exit(1);
        }
    }
}
我的掷骰子游戏是这样的:

import java.util.Random;
public class Die 
{
    private int number;
    private Random generator;

    public Die()
    {
        generator = new Random();
        roll();
    }

    public int getNumber()
    {
        return number;
    }

    public void roll()
    {
        number = generator.nextInt(6)+1;
    }
}
import java.util.Scanner;

public class Craps 
{
    private Die die1;
    private Die die2;

    private void rollDice()
    {
        die1.roll();
        die2.roll();
    }

    private int getSum()
    {
        return (die1.getNumber() + die2.getNumber());
    }

    public void playRound()
    {
        if (getSum() == 7 || getSum() == 11)
        {
            System.out.println("You rolled a " +getSum()+ "! You win!!!");
        }
        else if(getSum() == 2 || getSum() == 3 || getSum() == 12)
        {
            System.out.println("You rolled a " +getSum()+ " You lose.");
        }
        else if(getSum() == 4 || getSum() == 5 || getSum() == 6 || getSum() == 8 || getSum() == 9 || getSum() == 10)
        {
            int established = getSum();
            System.out.println("Establishing the point... Re-Rolling...");
            rollDice();

            do
            {
                rollDice();
                System.out.println("You rolled a " +getSum());
            }
            while (getSum() != established && getSum() != 7);

            if (getSum() == established)
            {
                System.out.println("You rolled a " +getSum()+ " which is also the established point, You win!");
            }
            else if(getSum() == 7)
            {
                System.out.println("You rolled a 7 after establishing the point, you lose.");
            }

        }       
    }

    public static void main(String[] args)
    {
        Craps game = new Craps();
        Scanner scan = new Scanner(System.in);
        System.out.println("Welcome to the game of Craps!");
        System.out.println("Would you like to play a game? (Y/N");
        String input = scan.nextLine();
        if (input.equalsIgnoreCase("Y"))
        {
            game.playRound();
        }
        else
        {
            System.out.println("Okay see you next time!");
            System.exit(1);
        }
    }
}

保存一个空引用。

您不会得到“空错误”-您会得到一个异常,它确切地告诉您出了什么问题以及在哪里。您应该将其包括在问题中。@John3136已编辑,谢谢您指出。是否可能重复?提示:
新模具()在哪里?
?我认为将此问题标记为重复问题是无效的。@IQV我不敢相信我没有看到,非常感谢!我可以知道投票被否决的原因吗?