java - 如何创建一个指定用于存储数组的类,然后从任何其他方法调用它?
问题描述
我试图通过专门为一组信息创建一个类来清理我的代码。它基本上就像变量的存储,以防我以后需要它们。这是我到目前为止所拥有的:
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();
}
}
当被问及是否想玩游戏时,他们可以输入“是”或“否”。如果用户在不想玩游戏时键入“否”,则程序将打印“考虑这是您的最后一个警告...”,然后继续执行上一个 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);
}
}
}
如何创建数组,将其存储在方法中,并在需要时调用它?
解决方案
推荐阅读
- java - Eclipse Workbench 选择工作区
- javascript - 在 Python 中编辑 javascript 文件时出现问题
- c# - 为什么更改 IIS Express 项目 URL 在 ASP.NET MVC 4.7.2 中不起作用?
- python - 使用 pyglet 绘制多边形会创建不需要的重叠区域
- amazon-web-services - AWS Educate 上的 Amazon SES
- google-cloud-platform - 是否可以访问 MemoryStore Fragmentation Ratio,例如在 Cloud Monitoring 中?
- api - alphavantage bist100 api 没有响应
- c# - Nservice消息字段问题
- html - 如何使不同 div 列和不同行高的 2 个表具有相同的表大小
- c# - 在浏览器上按回时页面不加载