Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.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,您好,我是一名java初学者,我正在尝试编写一个程序,记录两名玩家之间游戏中三个骰子的值。我必须利用阵列。问题是,当我出于某种原因打印到屏幕上时,每个骰子的值都是0,而不是打印随机生成的卷。我们的想法是,当我们打印到屏幕上时,它应该是第1轮玩家1:并显示在三个骰子上滚动的每个数字,然后进行计算并根据三个骰子是否相同或完全不同等情况输出点数。 这就是我迄今为止所做的一切。我是一个初学者,已经花了好几个小时试图弄明白这一点,如果有任何帮助,我将不胜感激 这是头等舱 //描述:程序模拟两名玩家,每名玩

您好,我是一名java初学者,我正在尝试编写一个程序,记录两名玩家之间游戏中三个骰子的值。我必须利用阵列。问题是,当我出于某种原因打印到屏幕上时,每个骰子的值都是0,而不是打印随机生成的卷。我们的想法是,当我们打印到屏幕上时,它应该是第1轮玩家1:并显示在三个骰子上滚动的每个数字,然后进行计算并根据三个骰子是否相同或完全不同等情况输出点数。 这就是我迄今为止所做的一切。我是一个初学者,已经花了好几个小时试图弄明白这一点,如果有任何帮助,我将不胜感激

这是头等舱

//描述:程序模拟两名玩家,每名玩家掷三个骰子进行多轮游戏

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

public class Dicegame   {

static Scanner read = new Scanner (System.in);

public static void main(String[] args)  {

int[] num = new int[3];
int numberOfRounds;
numberOfRounds = 0;
do {
//This will print out a request for the user
    System.out.println("Please enter the number of rounds: \n");
//This will ensure the input value will remain above 0
    numberOfRounds = read.nextInt(); } while (numberOfRounds < 1);

    Dicegame2 Dicegame2Object = new Dicegame2(numberOfRounds);
    //This is instantiating the scanner object
    Dicegame1 Dicegame1Object = new Dicegame1(num[0], num[1], num[2]);

    Dicegame1Object.order();
    Dicegame2Object.diceValue();

    for(int round=0;round<numberOfRounds;round++){
        System.out.println("Round "+ (round+1)
        +" Player1: "+ num[0]+num[1]+num[2]+
        " Points: ");   }

    if(Dicegame1Object.threeAllTheSame()){
        System.out.print(Dicegame2Object.sumOfDiceSame());;
    }else{
        if(Dicegame1Object.alInOrder()){
            System.out.print(Dicegame2Object.sumOfDiceRun());
        }else{
            if(Dicegame1Object.twoSameOneDifferent()){
                System.out.print(Dicegame2Object.sumOfDiceDifferent());
            }else{
                System.out.print(Dicegame2Object.sumOfDicePair());
            }
        }
    }
}   

}
import java.util.Arrays;

public class Dicegame1  {
//variables
private int Dice1;
private int Dice2;
private int Dice3;

    int[] num = new int[3];
private final int numberOfThrows = 3;

                    //Constructor
public Dicegame1 (int D1, int D2, int D3)

//This is assigning local variables to class variables

{   num[0] = D1;
    num[1] = D2;
    num[2] = D3;
}

//This will calculate to see if the dice are all the same
//This is a boolean method which contains an if statement   
    public boolean threeAllTheSame(){
        boolean trueOrNot;
        if((num[0] == num[1])&&(num[1]==num[2])){
                trueOrNot = true;
        }else{
                trueOrNot = false;
            }
        return trueOrNot;
}

//This will calculate to see if the dice are a pair
//This is a boolean method which contains an if statement
    public boolean twoSameOneDifferent(){
        boolean trueOrNot;
        if  (((num[0] == num[1])&&(num[1]!=num[2]))||
            ((num[1] == num[2])&&(num[2]!=num[0]))||
            ((num[0] == num[2])&&(num[0]!=num[1]))){
                trueOrNot = true;
        }else{
                trueOrNot = false;
            }
        return trueOrNot;
}

//This will calculate to see if the roll is a run
//This is a boolean method which contains an if statement
    public boolean alInOrder(){
        boolean trueOrNot;
        if  ((num[1] == (num[0]+1))&&
            (num[2] == (num[1]+1))){
                trueOrNot = true;
        }else{
                trueOrNot = false;
            }
        return trueOrNot;

    }

//This will calculate to see if the dice are all different  
//This is a boolean method which contains an if statement  
    public boolean allDifferent(){
        boolean trueOrNot;
        if  ((num[0] != (num[1]))&&(num[1] != (num[2]))&&(num[2] !=    (num[0]))){
                trueOrNot = true;
        }else{
                trueOrNot = false;
        }
        return trueOrNot;
    }


//This will sort out the numbers of the array in order  
    public void order(){
        Arrays.sort(num);
    }

    }
