1

原始问题

所以我是编码新手。我可能已经达到了我的 3 个月大关。但我喜欢通过我正在上的课程,因为这些东西真的让我很感兴趣。所以我想弄乱一些代码来尝试更多地理解它。经过大量的谷歌搜索,这是我所得到的。该程序假设要求输入密码。如果输入正确的密码,它将显示两个选项。选项 1 将让您输入信息(姓名、姓氏、年龄、手机号码)。选项 2 将显示存储的信息。除了我想将从 A 获得的信息显示到 B 之外,到目前为止一切都很好。我有两个单独的课程。第一个被称为 main (这是主要的方法,女巫工作正常)

import javax.swing.*;

//Created by: Robert Duval
//3/26/13

public class Main
{
    public static void main(String[] args)
    {
        String tempString, passWord = "mrGiggles", input = "null";

        while(!input.equals(passWord)) //This loop looks for the password
        {

        input = JOptionPane.showInputDialog("Hello, please enter password.");

        if(input.equals(passWord)) //If the password is correct
        {
            while(!input.equals("Enter information")||!input.equals("View profile")) //This loop looks to see what to do next
            {
                input = JOptionPane.showInputDialog("Welcome\nEnter information\nView profile");

                if(input.equals("Enter information"))
                {
                    display.input();
                }
                else if(input.equals("View profile"))
                {
                    display.stored();
                }
                else
                {
                tempString = "ERROR\nCannot find what you are looking for.";
                JOptionPane.showMessageDialog(null, tempString);
                }
            }


        }
        else //If the password is incorrect.
        {
            tempString = "In-correct";
            JOptionPane.showMessageDialog(null, tempString);
        }
    }
    }
}

我的第二堂课(显示器)是我遇到问题的地方。我应该将它们设为公共字符串吗?或者是什么?input() 方法填充了我想在 stored() 方法中使用的字符串。我一直在寻找这个,但我不明白回报和什么不是。如果你能帮助我并指出我的缺陷,那就太好了。

import javax.swing.*;

//Created by: Robert Duval
//3/26/13

public class display
{
    public static void input() //This is the method that will ask for the information
    {
        String age="null", cellNumber="null", name="null", lastName="mull", allInfo = name+ "\n" +lastName+ "\n" +age+ "\n" +cellNumber+ "\n";

        name = JOptionPane.showInputDialog("Enter the first name");
        lastName = JOptionPane.showInputDialog("Enter the last name");
        age = JOptionPane.showInputDialog("Enter the age");
        cellNumber = JOptionPane.showInputDialog("Enter the cell phone number");

        display.stored();

    }


    public static void stored() //This method is asking the user what to show for the input() method.
    {
        String loop = "loop", tempString;


    while(!loop.equals("break"))
    {

    tempString = JOptionPane.showInputDialog("What information would you like to see? \nname\nage\ncell number\nall info\nquit");

    if(tempString.equals("name")||tempString.equals("Name")||tempString.equals("NAME"))
    {
        JOptionPane.showMessageDialog(null, name); //This is where I want to display the name String from input() method
    }
    else if(tempString.equals("age")||tempString.equals("Age")||tempString.equals("AGE"))
    {
        JOptionPane.showMessageDialog(null, age); //This is where I want to display the age String from input() method
    }
    else if(tempString.equals("cell number")||tempString.equals("Cell number")||tempString.equals("cell Number")||tempString.equals("Cell Number")||tempString.equals("cellNumber")||tempString.equals("cellnumber")||tempString.equals("Cellnumber"))
    {
        JOptionPane.showMessageDialog(null, cellNumber); //This is where I want to display the cellNumber String from input() method
    }
    else if(tempString.equals("all info")||tempString.equals("All info")||tempString.equals("all Info")||tempString.equals("All Info")||tempString.equals("allinfo")||tempString.equals("allInfo")||tempString.equals("Allinfo")||tempString.equals("AllInfo"))
    {
        JOptionPane.showMessageDialog(null, allInfo); //This is where I want to display the allInfo String from input() method
    }
    else if(tempString.equals("quit")||tempString.equals("Quit")||tempString.equals("QUIT"))
    {
        loop = "break"; //Breaks the while loop
    }
    else
    {
        tempString = "Not a valid answer. \nPlease try again.";
        JOptionPane.showMessageDialog(null, tempString);
    }
}
}
}

更新的问题

好的,所以在查看答案后,我真的很接近了!但是由于某种原因,当我查看数据时,它会为所有内容生成“null”。我在想它是因为我关闭了该方法,然后重新打开它,所以一切都刷新了。如何保存输入的信息。离开方法。回来但打开显示并显示该信息?

这是更新的代码:

主班

import javax.swing.*;

//Created by Robert Duval
//3/26/13

