且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

没有所需的输出,可能的原因是过载

更新时间:2023-01-15 11:00:27

正如 Hot Licks 所建议的那样,这是您的问题:您正在使用 == 来检查字符串的等效性,而不是使用 ==code>equals(...) 或 equalsIgnoreCase(...) 方法.了解 == 检查两个 对象 是否相同,这不是您感兴趣的.另一方面,方法检查两个字符串是否相同字符以相同的顺序排列,这就是这里的重要性.所以代替

As Hot Licks suggests, that's your problem: you're using == to check for String equivalence rather than use either the equals(...) or the equalsIgnoreCase(...) method. Understand that == checks if the two objects are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead of

if (fu == "bar") {
  // do something
}

做,

if (fu.equals("bar")) {
  // do something
}

或者,

if (fu.equalsIgnoreCase("bar")) {
  // do something
}

另一个问题,与其使用大量 if 块,为什么不使用获胜矩阵来简单、轻松地简要检查一个选择是否优于另一个选择.枚举对此很有用,这样的事情可以工作:


Another issue, rather than having a chit-load of if blocks, why not use a win-matrix to simply, easily and briefly check to see if one choice beats another. Enums would be useful for this, something like this could work:

import java.util.Comparator;

public enum HandGameChoice  {
   ROCK,
   PAPER,
   SCISSORS,
   LIZARD,
   SPOCK;

   private static MyComparator myComparator = new MyComparator();

   public static int compare(HandGameChoice o1, HandGameChoice o2) {
      return myComparator.compare(o1, o2);
   }

   private static class MyComparator implements Comparator<HandGameChoice> {
      private int[][] winMatrix = {
            { 0, -1,  1,  1, -1},
            { 1,  0, -1, -1,  1},
            {-1,  1,  0,  1, -1},
            {-1,  1, -1,  0,  1},
            { 1, -1,  1, -1,  0}
      };

      @Override
      public int compare(HandGameChoice o1, HandGameChoice o2) {
         return winMatrix[o1.ordinal()][o2.ordinal()];
      }
   }
}

测试该枚举的类可能如下所示:

A class to test this enum could look like so:

public class TestHandGameChoices {
   public static void main(String[] args) {
      for (HandGameChoice choice1 : HandGameChoice.values()) {
         for (HandGameChoice choice2 : HandGameChoice.values()) {
            int value = HandGameChoice.compare(choice1, choice2);
            String result = "";
            if (value > 0) {
               result = "win";
            } else if (value < 0) {
               result = "lose";
            } else {
               result = "tie";
            }
            System.out.printf("%-8s vs %-8s: %s%n", choice1, choice2, result);
         }
      }
   }
}

测试类的输出显示:

ROCK     vs ROCK    : tie
ROCK     vs PAPER   : lose
ROCK     vs SCISSORS: win
ROCK     vs LIZARD  : win
ROCK     vs SPOCK   : lose
PAPER    vs ROCK    : win
PAPER    vs PAPER   : tie
PAPER    vs SCISSORS: lose
PAPER    vs LIZARD  : lose
PAPER    vs SPOCK   : win
SCISSORS vs ROCK    : lose
SCISSORS vs PAPER   : win
SCISSORS vs SCISSORS: tie
SCISSORS vs LIZARD  : win
SCISSORS vs SPOCK   : lose
LIZARD   vs ROCK    : lose
LIZARD   vs PAPER   : win
LIZARD   vs SCISSORS: lose
LIZARD   vs LIZARD  : tie
LIZARD   vs SPOCK   : win
SPOCK    vs ROCK    : win
SPOCK    vs PAPER   : lose
SPOCK    vs SCISSORS: win
SPOCK    vs LIZARD  : lose
SPOCK    vs SPOCK   : tie

然后 GUI 会像这样使用它:

Then the GUI would use it like so:

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

@SuppressWarnings("serial")
public class HandGameGui extends JPanel {
   private JTextArea tArea = new JTextArea(13, 40);

   public HandGameGui() {
      ButtonListener btnListener = new ButtonListener();
      JPanel btnPanel = new JPanel(new GridLayout(1, 0, 5, 0));
      for (HandGameChoice hgChoice : HandGameChoice.values()) {
         String choiceString = hgChoice.name();
         String initCapChoiceString = choiceString.substring(0, 1)
               + choiceString.substring(1, choiceString.length()).toLowerCase();

         JButton button = new JButton(initCapChoiceString);
         button.setActionCommand(choiceString);
         button.addActionListener(btnListener);
         btnPanel.add(button);
      }

      setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      setLayout(new BorderLayout(5, 5));
      add(new JScrollPane(tArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), BorderLayout.CENTER);
      add(btnPanel, BorderLayout.PAGE_END);
   }

   private class ButtonListener implements ActionListener {
      private Random random = new Random();
      @Override
      public void actionPerformed(ActionEvent e) {
         String actionCommand = e.getActionCommand();
         HandGameChoice userChoice = HandGameChoice.valueOf(actionCommand);
         int randomInt = random.nextInt(HandGameChoice.values().length);
         HandGameChoice aiChoice = HandGameChoice.values()[randomInt];

         int gameResult = HandGameChoice.compare(userChoice, aiChoice);
         String resultStr = "";
         if (gameResult > 0) {
            resultStr = "win";
         } else if (gameResult < 0) {
            resultStr = "lose";
         } else {
            resultStr = "tie";
         }

         String output = String.format("You chose %s, and the computer chose %s; you %s%n", 
               userChoice, aiChoice, resultStr);
         tArea.append(output);
      }
   }

   private static void createAndShowGui() {
      HandGameGui mainPanel = new HandGameGui();

      JFrame frame = new JFrame("Rock Paper Scissors Lizard Spock");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}