我会说实话,我一直在做一项任务而且我被困了。我正在创建一个简单的Java小程序,让用户输入10个数字。该程序清理该输入,确保只输入一个数字,并且该数字必须介于0和9之间。
到目前为止没有问题。使用给定的示例,在Web上搜索正确的语法和逻辑。我的代码编译没有抱怨。然而,一旦我尝试了我的第一个号码,只要它的有效输入,我的程序只是上升并告诉我我输入的号码是最大的,并且不等待输入10个号码。我的猜测是我有半个applet程序,我输出不正确,或者我的循环中的某些内容是错误的,但逻辑似乎很好。
几乎忘了提一下。我不知道如何在applet中创建输出文本,任何帮助都会很棒,但这不是我的主要关注点。我目前的代码:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class largest extends Applet implements ActionListener{
private static final long serialVersionUID = 1L;
//Create components for Applet
Label numberLabel = new Label("Enter a number:");
TextField numberField = new TextField(10); // Sets the size of the text field, You still may type as much data into the field that you want, input will need to be sanitized later
Label topNumberLabel = new Label("The top number is:");
Button calcButton = new Button("Ok");
public void init()
{
// Add controls to the applet window
add(numberLabel);
add(numberField);
add(topNumberLabel);
add(calcButton);
calcButton.addActionListener(this);
setSize(300, 200); // Sets the size of the applet window
}
public void actionPerformed(ActionEvent e) {
// Variables for counting, user input, and storage for largest number
double counter = 0;
double number = 0;
double largest = 0;
// Allows user to input 10 times
while (counter<10)
{
try { //Sanitize user input, make sure input entered is a number
number = Double.parseDouble(numberField.getText());
} catch (NumberFormatException ex) {
numberField.requestFocus();
JOptionPane.showMessageDialog(null, "Input invalid, please enter an integer",
"///-D-A-T-A---E-R-R-O-R-\\\\\\", JOptionPane.ERROR_MESSAGE);
return;
}
if (number < 0 || number > 9) { //Sanitize user input, make sure the given number is between 0 and 9
numberField.requestFocus();
JOptionPane.showMessageDialog(null,
"The number entered must be between 0 and 9",
"///-D-A-T-A---E-R-R-O-R-\\\\\\", JOptionPane.ERROR_MESSAGE);
return;
}
counter++;
// Determine what the largest number entered is by comparing to a baseline
// of previous numbers or zero if just beginning loop
if (number > largest)largest=number;
}
// Display's the largest number that got entered by user
JOptionPane.showMessageDialog(null," The largest number entered was " + largest);
}
}
答案 0 :(得分:0)
这是因为这一行
number = Double.parseDouble(numberField.getText());
一旦你在字段中输入了一个数字,就完成了10次,你就没有时间在循环结束之前改变字段中的数字。
答案 1 :(得分:0)
您按预期循环了10次,但每次只是使用getText()
获取字段值。这不等待用户更改号码,因此所有10个号码都是相同的。您必须将counter
和largest
定义为字段,并在每次调用actionPerformed
时进行更改。这是代码:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class largest extends Applet implements ActionListener{
private static final long serialVersionUID = 1L;
private int counter = 0; // These are the variables
private double largest = 0; // from actionPerformed().
// ...
public void actionPerformed(ActionEvent e) {
// Variable for user input
double number = 0;
try { //Sanitize user input, make sure input entered is a number
number = Double.parseDouble(numberField.getText());
} catch (NumberFormatException ex) {
numberField.requestFocus();
JOptionPane.showMessageDialog(null,
"Input invalid, please enter an integer",
"///-D-A-T-A---E-R-R-O-R-\\\\\\",
JOptionPane.ERROR_MESSAGE);
return;
}
if (number < 0 || number > 9) { //Sanitize user input, make sure the given number is between 0 and 9
numberField.requestFocus();
JOptionPane.showMessageDialog(null,
"The number entered must be between 0 and 9",
"///-D-A-T-A---E-R-R-O-R-\\\\\\",
JOptionPane.ERROR_MESSAGE);
return;
}
counter++;
// Determine what the largest number entered is by comparing to a baseline
// of previous numbers or zero if just beginning loop
if (number > largest)largest=number;
// Allows user to input 10 times
if (counter==10)
{
// Display's the largest number that got entered by user
JOptionPane.showMessageDialog(null,
"The largest number entered was " + largest);
}
}
}