从类的一部分到另一部分获取字符串

时间:2013-03-26 15:44:23

标签: java string class loops public

原始问题

所以我不熟悉编码。我可能已经打了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);
    }
}
}
}

更新的问题

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

以下是更新后的代码:

主要课程

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);
            }
        }
    }
}
BTW感谢所有人的帮助。我很感激。

解决方案

好吧,伙计们。我玩了一些它,并找到了如何让它工作。感谢您需要的所有帮助。

主要课程

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

它完美运行!

P.S 它不会让我回答我自己的问题

1 个答案:

答案 0 :(得分: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);
            }
        }
    }
}

如你所见,我将班级名称从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(现在是变量)上调用方法,则会在变量指向的对象上调用它们而不是类。