我剛開始使用java,所以這可能會非常簡單,但我們班有4人無法弄清楚這段代碼有什麼問題。我擔心這可能是掃描器和隨機命令之間的重疊問題,因爲.nextInt()是用於獲取新輸入並創建0到n之間的隨機變量的命令。此外,我相當新的Stackoverflow,所以如果有任何程序即時打破,我應該嘗試在未來適應,ID很樂意採取你的建議。您可以在同一個類中使用.nextInt()來處理Java.util.Random和Java.util.Scanner嗎?
感謝您提前幫助...乾杯
import java.util.Scanner;
import java.util.Random;
public class Question4 {
public static void main(String arg[]){
//-----------------------------
int numdice;
int sides;
int count = 1;
int score;
int total = 0;
boolean loop = false;
Scanner scan = new Scanner(System.in);
Random random = new Random();
System.out.print("Enter how many dice you would like to roll:");
numdice = scan.nextInt();
System.out.println("Enter how many sides to the dice there will be:");
sides = scan.nextInt();
System.out.println("You have rolled " + numdice + " dice with " + sides + " sides and have received the following values for each roll:");
while (!loop) {
if (numdice <= count){
score = random.nextInt(sides -1) +1;
total += score;
System.out.print(score);
++count;
}
else
{
System.out.println();
System.out.print(" your total value for your dices rolls are " + total);
loop = true;
}
}
System.out.println();
//-----------------------------
}
}
編輯:症狀是程序運行,但不創建/打印爲隨機變量的任意值。該程序將打印else語句,但它聲稱總值= 0.根據Eclipse,程序中沒有錯誤或錯誤。也感謝你關於空間的注意事項。
你們是對的,照顧它。多麼愚蠢。塗料。謝謝seth和paku!我會評價你,但它不會讓我。不管有沒有美好的一天!
是的,你可以把不是使用進口,充分導入包與訪問在你使用它的特定上下文的代碼行。 –
有什麼症狀?當你嘗試運行它時會發生什麼樣的可見問題?如果您收到錯誤消息,請將其複製/粘貼到您的問題中。你當然可以, – user2357112
,Y不?請提供錯誤堆棧! – Rugal