简体   繁体   English

如何从文本字段中提取信息并将其与以前的输入进行比较

[英]How to extract information from a textfield and compare it to the previous input

I'm in an entry level java class and have to design a program that has the user guess a random number between 1-1000. 我在入门级Java类中,必须设计一个程序,让用户猜测1-1000之间的随机数。 I know that I need a separate class to extract the information from the textfield, but how can i store each successive guess? 我知道我需要一个单独的类来从文本字段中提取信息,但是如何存储每个连续的猜测呢? I need to state "Warmer" or "Colder" if the guess is closer or further away than the previous attempt. 如果猜测比上一次尝试更近或更远,我需要声明“ Warmer”或“ Colder”。

private class Event implements ActionListener{
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == guessAgain)
        {

        }
        else if(e.getSource() == newGame)
        {

        }
        else if(e.getSource() == userGuess)
        {
            input = userGuess.getText();
            guess = Double.parseDouble(input);
        }

Something along these lines for the "colder"/"warmer" issue... 沿着这些思路解决“阴冷” /“温暖”的问题...

public static void main(String[] args) {

  int guess, prevGuess, prevMiss, miss, secretNumber;

  secretNumber = 99;

  prevGuess = 77;
  prevMiss = Math.abs(secretNumber - prevGuess);

  guess = 88;
  miss = Math.abs(secretNumber - guess);

  if     (miss > prevMiss) System.out.println("Colder");
  else if(miss < prevMiss) System.out.println("Warmer");
  else                     System.out.println("No change");

}

Absolute value of difference gives distance between. 差的绝对值给出了两者之间的距离。

As per my comment: 根据我的评论:

  • Give your class an int field, previousGuess, and have it hold the previous guess. 给您的班级一个int字段,previousGuess,并让它保存上一个猜测。
  • When the button is pressed, get the input, convert it to an int and put it into an int local variable, say called currentGuess. 当按下按钮时,获取输入,将其转换为int并将其放入int局部变量,即称为currentGuess。
  • You might want a boolean field as well, firstGuess, that is initially true, so you know when there is no previous guess. 您可能还需要一个布尔字段firstGuess,最初它是正确的,所以您知道什么时候没有以前的猜测。 Set this to false after checking if it is true or false and re-set it to true if you re-start the game. 在检查是否为true或false之后将其设置为false,如果重新启动游戏,则将其重新设置为true。
  • Simply compare the previousGuess with the currentGuess and with the solution number and based on these comparisons present the appropriate output to the user. 只需将previousGuess与currentGuess和解决方案编号进行比较,并根据这些比较向用户提供适当的输出。
  • Then assign currentGuess to previousGuess for the next go-around. 然后将currentGuess分配给previousGuess,以进行下一次解决。 ie, previousGuess = currentGuess; 即, previousGuess = currentGuess;

That's it. 而已。


Edit 编辑
per DSlomer64's comment: 根据DSlomer64的评论:

To determine whether to say "warmer" or "colder", use Math.abs() , the absolute value function to hold previous and current amount of miss. 要确定是说“变暖”还是“变冷”,请使用Math.abs()这个绝对值函数来保存之前和当前的未命中量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM