1

元の質問

だから私はコーディングが初めてです。私はおそらく私の3ヶ月のマークに達しました。しかし、これには本当に興味があるので、受講しているクラスを通過するのが好きです。だから私はそれをもう少し理解しようとするためにいくつかのコードをいじりたいと思っていました。多くのグーグルの後、これは私が得た限りです。プログラムはパスワードを要求すると想定されています。正しいパスワードを入力すると、2 つのオプションが表示されます。オプション 1 では、情報 (名前、苗字、年齢、携帯電話番号) を入力します。オプション 2 は、保存されている情報を表示します。A から取得した情報を B に表示したいという事実を除けば、これまでのところすべて順調に進んでいます。2 つの別個のクラスがあります。1 つ目は 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);
        }
    }
    }
}

私の 2 番目のクラス (表示) は、私が問題に遭遇した場所です。それらをパブリック文字列にする必要がありますか? または何?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);
            }
        }
    }
}

ご覧のとおり、クラス名を から に変更し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()は、クラスの新しいオブジェクトを作成し、それを typeと nameDisplayの変数に割り当てます。(現在は変数になっている) でメソッドを呼び出すと、クラスではなく、変数が指すオブジェクトで呼び出されます。Displaydisplaydisplay

于 2013-03-26T15:57:11.313 に答える