0

老实说,我一直在做一项任务,但我已经被困住了。我正在创建一个简单的 Java 小程序,让用户输入 10 个数字。程序会清理该输入,确保只输入一个数字,并且该数字必须在 0 到 9 之间。

到目前为止没有问题。使用给定的示例,在网上搜索正确的语法和逻辑。我的代码编译没有抱怨。但是,一旦我尝试了我的第一个数字,只要它的输入有效,我的程序就会启动并告诉我刚刚输入的数字是最大的,并且不会等待输入 10 个数字。

我的猜测是我有半个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);
    }

}

4

2 回答 2

0

因为这条线

number = Double.parseDouble(numberField.getText());

在字段中输入数字后立即执行 10 次,在循环完成之前您将没有时间更改字段中的数字。

于 2013-09-22T10:00:24.880 回答
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);
        }
    }
}
于 2013-09-22T10:03:36.893 回答