代码没有经过彻底处理就完成了

Code just finishes without thoroughly processing

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

    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;
      }
   }
}

这是我的骰子 class。

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;
   }
}

首先改变如下并尝试

 while (input == "yes")

 while (input.equals("yes"))

这是您比较返回值的方式,应该会导致您的代码不会在 while 循环内执行。当您应该使用 equals 方法时,您正在与 == 进行比较。 此外,为了安全起见,您应该将返回的字符串转换为小写字母(如果您愿意,也可以是大写字母),如下所示:

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