public class Main
{
    public static void main(String[] args)
    {
        String tempString, passWord = "mrGiggles", input = "null";

        display display = new display();

        while(!input.equals(passWord)) //This loop looks for the password
        {

            input = JOptionPane.showInputDialog("Hello, please enter password.");

            if(input.equals(passWord)) //If the password is correct
            {
                while(!input.equalsIgnoreCase("Enter information")||!input.equalsIgnoreCase("View profile")) //This loop looks to see what to do next
                {
                    input = JOptionPane.showInputDialog("Welcome\nEnter information\nView profile");

                    if(input.equalsIgnoreCase("Enter information"))
                    {
                        display.input();
                    }
                    else if(input.equalsIgnoreCase("View profile"))
                    {
                        display.stored();
                    }
                    else
                    {
                        tempString = "ERROR\nCannot find what you are looking for.";
                        JOptionPane.showMessageDialog(null, tempString);
                    }
                }
            }
            else //If the password is incorrect.
            {
                tempString = "In-correct";
                JOptionPane.showMessageDialog(null, tempString);
            }
        }
    }
}

显示类

import javax.swing.*;

//Created by: Robert Duval
//3/26/13

public class display
{
    String age="null", cellNumber="null", name="null", lastName="mull", allInfo = name+ "\n" +lastName+ "\n" +age+ "\n" +cellNumber+ "\n";

    public void input()
    {
        name = JOptionPane.showInputDialog("Enter the first name");
        lastName = JOptionPane.showInputDialog("Enter the last name");
        age = JOptionPane.showInputDialog("Enter the age");
        cellNumber = JOptionPane.showInputDialog("Enter the cell phone number");
    }


    public void stored()
    {
        String tempString;


        while(true)
        {

            tempString = JOptionPane.showInputDialog("What information would you like to see? \nname\nage\ncell number\nall info\nquit");

            if (tempString.equalsIgnoreCase("name"))
            {
                JOptionPane.showMessageDialog(null, name); //This is where I want to display the name String from input() method
            }
            else if(tempString.equalsIgnoreCase("age"))
            {
                JOptionPane.showMessageDialog(null, age); //This is where I want to display the age String from input() method
            }
            else if(tempString.equalsIgnoreCase("cell number"))
            {
                JOptionPane.showMessageDialog(null, cellNumber); //This is where I want to display the cellNumber String from input() method
            }
            else if(tempString.equalsIgnoreCase("all info")||tempString.equalsIgnoreCase("allinfo"))
            {
                JOptionPane.showMessageDialog(null, allInfo); //This is where I want to display the allInfo String from input() method
            }
            else if(tempString.equalsIgnoreCase("quit"))
            {
               break; //Breaks the while loop
            }
            else
            {
                tempString = "Not a valid answer. \nPlease try again.";
                JOptionPane.showMessageDialog(null, tempString);
            }
        }
    }
}

顺便说一句,感谢大家的所有帮助。我很感激。

解决方案

好吧,伙计们。我又玩了一些,发现如何让它工作。感谢所有需要的帮助。

主班

import javax.swing.*;

//Created by Robert Duval
//3/26/13

public class Main
{
    public static void main(String[] args)
    {
        String tempString, passWord = "mrGiggles", input = "null";

        display display = new display();

        while(!input.equals(passWord)) //This loop looks for the password
        {

            input = JOptionPane.showInputDialog("Hello, please enter password.\nQuit");

            if(input.equals(passWord)) //If the password is correct
            {
                while(!input.equalsIgnoreCase("Enter information")||!input.equalsIgnoreCase("View profile")) //This loop looks to see what to do next
                {
                    input = JOptionPane.showInputDialog("Welcome\nEnter information\nView profile\nLog out");

                    if(input.equalsIgnoreCase("Enter information"))
                    {
                        display.input();
                    }
                    else if(input.equalsIgnoreCase("View profile"))
                    {
                        display.stored();
                    }
                    else if(input.equalsIgnoreCase("log out"))
                    {
                        break;
                    }
                    else
                    {
                        tempString = "ERROR\nCannot find what you are looking for.";
                        JOptionPane.showMessageDialog(null, tempString);
                    }
                }
            }
            else if(input.equalsIgnoreCase("quit"))
            {
                break;
            }
            else //If the password is incorrect.
            {
                tempString = "In-correct";
                JOptionPane.showMessageDialog(null, tempString);
            }
        }
    }
}

显示类

import javax.swing.*;

//Created by: Robert Duval
//3/26/13

public class display
{
    String age="null", cellNumber="null", name="null", lastName="null";

    public void input()
    {
        name = JOptionPane.showInputDialog("Enter the first name");
        lastName = JOptionPane.showInputDialog("Enter the last name");
        age = JOptionPane.showInputDialog("Enter the age");
        cellNumber = JOptionPane.showInputDialog("Enter the cell phone number");
    }