import java.util.Arrays;
import java.util.Random;

public class Dicegame2  {

//variables
private int numberOfRounds;
private int Dice1;
private int Dice2;
private int Dice3;
private Dicegame1[] match;

//constructor
public Dicegame2(int nOfRounds) {

    numberOfRounds = nOfRounds;
    match = new Dicegame1 [numberOfRounds];

}
    public void diceValue() {
        int[] num = new int[3];
        for(int i=1;i<numberOfRounds;i++){
            Dice1 = 1+(int)(6*Math.random());
            Dice2 = 1+(int)(6*Math.random());
            Dice3 = 1+(int)(6*Math.random());
            match [i] = new Dicegame1(num[0], num[1], num[2]);
        }   

}

//This will calculate the sum of dice if all the same       
    public int sumOfDiceSame(){
        int calc1;
        int[] num = new int[3];
        calc1 =((num[0]+num[1]+num[2])+60);
        return calc1;
    }
 //This will calculate the sum of dice if its a pair
    public int sumOfDicePair(){
        int calc2;
        int[] num = new int[3];
        calc2 =((num[0]+num[1]+num[2])+20);
        return calc2;
    }
//This will calculate the sum of dice if its a run
    public int sumOfDiceRun(){
        int calc3;
        int[] num = new int[3];
        calc3 =((num[0]+num[1]+num[2])+40);
        return calc3;
    }       
 //This will calculate the sum of dice if all different
    public int sumOfDiceDifferent(){
        int calc4;
        int[] num = new int[3];
        calc4 =(num[0]+num[1]+num[2]);
        return calc4;
    }
}
这是第三节也是最后一节课

//描述:程序模拟两名玩家,每名玩家掷三个骰子进行多轮游戏

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

public class Dicegame   {

static Scanner read = new Scanner (System.in);

public static void main(String[] args)  {

int[] num = new int[3];
int numberOfRounds;
numberOfRounds = 0;
do {
//This will print out a request for the user
    System.out.println("Please enter the number of rounds: \n");
//This will ensure the input value will remain above 0
    numberOfRounds = read.nextInt(); } while (numberOfRounds < 1);

    Dicegame2 Dicegame2Object = new Dicegame2(numberOfRounds);
    //This is instantiating the scanner object
    Dicegame1 Dicegame1Object = new Dicegame1(num[0], num[1], num[2]);

    Dicegame1Object.order();
    Dicegame2Object.diceValue();

    for(int round=0;round<numberOfRounds;round++){
        System.out.println("Round "+ (round+1)
        +" Player1: "+ num[0]+num[1]+num[2]+
        " Points: ");   }

    if(Dicegame1Object.threeAllTheSame()){
        System.out.print(Dicegame2Object.sumOfDiceSame());;
    }else{
        if(Dicegame1Object.alInOrder()){
            System.out.print(Dicegame2Object.sumOfDiceRun());
        }else{
            if(Dicegame1Object.twoSameOneDifferent()){
                System.out.print(Dicegame2Object.sumOfDiceDifferent());
            }else{
                System.out.print(Dicegame2Object.sumOfDicePair());
            }
        }
    }
}   

}
import java.util.Arrays;

public class Dicegame1  {
//variables
private int Dice1;
private int Dice2;
private int Dice3;

    int[] num = new int[3];
private final int numberOfThrows = 3;

