检查多个字符串

时间:2016-06-16 05:20:22

标签: java applet awt

我正在尝试制作一个你必须猜测一个数字的游戏,我决定在游戏中添加复活节彩蛋,但每当我尝试检查复活节彩蛋时,它只适用于最后一个

public class Guess extends Applet implements ActionListener
{
    util u = new util(); //Utility class
    int answer = u.rand(1,100); //Randomize answer
    String ansWord = Integer.toString(answer); //Convert Integer to String
    int winloss = 1; //Set winloss to display startup message
    TextField input; //Declare TextField
    boolean pizza,blazeIt; //Easter Eggs
    String response; //Declare Strings
    public void init()
    {
        input = new TextField(5);
        add(input);
        input.addActionListener(this);
    }

    public void paint(Graphics g)
    {
        g.drawString(""+winloss,10,20);
        switch(winloss)
        {
            case 1:g.drawString("Guess What Number I Am Thinking Of",getWidth()/2-100,50);
            break;
            case 2:g.drawString("How did you know?!?!?!",getWidth()/2-50,50);
            break;
            case 3:g.drawString("Nope",getWidth()/2-5,50);
            break;
            case 4:pizza = true;
            break;
            case 5:blazeIt = true;
            break;
            default:g.drawString("Broken",10,10);
            break;
        }
        easterCheck(g);
    }

    public void actionPerformed(ActionEvent e)
    {
        response = String.valueOf(input.getText());
        if(response.equals(ansWord))
        {
            winloss = 2;
        }

        else
        {
            easter("420",5); //Does not work
            easter("Pizza",4); //Works
        }
        repaint();
    }

    public void easter(String wordInput, int output)
    {
        if(response.equals(wordInput))
        {
            winloss = output;
        }
        else
        {
            winloss = 3;
        }
    }

    public void easterCheck(Graphics g)
    {
        if(pizza)
        {
            g.drawString("Delicious!",10,getHeight()-10);
        }
        if(blazeIt)
        {
            g.drawString("Blaze it!",10,getHeight()-20);
        }
    }

}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

因为每次打完复活节后(" 420",5),你都会在下一次打电话给复活节("比萨",4)。因此,如果输入是" 420",您知道它的字符串,并设置winloss = 4。但在那之后,在复活节内("比萨",4),你比较你的输入(" 420")和#34;比萨",你重置winloss = 3。这就是你的代码无法运作的方式。

public class Guess extends Applet implements ActionListener
{
  util u = new util(); //Utility class
  int answer = u.rand(1,100); //Randomize answer
  String ansWord = Integer.toString(answer); //Convert Integer to String
  int winloss = 1; //Set winloss to display startup message
  TextField input; //Declare TextField
  boolean pizza,blazeIt; //Easter Eggs
  String response; //Declare Strings
  public void init()
  {
    input = new TextField(5);
    add(input);
    input.addActionListener(this);
  }

  public void paint(Graphics g)
  {
    g.drawString(""+winloss,10,20);
    switch(winloss)
    {
      case 1:g.drawString("Guess What Number I Am Thinking Of",getWidth()/2-100,50);
        break;
      case 2:g.drawString("How did you know?!?!?!",getWidth()/2-50,50);
        break;
      case 3:g.drawString("Nope",getWidth()/2-5,50);
        break;
      case 4:pizza = true;
        break;
      case 5:blazeIt = true;
        break;
      default:g.drawString("Broken",10,10);
        break;
    }
    easterCheck(g);
  }

  public void actionPerformed(ActionEvent e)
  {
    response = String.valueOf(input.getText());
    if(response.equals(ansWord))
    {
      winloss = 2;
    } else if (response.equals("420")) {
      winloss = 5;
    } else if (response.equals("Pizza")) {
      winloss = 4;
    } else {
      winloss = 3;
    }
    repaint();
  }

  public void easterCheck(Graphics g)
  {
    if(pizza)
    {
      g.drawString("Delicious!",10,getHeight()-10);
    }
    if(blazeIt)
    {
      g.drawString("Blaze it!",10,getHeight()-20);
    }
  }

}