我目前正在为第一年的编程课程完成一项任务,老实说,编程并不是我最好的技能。所以对这个小问题的任何帮助都会非常感激。我为因为我的新生而堵塞网站而道歉。
该程序本质上是一个使用温度的数字猜谜游戏,它将来自JTextField的猜测(int guessTemperature)存储和排序到一个arraylist中并通过JOptionMessage显示,一旦你猜对了,最低的猜测,索引数组正确的猜测,所有猜测的平均值和华氏温度的转换温度。它在9到40之间,因为作业指定了40,而且我的学生ID上的编号最高。我没有包括avg或index的方法,因为无效的arraylist使它变得多余。
我遇到的麻烦是在arraylist中存储温度猜测,无论是正确的还是不正确的。我手动测试程序,字面上从9开始,然后一直工作到40个。所以如果我的arraylist运行正常,排序最低猜测温度后的输出应始终为9,因为这总是我开始的地方,但输出最低温度当前是随机计算的数字和正确的猜测。除非我的排序方法和找到最小方法不正确,否则arraylist除了最后一个之外没有填充任何猜测。因此,如果任何人可以向我推荐我的arraylist错误(或者如果一切都是错误的)它不会捕获每个猜测的实例,那将非常感激。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import javax.swing.*;
public class TemperatureFrame extends JFrame {
public JFrame mainFrame;
public JLabel prompt1, prompt2;
public JTextField userInput;
public JLabel comment;
public JButton restart;
public int randomTemperature;
public int min = 9;
public int max = 40;
public int guessTemperature;
public int sumTemperature;
public double avgTemperature;
public int lowestTemperature;
public int convertedTemperature;
public int indexTemperature;
public Color background;
public ArrayList<Integer> arList;
public TemperatureFrame() {
super("Temperature Guess/Conversion Application");
prompt1 = new JLabel("Randomly generated temperature is between 9 and 40." );
prompt2 = new JLabel("Write temperature (your guess) or -1 (for exit) and press enter key:");
userInput = new JTextField(5);
userInput.addActionListener(new GuessHandler());
comment = new JLabel("The results will be shown here.");
restart = new JButton("Start Again - Generate A New Temperature");
restart.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
userInput.setText("");
comment.setText("The results will be shown here.");
RandomTemperature();
userInput.setEditable(true);
}
});
setLayout(new FlowLayout());
background = Color.LIGHT_GRAY;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 150);
setLocationRelativeTo(null);
setVisible(true);
setResizable(false);
add(prompt1);
add(prompt2);
add(userInput);
add(comment);
add(restart);
RandomTemperature();
ConvertTemperature();
}
public void RandomTemperature() {
Random random = new Random();
randomTemperature = random.nextInt(max - min) + min;
}
public void ConvertTemperature() {
convertedTemperature = randomTemperature * 9 / 5 + 32;
}
class GuessHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
{
arList = new ArrayList<>();
String str;
str = userInput.getText().toString();
guessTemperature = Integer.parseInt(str);
arList.add(guessTemperature);
{
if (guessTemperature > randomTemperature) {
comment.setText("Temperature guessed is higher than the random temperature.");
userInput.setText("");
userInput.setEditable(true);
}
if (guessTemperature < randomTemperature) {
comment.setText("Temperature guessed is lower than the random temperature.");
userInput.setText("");
userInput.setEditable(true);
}
if (guessTemperature == randomTemperature) {
Collections.sort(arList);
lowestTemperature = Collections.min(arList);
comment.setText("Temperature guessed is equal to the random temperature.");
JOptionPane.showMessageDialog(null, "Temperature guessed is equal to the random temperature.\n\n1. Lowest temperature is:" + lowestTemperature + "\n2. Average temperature is:" + avgTemperature + "\n3. Array index of correctly guessed temperture is:" + indexTemperature + "\n4. Temperature in Farenheit is:" + convertedTemperature + "\n\nThank you for playing!");
}
else if (guessTemperature == -1) {
System.exit(0);
}
}
}
}
}
}
答案 0 :(得分:0)
您可能希望将arList = new ArrayList<>();
移动为处理程序类的实例变量。现在,由于arraylist在actionPerformed方法中初始化,因此每次用户单击按钮时都会重新初始化。
(只是放眼了一下该程序。我可能错了)
答案 1 :(得分:0)
只是快速浏览一下,但我认为问题在于每次执行动作时都会创建一个新的ArrayList。