    public void stored()
    {
        String tempString, allInfo = name+ "\n" +lastName+ "\n" +age+ "\n" +cellNumber+ "\n";


        while(true)
        {

            tempString = JOptionPane.showInputDialog("What information would you like to see? \nName\nAge\nCell number\nAll info\nBack");

            if (tempString.equalsIgnoreCase("name"))
            {
                JOptionPane.showMessageDialog(null, name);
            }
            else if(tempString.equalsIgnoreCase("age"))
            {
                JOptionPane.showMessageDialog(null, age);
            }
            else if(tempString.equalsIgnoreCase("cell number"))
            {
                JOptionPane.showMessageDialog(null, cellNumber);
            }
            else if(tempString.equalsIgnoreCase("all info")||tempString.equalsIgnoreCase("allinfo"))
            {
                JOptionPane.showMessageDialog(null, allInfo);
            }
            else if(tempString.equalsIgnoreCase("back"))
            {
               break;
            }
            else
            {
                tempString = "Not a valid answer. \nPlease try again.";
                JOptionPane.showMessageDialog(null, tempString);
            }
        }
    }
}

它运行完美!

PS它不会让我回答我自己的问题

4

1 回答 1

0

您需要使您的显示类方法非静态并使用带有字段的对象:

public class Display
{
    private String age="null", cellNumber="null", name="null", lastName="mull", allInfo = name+ "\n" +lastName+ "\n" +age+ "\n" +cellNumber+ "\n";

    public void input()
    {
        name = JOptionPane.showInputDialog("Enter the first name");
        lastName = JOptionPane.showInputDialog("Enter the last name");
        age = JOptionPane.showInputDialog("Enter the age");
        cellNumber = JOptionPane.showInputDialog("Enter the cell phone number");

        stored();
    }        

    public void stored()
    {
        String tempString;

        while(true)
        {        
            tempString = JOptionPane.showInputDialog("What information would you like to see? \nname\nage\ncell number\nall info\nquit");

            if(tempString.equalsIgnoreCase("name"))
            {
                JOptionPane.showMessageDialog(null, name); //This is where I want to display the name String from input() method
            }
            else if(tempString.equalsIgnoreCase("age"))
            {
                JOptionPane.showMessageDialog(null, age); //This is where I want to display the age String from input() method
            }
            else if(tempString.equalsIgnoreCase("cell number")||tempString.equals("Cell number")||tempString.equalsIgnoreCase("cellNumber"))
            {
                JOptionPane.showMessageDialog(null, cellNumber); //This is where I want to display the cellNumber String from input() method
            }
            else if(tempString.equalsIgnoreCase("all info")||tempString.equalsIgnoreCase("allinfo"))
            {
                JOptionPane.showMessageDialog(null, allInfo); //This is where I want to display the allInfo String from input() method
            }
            else if(tempString.equalsIgnoreCase("quit"))
            {
               break; //Breaks the while loop
            }
            else
            {
                tempString = "Not a valid answer. \nPlease try again.";
                JOptionPane.showMessageDialog(null, tempString);
            }
        }
    }
}

如您所见,我将班级名称从更改displayDisplay. 这是区分类名和变量名的 Java 约定。这些方法不再是静态的,这意味着您不能在类上调用它们,而只能在该类的对象上调用它们。这样的对象可以具有描述其条件的字段。要访问这些字段,您需要非静态成员。该调用display.stored()现在只是stored()在您刚刚调用的同一对象上调用该方法input()。要清除它,您还可以编写this.stored(). this总是指向现在的对象。

我还在课堂上介绍break了循环中的命令Display

让我们看看现在必须在主类中进行哪些更改:

public class Main
{
    public static void main(String[] args)
    {
        String tempString, passWord = "mrGiggles", input = "null";

        Display display = new Display();

        while(!input.equals(passWord)) //This loop looks for the password
        {
            input = JOptionPane.showInputDialog("Hello, please enter password.");

            if(input.equals(passWord)) //If the password is correct
            {
                while(!input.equals("Enter information")||!input.equals("View profile")) //This loop looks to see what to do next
                {
                    input = JOptionPane.showInputDialog("Welcome\nEnter information\nView profile");

                    if(input.equals("Enter information"))
                    {
                        display.input();
                    }
                    else if(input.equals("View profile"))
                    {
                        display.stored();
                    }
                    else
                    {
                        tempString = "ERROR\nCannot find what you are looking for.";
                        JOptionPane.showMessageDialog(null, tempString);
                    }
                }
            }
            else //If the password is incorrect.
            {
                tempString = "In-correct";
                JOptionPane.showMessageDialog(null, tempString);
            }
        }
    }
}

该行Display display = new Display()创建了Display该类的一个新对象并将其分配给具有 typeDisplay和 name的变量display。如果您在display(现在是变量)上调用方法,则会在变量指向的对象而不是类上调用它们。

于 2013-03-26T15:57:11.313 回答