【问题标题】:Not returning what's intended不返回预期的内容
【发布时间】:2015-10-27 02:11:49
【问题描述】:

我无法打印出扫描的名字和姓氏(fname1lname1)。我必须创建 6 个对象,而这两个对象我似乎都无法开始。此外,如果我输入除“yes”或“y”之外的任何内容,它不会循环回到我在 sn-p 上方插入的单选按钮。我该如何解决这个问题?

这是在输出窗口中打印出来的内容:

[,0,0,0x0,invalid,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=java.awt.Dimension[width=350,height=200]]
public class Cabin_Selector
{
    public static void main(String[] args)
    {
        JFrame cabin_selection = new JFrame("Select Your Cabin"); //Prompts the user
        cabin_selection.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Close the Frame when exiting

        Project2_JoshuaLucas selection = new Project2_JoshuaLucas("", "", "", "", "", "", "", 0.00); //Call the constructor for the Project2_JoshuaLucas class
        cabin_selection.getContentPane().add(selection); //put the object in the current pane of the jframe

        cabin_selection.pack();  //size the frame
        cabin_selection.setVisible(true);  //make the frame visible

    }  //end main method
}  //end class

if (source == cabin1)
{
    cabin1.setBackground(Color.darkGray);
    cabin2.setBackground(Color.gray);
    cabin3.setBackground(Color.gray);
    cabin4.setBackground(Color.gray);
    cabin5.setBackground(Color.gray);
    cabin6.setBackground(Color.gray);
    cabin7.setBackground(Color.gray);
    cabin8.setBackground(Color.gray);
    cabin9.setBackground(Color.gray);
    cabin10.setBackground(Color.gray);
    suite1.setBackground(Color.red);
    suite2.setBackground(Color.red);

    System.out.println("Your choice is Cabin 11-1, would you like to designate this as your room?");
    info1 = scan_in.nextLine();
    info1 = info1.toLowerCase();

    if ( info1.equals ("yes") || info1.equals ("y"))
    {
        continues=true;   
        System.out.println("Please enter the number of people in your cabin (*Maximum number of people is 2*)");
        cabin_people = scan_in.nextInt();
        scan_in.nextLine();

        while(continues)
        {
            switch (cabin_people)
            {
            case 1:
                System.out.println("There is one passenger within the cabin. (You will pay an EXTRA 45% because of the empty passenger slot)");
                continues=false;
                onepassenger=true;
                break;
            case 2:
                System.out.println("There are two passenger within this cabin.");
                continues=false;
                twopassenger=true;
                break;
            default:
                System.out.println("Please try again. Remember, the maximum amount of passengers allowed is 2.");
                System.out.println("How many passengers are staying within this cabin?");
                cabin_people=scan_in.nextInt();
                scan_in.nextLine();
                continues=true;
            }//Closes the Switch
        }//Closes the while(continues) loop

        while(onepassenger)
        {
            System.out.println("Please state your FIRST name: ");
            fname1=scan_in.nextLine();
            System.out.println();
            System.out.println("Please state your LAST name: ");
            lname1=scan_in.nextLine();

            onepassenger=false;
            Project2_JoshuaLucas passenger1 = new Project2_JoshuaLucas (fname1, lname1, "", "", "", "", "", 0.00);
            System.out.println(passenger1);
        } //Closes while(1passenger)
        while(twopassenger)
        {
            System.out.println("Please state your FIRST name: ");
            fname1=scan_in.nextLine();
            System.out.println("Please state your LAST name: ");
            lname1=scan_in.nextLine();
            System.out.println("Please enter the second passenger's FIRST name: ");
            fname2=scan_in.nextLine();
            System.out.println("Please enter the second passenger's LAST name: ");
            lname2=scan_in.nextLine();
            System.out.println("Please enter the city you live in: ");
            cob=scan_in.nextLine();

            twopassenger=false;
        } //Closes while(2passenger)
    } //Closes yes | y
    else 
        System.out.println("Please select another cabin");
    continues=false;
} //Closes source==cabin1

【问题讨论】:

    标签: java constructor calling-convention


    【解决方案1】:

    您正在显示由您的 Swing 组件之一返回的 toString() 方法——可能是 JPanel,因为它使用 FlowLayout,解决方案是:

    • 不要将 Swing 组件传递给 System.out.println(...) 调用,
    • 而是打印出关键的非 GUI 对象的状态,这些对象称为“模型”对象,因为它们对程序的行为和状态进行建模。
    • 确保这些类具有良好的 public String toString() 方法覆盖,这样它们打印出来的内容才有意义。

    你问:

    什么是模型对象?

    例如,如果您正在创建一个飞机 GUI,您可能需要创建几个类,一些是 GUI 组件类,另一些则不是。模型类可能包括:

    • 乘客
    • 飞机
    • 飞机座椅
    • ....

    这些模型类将包含有关它们自己的信息,例如,Passenger 将有一个姓名,也许是一个年龄,一个passengerNumber id 号...... Airplane 将有一个头等 AirplaneSeats 的集合,coach 也是如此。 AirplaneSeat 将有一个布尔值占用字段,也许还有一个乘客字段来告诉谁占据了哪个座位。但是这些类都不会包含 GUI 组件代码,也不会扩展 JPanel 或 JFrame 等。

    然后您可能会拥有扩展 GUI 组件或包含它们的视图类,它们将显示上述模型类的状态。您试图打印出视图类的 toString(),它不会覆盖 toString() 方法。

    附带建议:您似乎正在尝试将 Swing GUI 程序与控制台程序混合使用,而您不会想要这样做。相反,您应该在 GUI 中而不是在控制台中进行所有用户交互和输出,可能只是为了简单的调试目的,但仅此而已。

    【讨论】:

    • 对不起,我是 CSCI 专业一年级的学生,我对 java 非常陌生。什么是模型对象?并且覆盖只是@Override?​​span>
    • @JoshuaLucas:见编辑。还可以考虑通过显示哪一行打印出违规信息并发布打印出的完整字符串来改进您的问题,因为它看起来有些被遗漏了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-13
    • 2023-03-27
    • 2011-09-25
    • 2018-06-24
    • 2012-06-23
    • 1970-01-01
    • 2010-10-31
    相关资源
    最近更新 更多