【问题标题】:Code just finishes without thoroughly processing代码在没有彻底处理的情况下完成
【发布时间】:2023-03-04 15:21:01
【问题描述】:

这可能是初学者的错误,因为我是 Java 和一般编程的新手。无论如何,我认为这是 System.exit(0) 在我的代码中的位置,但是当我将其切换到其他地方时出现错误,所以我确定它是正确的。我的所有代码都是询问第一个 JOptionPane.showInputDialog。当我输入“是”作为响应时,我的代码刚刚完成处理,不执行任何其他操作。是什么导致我的代码只是结束而不是处理骰子事件?

    import javax.swing.JOptionPane;

/**
   The Fishing class simulates a game of fishing.
*/

public class Fishing
{
   public static void main(String[] args)
   {
      String input = JOptionPane.showInputDialog("Would you like to play a round " +
                                                    "of fishing? Enter yes or no?"); 
      while (input == "yes")
      {
         int score = 0;   // Sets player's score to 0
         int points = 0;      // Holds player's points
         final int DIE_SIDES = 6;    // # of sides for the die

         //Create an instance of the Die class
         Die die = new Die(DIE_SIDES);

         //Roll the die once and store value in result
         die.roll();
         int result = die.getValue();

         //Call getScore method and pass points and result
         getScore(points, result);

         //Keeps running total of player's score
         score = score + points;
      }

      System.exit(0);
   }


   /**
      The getScore method will calculate the player's score
      depending on what the player rolled. It will also show
      a message and return the score.
      @return A reference to an integer object containing
              the player's score for one roll.
   */

   public static int getScore(int points, int result)
   {
      if (result == 1)
      {
         JOptionPane.showMessageDialog(null, "Waaaaahhhhh, you have caught " +
                                       "a shark. Sharks are dangerous. You " +
                                            "have been awarded zero points.");
         points = 0;
         return points;
      }
      else if (result == 2)
      {
         JOptionPane.showMessageDialog(null, "You have caught a jellyfish. " +
                                  "This beautiful creature has awarded you " +
                                                               "50 points!!");
         points = 50;
         return points;
      }
      else if (result == 3)
      {
         JOptionPane.showMessageDialog(null, "You have caught an old boot. " +
                                "Maybe you can sell this old boot after it " +
                                 "dries out. You have been awarded 1 point.");
         points = 1;
         return points;
      }
      else if (result == 4)
      {
         JOptionPane.showMessageDialog(null, "You have caught an Alaskan salmon. " +
                                "This delicious and popular fish has awarded you " +
                                                                    "75 points!!!");
         points = 75;
         return points;
      }  
      else if (result == 5)
      {
         JOptionPane.showMessageDialog(null, "You have caught a small fish. You " +
                                                   "have been awarded 20 points!");                                                                                
         points = 20;
         return points;
      }
      else
      {
         JOptionPane.showMessageDialog(null, "You have caught a treasure chest!! " +
                                  "It is filled with shining pieces of gold, and " +
                                            "you have been awarded 100 points!!!!");
         points = 100;
         return points;
      }
   }
}

这是我的模具课。

import java.util.Random;


/**
   The Die class simulates the rolling of a die.
*/

public class Die
{
   private int sides;    //number of sides
   private int value;    //The die's value

   /**
      The constructor performs an initial roll of the die.
      @param numSides The number of sides for this die.
   */

   public Die(int numSides)
   {
     sides = numSides;
     roll();
   }

   /**
      The roll mthod simulates the rolling of the die.
   */

   public void roll()
   {
      //Create a random object.
      Random rand = new Random();

      //Get a random value for the die.
      value = rand.nextInt(sides) + 1;
   }

   /**
      getSides method
      @return The number of sides for this die.
   */

   public int getSides()
   {
      return sides;
   }

   /**
      getValue method
      @return The value of the die.
   */

   public int getValue()
   {
      return value;
   }
}

【问题讨论】:

  • 我的更改是否有效?

标签: java performance optimization pass-by-reference


【解决方案1】:

首先更改如下并尝试

 while (input == "yes")

 while (input.equals("yes"))

【讨论】:

  • 解决了这个问题,非常感谢。两者有什么区别?我之前不得不在不同的情况下将其更改为这个。
  • 出于好奇,我尝试通过在循环中定义输入来调整代码以使用 do-while 循环,但它不起作用。前任。 String input = JOption...}while (input.equals("yes"). 它说它找不到符号,即使它被声明了。
【解决方案2】:

您比较返回值的方式应该是导致您的代码不会在while 循环内执行。当您应该使用equals 方法时,您正在与== 进行比较。 此外,您应该像这样将返回的字符串转换为小写(或大写,如果您愿意)来保证安全:

while (input.toLowerCase().equals("yes"));

【讨论】:

  • 字符串始终是字符串,不涉及转换,但是 == 首先比较它们是否是同一个对象,而它们不是,所以,如果你使用覆盖(默认情况下在String 类) equals 方法它实际上比较了 String 对象的内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-10
  • 2021-05-18
  • 2012-03-21
  • 1970-01-01
相关资源
最近更新 更多