原始问题

所以我是编码新手。我可能达到了我的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);
            }
        }
    }
}


它运行完美!

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

最佳答案

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

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);
            }
        }
    }
}


如您所见,我将类名称从display更改为Display。这是一种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类的新对象,并将其分配给类型为Display和名称为display的变量。如果在display(现在是变量)上调用方法,则在对象指向的变量上调用它们,而不是指向类。

09-09 23:35
查看更多