                    //Constructor
public Dicegame1 (int D1, int D2, int D3)

//This is assigning local variables to class variables

{   num[0] = D1;
    num[1] = D2;
    num[2] = D3;
}

//This will calculate to see if the dice are all the same
//This is a boolean method which contains an if statement   
    public boolean threeAllTheSame(){
        boolean trueOrNot;
        if((num[0] == num[1])&&(num[1]==num[2])){
                trueOrNot = true;
        }else{
                trueOrNot = false;
            }
        return trueOrNot;
}

//This will calculate to see if the dice are a pair
//This is a boolean method which contains an if statement
    public boolean twoSameOneDifferent(){
        boolean trueOrNot;
        if  (((num[0] == num[1])&&(num[1]!=num[2]))||
            ((num[1] == num[2])&&(num[2]!=num[0]))||
            ((num[0] == num[2])&&(num[0]!=num[1]))){
                trueOrNot = true;
        }else{
                trueOrNot = false;
            }
        return trueOrNot;
}

//This will calculate to see if the roll is a run
//This is a boolean method which contains an if statement
    public boolean alInOrder(){
        boolean trueOrNot;
        if  ((num[1] == (num[0]+1))&&
            (num[2] == (num[1]+1))){
                trueOrNot = true;
        }else{
                trueOrNot = false;
            }
        return trueOrNot;

    }

//This will calculate to see if the dice are all different  
//This is a boolean method which contains an if statement  
    public boolean allDifferent(){
        boolean trueOrNot;
        if  ((num[0] != (num[1]))&&(num[1] != (num[2]))&&(num[2] !=    (num[0]))){
                trueOrNot = true;
        }else{
                trueOrNot = false;
        }
        return trueOrNot;
    }


//This will sort out the numbers of the array in order  
    public void order(){
        Arrays.sort(num);
    }

    }
import java.util.Arrays;
import java.util.Random;

public class Dicegame2  {

//variables
private int numberOfRounds;
private int Dice1;
private int Dice2;
private int Dice3;
private Dicegame1[] match;

//constructor
public Dicegame2(int nOfRounds) {

    numberOfRounds = nOfRounds;
    match = new Dicegame1 [numberOfRounds];

}
    public void diceValue() {
        int[] num = new int[3];
        for(int i=1;i<numberOfRounds;i++){
            Dice1 = 1+(int)(6*Math.random());
            Dice2 = 1+(int)(6*Math.random());
            Dice3 = 1+(int)(6*Math.random());
            match [i] = new Dicegame1(num[0], num[1], num[2]);
        }   

}

//This will calculate the sum of dice if all the same       
    public int sumOfDiceSame(){
        int calc1;
        int[] num = new int[3];
        calc1 =((num[0]+num[1]+num[2])+60);
        return calc1;
    }
 //This will calculate the sum of dice if its a pair
    public int sumOfDicePair(){
        int calc2;
        int[] num = new int[3];
        calc2 =((num[0]+num[1]+num[2])+20);
        return calc2;
    }
//This will calculate the sum of dice if its a run
    public int sumOfDiceRun(){
        int calc3;
        int[] num = new int[3];
        calc3 =((num[0]+num[1]+num[2])+40);
        return calc3;
    }       
 //This will calculate the sum of dice if all different
    public int sumOfDiceDifferent(){
        int calc4;
        int[] num = new int[3];
        calc4 =(num[0]+num[1]+num[2]);
        return calc4;
    }
}

正如我在代码中看到的,您正在将NUM数组打印到未设置的main中

其次,根本不需要阵列

你有类似骰子游戏2的东西。根据我所看到的,我将在这里创建额外的方法来返回数组匹配

我也不明白这门课的内容。我会在游戏1中求和,但我会从中删除新的int[3]


这只是指导方针。读取变量作用域和for循环。

new Dicegame1num[0],num[1],num[2];:这些值被传递到Dicegame1,它不能更改num,因为Java没有输出参数/引用调用。只有通过num,才能更改num[0]等等。我不明白如何通过num。老实说,我不知道你的意思