如何创建一个指定用于存储数组的类,然后从任何其他方法调用它?

时间:2019-01-26 06:54:22

标签: java arrays

我试图通过创建专门用于信息数组的类来清理代码。基本上就像是变量存储,以备日后需要时使用它们。这是我到目前为止的内容:

package com.input;

import java.util.Scanner;


public class Gender extends Welcome {
    Scanner input = new Scanner(System.in);
    private static String gender;


    public static void setGender() {
        Scanner input = new Scanner(System.in);
        int[] storeInts = new int[25];
        storeInts[0] = 0;
        //The [0] index of array storeInformation is the gender value. 0 = female; 1 = male
        gender = input.nextLine();
        if(gender.equalsIgnoreCase("boy")) {
            System.out.println("What is your name, sir?");
            while (storeInts[0] < 1) {
                storeInts[0]++;
            }
        }else if(gender.equalsIgnoreCase("girl")) {
            System.out.println("What is your name, ma'am?");
        }else{
            System.out.println("You have failed to answer correctly. Try again:");
            init();
        }
        Name nameObject = new Name();
        nameObject.setName(storeInts[0]);
    }

    public static void nextName(int x) {
        if(x == 1) {
            System.out.println("What is your name, sir?");
        }else{
            System.out.println("What is your name, ma'am?");
        }

        Name nameObject = new Name();
        nameObject.setName2();
    }
}

我要在这里完成的操作是,如果用户键入“ boy”,我的代码将在数组storeInts []的索引[0]中存储1。如果用户键入“ girl”,则索引[0]的值将保持为0。

如果以后需要参考用户的性别,我希望能够返回并使用数组确定他们是“男孩”还是“女孩”。

我希望能够从代码中的任何方法调用此数组。我已经以一种复杂的方式使用了这个数组,我想找到一个使它变得更容易的解决方案。

这是我使用的时间:

nameObject.setName(storeInts[0]);

我将索引[0]转移到setName()方法。 这是setName()方法:

 public void setName(int x) {
    String name;
    name = input.nextLine();
    String storedStrings[] = new String[25];
    storedStrings[0] = name;
    FirstTask firstTaskObject = new FirstTask();

    if (name.length() == 0) {
        System.out.println("You must be a unicorn. You want to play games?");
        altInit(x);
    }else{
        System.out.println("Nice to meet you, " + name + "!");
        firstTaskObject.beginning(name);
    }
}

如您所见,我以与上一个数组相同的方式创建了另一个数组,但是这个数组用来存储字符串。现在回到我在说的-参数(int x)与storeInts [0]的值相同。这将告诉我用户是男性还是女性。当用户决定尝试继续而不先输入姓名时,此值将发送到altInit()方法。

这是altInit()方法:

public void altInit(int x) {
    String yesOrNo;
    AltStory altStoryObject = new AltStory();
    Gender backToGender = new Gender();
    yesOrNo = input.nextLine();
    if(yesOrNo.equalsIgnoreCase("yes")) {
        altStoryObject.AltInit();
    }else if(yesOrNo.equalsIgnoreCase("no")) {
        System.out.println("Consider this your last warning...");
        backToGender.nextName(x);
    }else{
        System.out.println("You have failed to answer correctly. Try again:");
        init();
    }
}

当询问他们是否要玩游戏时,他们可以键入“是”或“否”。如果用户输入“ no”(例如不想玩游戏),则程序将打印“考虑这是您的最后警告...”,然后继续执行上一类Gender中的nextName()方法。这也会再次在数组storedInts []中传递该索引[0]。

这是nextName()方法:

public static void nextName(int x) {
    if(x == 1) {
        System.out.println("What is your name, sir?");
    }else{
        System.out.println("What is your name, ma'am?");
    }

    Name nameObject = new Name();
    nameObject.setName2();
}

如您所见,如果用户使用的是男性(或1)的值,则程序将打印“您叫什么名字,先生?”。如果该值是女性(或0),则程序将显示“您叫什么名字,女士?”

这一次,我一直感觉到storeInts [0]的存储值一直在跳跃,直到被使用为止...我想通过只创建一个类使方法能够调用任何值的方法来防止这种情况在需要时将其存储在该数组中。如何创建数组,将其存储在方法中,并在需要时调用它?

根据某人的要求,下面是整个代码:

//Gender class
package com.input;

import java.util.Scanner;


public class Gender extends Welcome {
Scanner input = new Scanner(System.in);
private static String gender;


public void setGender() {
    Scanner input = new Scanner(System.in);
    int [] storeInts = new int[25];
    storeInts[0] = 0;
    //The [0] index of array storeInformation is the gender value. 0 = female; 1 = male
    gender = input.nextLine();
    if (gender.equalsIgnoreCase("boy")) {
        System.out.println("What is your name, sir?");
        while(storeInts[0]<1){
            storeInts[0]++;
        }
    } else if (gender.equalsIgnoreCase("girl")) {
        System.out.println("What is your name, ma'am?");
    } else {
        System.out.println("You have failed to answer correctly. Try again:");
        init();
    }
    Name nameObject = new Name();
    nameObject.setName(storeInts[0]);
}

public void nextName(int x){
    if (x == 1) {
        System.out.println("What is your name, sir?");
    }else {
        System.out.println("What is your name, ma'am?");
    }

    Name nameObject = new Name();
    nameObject.setName2();
    }

}
//Name class
package com.input;

public class Name extends Gender{

public void setName(int x) {
    String name;
    name = input.nextLine();
    String storedStrings[] = new String[25];
    storedStrings[0] = name;
    FirstTask firstTaskObject = new FirstTask();

    if (name.length() == 0) {
        System.out.println("You must be a unicorn. You want to play games?");
        altInit(x);
    } else {
        System.out.println("Nice to meet you, " + name + "!");
        firstTaskObject.beginning(name);
    }
}

public void altInit(int x){
    String yesOrNo;
    AltStory altStoryObject = new AltStory();
    Gender backToGender = new Gender();
    yesOrNo = input.nextLine();
        if(yesOrNo.equalsIgnoreCase("yes")) {
            altStoryObject.AltInit();
        }else if(yesOrNo.equalsIgnoreCase("no")){
            System.out.println("Consider this your last warning...");
            backToGender.nextName(x);
        }else{
            System.out.println("You have failed to answer correctly. Try again:");
            init();
        }
}
public void setName2() {
    String name;
    name = input.nextLine();
    FirstTask firstTaskObject = new FirstTask();

    if (name.length() == 0) {
        System.out.println("You have failed to answer correctly. Try again:");
        init();
    } else {
        System.out.println("Nice to meet you, " + name + "!");
        firstTaskObject.beginning(name);
        }
    }
}

如何创建数组,将其存储在方法中,并在需要时调用它?

0 个答案:

没有答案