-2

このプログラムは、10 個の 1 桁の数字を受け入れ、入力された最大の数字を出力することになっています。プログラムは正常に動作しますが、数値を入力するたびに出力画面が表示されます。出力は正しいです。10個すべてが入力された後にのみ出力画面を表示する方法がわかりません。

package largest;
import javax.swing.*;

public class Largest {

// Main method
public static void main(String[] args) {
    // Declare variables
    int largest = 0;
    int counter = 0;
    int number = 0;

    // Condition statement to repeat loop until 10 digits are entered
    while (counter < 10) {
        // Prompt user for input
        String input = JOptionPane.showInputDialog(null,
                "Enter a number between 0 and 9: ");

        // Try-Catch statements to check and handle format errors
        try {
            number = Integer.parseInt(input);
        } catch (NumberFormatException e) {
            number = -1; // Triggers the error message
        }

        // Checks to make sure number is a single digit 
        if (number >= 0 && number < 10) {
            // Determines if the number entered is the largest
            if (number > largest) {
                largest = number;

            }
            // Increases counter variable by 1 with a valid entry
            counter++;
        }
        // Display error message
        else {
            JOptionPane.showMessageDialog(null,
                    "Your entry was not a single digit, please re-enter.",
                    "Error", JOptionPane.ERROR_MESSAGE);
        }

        // Display the largest number
        JOptionPane.showMessageDialog(null,
                "The largest number entered is: " + largest, "Results",
                JOptionPane.INFORMATION_MESSAGE);

    }

}

}
4

2 に答える 2

0

ループJOptionPaneの外に入力された最大数を示すステートメントを移動する必要がありますwhile

while (){
    // code...
}

// Display the largest number
JOptionPane.showMessageDialog(null,
            "The largest number entered is: " + largest, "Results",
            JOptionPane.INFORMATION_MESSAGE);
于 2013-09-12T01:07:52.187 に答える
0

毎回出力を評価しないでください。

//First get all ten numbers:
int[] numbers = new int[10]; //array of int to store 10 numbers
int counter=0;
while(counter<10){
String input = JOptionPane.showInputDialog(null,
                "Enter a number between 0 and 9: ");

        // Try-Catch statements to check and handle format errors
        try {
            number = Integer.parseInt(input);
        } catch (NumberFormatException e) {
            number = -1; // Triggers the error message
        }

        // Checks to make sure number is a single digit 
        if (number >= 0 && number < 10) {
            numbers[counter]=number;
            counter++;
        }
        // Display error message
        else {
            JOptionPane.showMessageDialog(null,
                    "Your entry was not a single digit, please re-enter.",
                    "Error", JOptionPane.ERROR_MESSAGE);
        }
}

//Now After that loop evaluate your largers number
int max = 0;     
while(counter <10)
{
if(numbers[counter] > max )
{
max == numbers
}
counter++;
}

// Display the largest number
        JOptionPane.showMessageDialog(null,
                "The largest number entered is: " + max , "Results",
                JOptionPane.INFORMATION_MESSAGE);
于 2013-09-12T01:11:28.243 に答える