我目前正在为第一年的编程课做作业,老实说,编程并不是我最擅长的技能。因此,如果能为这个小问题提供任何帮助,我们将不胜感激。我很抱歉因为我的新手而堵塞了网站。
该程序本质上是一个使用温度的数字猜测游戏,它将JTextField中的猜测(int guessTemperature)存储并排序到一个数组列表中,并通过JOptionMessage显示,一旦你猜对了,最低的猜测、正确猜测的索引数组、所有猜测的平均值以及以华氏度为单位的转换温度。它在9到40之间,因为作业指定了40和我的学生ID上的最高数字。我没有包括平均值或索引的方法,因为不起作用的arraylist使它变得多余。
我遇到的问题是将温度猜测存储在数组列表中,无论是正确的还是不正确的。我手动测试程序,从9点开始,一直到40点。所以,如果我的数组列表运行正常,那么对猜测的最低温度进行排序后的输出应该总是9,因为这总是我的起点,但当前最低温度的输出是随机计算的数字和正确的猜测。除非我的排序方法和findminimum方法不正确,否则数组列表中除了最后一个之外不会填充任何猜测。因此,如果有人能向我建议我的数组列表出了什么问题(或者如果一切都错了),它没有捕捉到猜测的每一个实例,我将不胜感激。
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.nn1. Lowest temperature is:" + lowestTemperature + "n2. Average temperature is:" + avgTemperature + "n3. Array index of correctly guessed temperture is:" + indexTemperature + "n4. Temperature in Farenheit is:" + convertedTemperature + "nnThank you for playing!");
}
else if (guessTemperature == -1) {
System.exit(0);
}
}
}
}
}
}
您可能希望将arList = new ArrayList<>();
作为处理程序类的实例变量进行移动。现在,由于arraylist是在actionPerformed方法中初始化的,所以每当用户单击按钮时,它都会被重新初始化。
(只是粗略地看了一下程序。我可能错了)
只是简单看了一下,但我认为问题是每次执行操作时都会创建一个新的ArrayList。