亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用一個類來隨機擲骰子

使用一個類來隨機擲骰子

函數式編程 2022-01-12 14:51:16
我正在編寫一個骰子游戲程序,它為用戶和計算機創建一個骰子,然后詢問用戶名、骰子的面數以及用戶想在計算機上玩多少次迭代。然后讓程序在每次擲骰后檢查哪個擲骰更高,并在迭代結束時寫回每個用戶和計算機的總獲勝次數以及獲勝者是誰。我遇到的問題是我需要創建一個roll()類來隨機化骰子并將其設置為用戶滾動和計算機滾動的值,我收到此錯誤...Exception in thread "main" java.lang.IllegalArgumentException: bound must be positiveat java.util.Random.nextInt(Random.java:388)at classwork6_3.Die.roll(Die.java:52)at classwork6_3.ClassWork6_3.main(ClassWork6_3.java:29)這是我的main()課...package classwork6_3;import java.util.*;public class ClassWork6_3 {public static void main(String[] args) {    Scanner s = new Scanner(System.in);    Random r = new Random();    System.out.print("Enter user's name: ");    String name = s.nextLine();    int value = 0;    int value2 = 0;    System.out.print("How many sides does the die have?: ");    int sides = s.nextInt();    s.nextLine();    System.out.print("How many times would you like to roll?: ");    int numRoll = s.nextInt();    int counter = 0;    int counter2 = 0;    Die user = new Die(name, sides, value);    Die comp = new Die(value);    for(int i = 1; i<= numRoll; i++){        value = r.nextInt(user.roll(sides)) + 1;        value2 = r.nextInt(comp.roll(sides)) + 1;        System.out.printf("%s rolled: %d\n", user.getOwner(), user.getValue());        System.out.print("Computer rolled: " + comp.getValue() + "\n\n");            if(value > value2){                counter++;            } else if(value2 > value){                counter2++;            }   }            System.out.printf("%s TOTAL WINS: %d\n", user.getOwner(), counter);            System.out.print("Computer TOTAL WINS: " + counter2 + "\n\n");                if(counter > counter2){                    System.out.printf("%s wins!!\n", user.getOwner());                }else if(counter2 > counter){                    System.out.print("Computer wins\n");                }else{                    System.out.print("It's a tie!\n");                }}}
查看完整描述

2 回答

?
森欄

TA貢獻1810條經驗 獲得超5個贊

以下是您的代碼的問題:


您永遠不會返回該roll(...)方法生成的隨機值


public int roll(int rand){

    rand = r.nextInt(value);    

    return value;

}

將其更改為


public int roll(int rand) {         

    return r.nextInt(rand);     

}

在 for 循環中,您只需調用該roll() 方法即可。由于它已經計算出骰子的隨機值,因此您無需r.nextInt()再次調用。


value = r.nextInt(user.roll(sides)) + 1;

value2 = r.nextInt(comp.roll(sides)) + 1;

將其更改為


value = user.roll(sides) + 1;

value2 = comp.roll(sides) + 1;

現在使用打印出值:


System.out.printf("%s rolled: %d\n", user.getOwner(), value);

System.out.print("Computer rolled: " + value2 + "\n\n");

執行上述步驟后,您的代碼將按預期工作。另外作為旁注,不要使用含義不明確的變量名,如value、value2等。


查看完整回答
反對 回復 2022-01-12
?
喵喵時光機

TA貢獻1846條經驗 獲得超7個贊

每當您制作一個新骰子時,您的骰子value永遠不會從零更改,從而導致該錯誤彈出,因為根據您的 Die 類,每當您調用 roll() 時它將查看的范圍是 0 到 0。更改value為用戶投入的價值并制造新的模具。


查看完整回答
反對 回復 2022-01-12
  • 2 回答
  • 0 關注
  • 201 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號