JOptionPane在输入后关闭,但应打开第二个输入对话框

问题描述 投票:1回答:2

我正在尝试创建一个代码,要求用户输入3个单独的测试分数。我正在使用JOptionPane进行对话和输入。在询问我的第一个价值之后,它应该打开另一个价值并要求我的第二个价值等等。而不是这样做它关闭,并没有提供第二个对话框。

这是我的代码。

import java.util.Scanner;
import javax.swing.JOptionPane;

public class GradeTest
{
    public static void main(String[] args)
    {
        int Value1;
        int Value2;
        int Value3;
        int average;

        Scanner keyboard = new Scanner(System.in);

        JOptionPane.showInputDialog("What is your first value?");
        Value1 = keyboard.nextInt();

        JOptionPane.showInputDialog("What is your second value?");
        Value2 = keyboard.nextInt();

        JOptionPane.showInputDialog("What is your second value?");
        Value3 = keyboard.nextInt();

        average = Value1 + Value2 + Value3;
        average = average / 3;

        JOptionPane.showMessageDialog(null, "The average grade is " 
                                            + average + "%.");

        if(average < 60)
        {
            JOptionPane.showMessageDialog(null, "The grade for " 
                                                + average + "% is an F.");
        }
        else
        {
            if(average < 70)
            {
                JOptionPane.showMessageDialog(null, "The grade for " 
                                                    + average + "% is a D.");
            }
            else
            {
                if(average <80)
                {
                     JOptionPane.showMessageDialog(null, "The grade for " 
                                                        + average + "% is a C.");
                }
                else
                {
                    if(average <90)
                    {
                        JOptionPane.showMessageDialog(null, "The grade for " 
                                                            + average + "% is a B.");
                    }
                    else
                    {
                        if(average <100)
                        {
                            JOptionPane.showMessageDialog(null, "The grade for " 
                                                                + average + "% is an A.");
                        }
                    }
                }
            }
        }
    }
}
java joptionpane
2个回答
0
投票

默认的System.in Scanner是命令行输入阅读器。如果将值设置为等于控制台输入,则对JOptionPane输入完全没有任何作用。您不应该将控制台程序与GUI混合使用。通过使用JOptionPane结果,您想要做的事情可能更像是这样。

...
int Value1, Value2, Value3, average;

Value1 = Integer.parseInt(JOptionPane.showInputDialog("What is your first value?"));

Value2 = Integer.parseInt(JOptionPane.showInputDialog("What is your second value?"));

Value3 = Integer.parseInt(JOptionPane.showInputDialog("What is your third value?"));

average = (Value1 + Value2 + Value3) / 3;

JOptionPane.showMessageDialog(null, "The average grade is " + average + "%.");
...

虽然这不在您的问题中,但您的if else条款非常长,您应该将它们缩短到这一点。

String avgGrade = "";
if (average < 60)
    avgGrade = "F";
else if (average < 70)
    avgGrade = "D";
else if (average < 80)
    avgGrade = "C";
else if (average < 90)
    avgGrade = "B";
else
    avgGrade = "A";

JOptionPane.showMessageDialog(null, "The grade for "+ average + "% is "+avgGrade+".");

0
投票

JOptionPane.showInputDialog返回用户输入的字符串。使用Integer.parseInt方法将返回值转换为整数。您不应该使用等待来自控制台的用户输入的keyboard.nextInt()

© www.soinside.com 2019 - 2024. All rights reserved.