[英]When I run the given code I get A B A as output. I don't understand why it prints A again
[英]Why don't I get any output when I run my code (in Eclipse)?
Eclipse 沒有顯示我做錯了什么,但是當我點擊運行時,它的控制台只是閃爍並保持空白。
誰能指出我做錯了什么?
我評論了大部分代碼,但如果你在試圖弄清楚我在做什么時遇到困難:
import java.util.Random; // Imports RNG
public class Guarascio_Chap_4_21 {
// Calls for main
public static void main(String[] args) {
// Create Random Object to create random numbers
Random randomNumbers = new Random();
// List of variables
int number;
int compPlayer;
int userPlayer;
int compScore = 0;
int userScore = 0;
int numRolls = 10;
// For loop that begins at 1 and counts by 1 until it completes numRolls
for (number = 1; number >= numRolls; number++)
{
// CompPlayer rolls the dice, then prints the number
compPlayer = randomNumbers.nextInt(7)+1;
System.out.println("Computer rolls a "+ compPlayer);
// UserPlayer rolls the dice, then prints the number
userPlayer = randomNumbers.nextInt(7)+1;
System.out.println("User rolls a "+ userPlayer);
// If/Else loop that informs the user which player has won the roll, 1 point is added to winners score
if (compPlayer > userPlayer)
{
compScore = (compScore + 1);
System.out.println("Computer gains 1 point");
}
else
{
userScore = (userScore + 1);
System.out.println("User gains 1 point");
}
// Print out both users and comps score
System.out.println("Computers score is " + compScore);
System.out.println("Users score is " + userScore);
// Compare scores to decide winner
if (compScore > userScore)
{
System.out.println("Computer wins with " + compScore + " points!");
}
else
{
System.out.println("User wins with " + userScore + " points!");
}
}
}
}
您的 for 循環for (number = 1; number >= numRolls; number++)
中的條件number >= numRolls
始終為 false 。
將其更改為number <= numRolls
以執行循環並在控制台中獲取 output。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.