代码刚刚完成而没有彻底处理

时间:2016-03-07 02:10:09

标签: java performance optimization pass-by-reference

这可能是一个初学者错误,因为我不熟悉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;
      }
   }
}

这是我的骰子。

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

2 个答案:

答案 0 :(得分:0)

首先改变如下并尝试

 while (input == "yes")

 while (input.equals("yes"))

答案 1 :(得分:0)

这是比较返回值的方式,该值应该导致您的代码不在while循环内执行。您应该使用==方法时与equals进行比较。 另外,你应该通过将返回的字符串转换为小写字母(如果你愿意,也可以是大写字母)来安全地播放:

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