我只是从Java和程序设计开始。有人可以向我解释为什么在输入第一个对话框的信息后第二个对话框不会显示吗?

谢谢!

// Java Practice

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

public class DialogTest
{
    public static void main(String [] args)
    {
        Scanner keyboard = new Scanner(System.in);

        String firstname;
        String lastname;
        int age;

        JOptionPane.showInputDialog("What is " +
                                    "your first name?");
        firstname = keyboard.nextLine();

        JOptionPane.showInputDialog("What is " +
                                    "your last name?");
        lastname = keyboard.nextLine();

        JOptionPane.showInputDialog("How old are you?");
        age = keyboard.nextInt();

        JOptionPane.showMessageDialog(null, "I see, so your name is: " + firstname + lastname + " and you are" + age + " years old.");

        System.exit(0);
    }
}

最佳答案

JOptionPane.showInputDialog()返回一个String,其中包含用户输入的值。代替使用Scanner类,将方法调用的返回值存储在变量中:

String firstname, lastname, age;

firstname = JOptionPane.showInputDialog("What is " +
                                "your first name?");

lastname = JOptionPane.showInputDialog("What is " +
                                "your last name?");

age = JOptionPane.showInputDialog("How old are you?");

JOptionPane.showMessageDialog(null, "I see, so your name is: " + firstname + lastname + " and you are" + age + " years old.");

关于java - 第二个JOptionPane不会出现,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39236725/

10-15 17:16