[英]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: 根据我的评论:
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.