簡體   English   中英

當我運行我的代碼(在 Eclipse 中)時,為什么我沒有得到任何 output?

[英]Why don't I get any output when I run my code (in Eclipse)?

Eclipse 沒有顯示我做錯了什么,但是當我點擊運行時,它的控制台只是閃爍並保持空白。

誰能指出我做錯了什么?

我評論了大部分代碼,但如果你在試圖弄清楚我在做什么時遇到困難:

  • 電腦玩家和用戶擲骰子。 兩個骰子都是從 1 到 6 的隨機數。
  • 有10輪。 誰贏了一輪,誰就得一分。
  • 最后,所有分數相加,並宣布獲勝者。
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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM