我必須創建一個程序,用戶可以在其中輸入所需的總和,然後滾動兩個六面骰子,直到它們的總和爲期望的總和。我知道a)我必須使用Scanner,因爲它是一個交互式程序。 b)我必須使用無限循環來解決包括使用隨機數的問題。 c)我不指望你像我只是複製和粘貼一樣給我答案。我只是想引導做什麼,以便我的代碼實際編譯。Java初學者:掃描儀控制檯和兩個6面骰子問題
這裏是我的代碼:
import java.util.*;
public class Game {
public static void main(String[] args) {
System.out.println("Try rolling two six-sided dices");
System.out.println("until their sum is your");
System.out.println("desired sum.");
System.out.println();
Scanner console = new Scanner (System.in);
Random r = new Random();
int sum = 0;
int tries = 0;
While (sum != number) {
int roll1 = rand.nextInt(6) +1;
int roll2 = rand.nextInt(6) +1;
sum = roll1 + roll2;
tries++;
}
}
System.out.println("Desired dice sum: ");
int number = console.nextInt();
*我不斷收到這3個編譯錯誤:
Game.java:21: ';'預期雖然 (sum!= number){Game.java:29:expected
System.out.println(「Desired dice sum: 」); ^ Game.java:29:非法啓動類型
System.out.println(「Desired dice sum: 」); ^ 3個錯誤
編輯:
仍然得到3個編譯錯誤
進口的java.util。*; (「試着滾動兩個六面骰子」); System.out.println(「直到他們的總和是你的」); System.out.println(「期望的總和」); System.out.println();
Scanner console = new Scanner (System.in);
Random r = new Random();
System.out.println("Desired dice sum: ");
int number = console.nextInt();
int sum = 0;
int tries = 0;
while (sum != number) {
int roll1 = rand.nextInt(6) +1;
int roll2 = rand.nextInt(6) +1;
sum = roll1 + roll2;
tries++;
}
}
}
@馬特:
這是我應該有輸出,對不起,沒有說清楚:
Desired dice sum: 9
4 and 3 = 7
3 and 5 = 8
5 and 6 = 11
5 and 6 = 11
1 and 5 = 6
6 and 3 = 9
更正。謝謝。 – Makurian1 2010-10-26 03:37:29
@ Makurian1:看我的編輯。有用嗎? – 2010-10-26 03:43:43
我試過了,得到0錯誤。謝謝! – Makurian1 2010-10-26 03:49:54