【问题标题】:Why is my random number generator not producing an output? [closed]为什么我的随机数生成器不产生输出? [关闭]
【发布时间】:2017-03-03 00:06:44
【问题描述】:

因此,对于我的 java 程序,我需要编写一个带有 if else 语句的 while 循环,该语句允许人们输入季节的猜测,该猜测是通过将季节分配给从 1 到 4 生成的数字而随机生成的. 当我编译时我没有得到任何错误,但是当我执行程序时,我得到的所有猜测都是零。在数字生成中我缺少什么吗?还是在字符串比较中?

编辑:代码现在按预期工作。我明白我做错了什么。感谢那些帮助过的人。

这里是代码和需要的变量:

public class SeasonsGenerator {
     public static void main(String[] args){

        //Variables for seasons counter 
        int summerSeasonCounter=0;
        int fallSeasonCounter=0;
        int springSeasonCounter=0;
        int winterSeasonCounter=0;
        int number;
        int limit = 5;
        Scanner keyboard = new Scanner(System.in);

        int count = 0;       // Counter for total number of iterations or loops
        String season = "";//Variable for season    
        final int NUMBER = 15;  // Number of while loop iterations
        String userGuess;//save user guess here


       // A random number generator
       Random generator = new Random();

       while(count < NUMBER)
       {
         number = generator.nextInt(limit);
         if(number == 1)
         {
          season = "summer";
         }
        else if(number == 2)
         {
          season = "spring";
         }
         else if(number == 3)
        {
          season = "fall";
        }
         else if(number == 4)
        {
         season = "winter";
        }

        System.out.print("Guess the season generated by the random number generator:");
        userGuess = keyboard.nextLine();
        userGuess.equalsIgnoreCase(season);

        if(season == "summer")
         {
        summerSeasonCounter +=1;
        }
        else if(season == "spring")
        {
          springSeasonCounter += 1;
        }
        else if(season == "fall")
        {
          fallSeasonCounter += 1;
        }
        else if(season == "winter")
        {
          winterSeasonCounter += 1;
        }


    // Display the results
       System.out.println ("You guessed Summer season correctly     "+summerSeasonCounter+" number of times.");
       System.out.println ("You guessed Fall season correctly "+fallSeasonCounter+" number of times.");
       System.out.println ("You guessed Spring season correctly "+springSeasonCounter+" number of times.");
       System.out.println ("You guessed Winter season correctly "+winterSeasonCounter+" number of times.");

    }
}

【问题讨论】:

标签: java if-statement random string-comparison


【解决方案1】:

您有很多问题,我建议您通过调试器运行代码以帮助您发现错误。一些明显的错误是

  • nextInt(4) 返回 0 到 3。
  • season.equals("summer"); 将季节与“夏天”进行比较并返回 false。它不分配任何东西。
  • 你测试了四次if (userGuess.equalsIgnoreCase(season))。如果第一次为假,则四次全部为假。

您的直接问题是要分配一个变量,您需要使用=

season = "summer";

【讨论】:

  • 我将使用什么来将字符串文字summer分配给字符串变量season呢?是不是使用:
  • 季节=“夏天”;不正确?
  • @CodyBrody 我添加了一个如何使用=的示例
  • @CodyBrody 我不明白你的问题。能改一下吗?
  • 是的。我的理解是,要将字符串文字分配给字符串变量,不能使用 = 运算符。所以我认为这样做我错了吗?
猜你喜欢
  • 2018-11-23
  • 1970-01-01
  • 2014-11-17
  • 2021-02-07
  • 2012-09-24
  • 2019-11-28
  • 1970-01-01
  • 2015-06-03
  • 1970-01-01
相关资源
最近更新 更多