如何在不同的類中使用我的main中的數組。我正在製作一款遊戲,我需要一次擲骰三次。然後我拿出每個值並進行比較,比如2/3的數字匹配。如果再添加到12則給玩家回鍋。 這是我想使用數組中的信息的類。我昨天晚上了解了陣列和循環,所以我真的不知道我在做什麼在java中的2個獨立的類中使用數組
import java.util.Scanner;
public class Game {
private double Bet;
private double Pot;
private double TotalPot;
public void Pot(){
Pot = 50;
}
public void inputBet() {
Scanner keyboard = new Scanner (System.in);
System.out.println("Please enter the current month in numerical format: ");
Bet = keyboard.nextDouble();
if (Bet <= Pot) {
System.out.println("Error, Bet out of range");
inputBet();
}
else if (Bet == 0) {
System.out.println("Thank you for playing");
}
}
public void inputEnd(){
}
public void removeBet(){
TotalPot = Bet - Pot;
}
public void dieComparison1(){
if ((die[0] == die[1]) || (die[0] == die[2])){
TotalPot = (Bet * 2) + Pot;
}
}
public void print(){
System.out.println(+ TotalPot);}
}
這是我的主要創建數組的地方。
public class Assign3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Game smith = new Game();
//smith.Pot();
//smith.inputBet();
int[] die = new int[3];
Die bob = new Die();
int total = 0;
for(int i = 0; i < 3; i++){
die[i] = bob.rollDie();
bob.displayDie(die[i]);
total = total + die[i];
}
bob.displayDie(total);
}
}
你的問題不清楚:從主'()'你調用的方法,如'rollDie()'和' displayDie()'但你不會向我們展示他們的實現。此外,你不解釋你想要達到什麼目的,數組的作用是什麼等等。 – alfasin
你的'inputBet'方法要求用戶*「請用數字格式輸入當前月份:」 *但投入需要投注。你是否從某處複製了代碼並試圖修改它以實現程序的預期結果? – Blip
如果你想以某種方式將die數組放入Game類中,那很簡單。嘗試這樣的: '遊戲遊戲=新遊戲(死);' 遊戲構造函數看起來像這樣: 'public Game(int [] dieArray){}' 有意義嗎? –