gpt4 book ai didi

Java 二维数组与数组并行的问题

转载 作者:行者123 更新时间:2023-12-01 18:42:26 25 4
gpt4 key购买 nike

我想知道是否有一种更有效的方法来从锯齿状数组中获取每一行,而不是列出单词的每个位置。问题是,当用户输入家庭名称时,它会显示相应的 sibling 列表(并行数组,用于存储和检索名称),然后再次提示用户输入数字,然后显示先出生的 child 、第二等。

这是我到目前为止所拥有的:

import java.util.*;
public class siblings{
public static void main (String [] argrs){
String [] family = { "Potter", "Weasley", "Grangley",
"Dubmbledore", "Black", "Patil", "Creevey"};
String [] [] siblings = {
{"harry"},
{"bill", "charlie", "percry", "fred", "george", "ron", "ginny"},
{"hermione"},
{"Ablus","aberforth", "arrianna"},
{"Sirius", "regulus"},
{"padma", "pavarti"},
{"colin", "dennnis"}};


Scanner kb = new Scanner (System.in);
System.out.println("Enter a family name: ");
String input = kb.next().toLowerCase();



switch (input) {
case "black" :
System.out.println(
siblings[4][0]+" "+ siblings[4][1]);

break;
case "potter":
System.out.println(
siblings[0][0]);

break;
case "weasley":
System.out.println(
siblings[1][0]);

}




System.out.println("Enter number between 1-7");
int input2 = kb.nextInt();

switch (input2) {
case 1:
System.out.println("the names of the 1st born child:");
System.out.println(
siblings[0][0]
+", "
+siblings [1][0]
+", "
+siblings [2][0]
+", "
+siblings [3][0]
+", "
+siblings [4][0]
+", "
+siblings [5][0]
+", "
+siblings [6][0]
);

break;
case 2 :
System.out.println("the names of the 2nd born child:");
System.out.println(
siblings [1][1]
+", "
+siblings [3][1]
+", "
+siblings [4][1]
+", "
+siblings [5][1]
+", "
+siblings [6][1]
);

break;
case 3 :
System.out.println("the names of the 3nd born child:");
System.out.println(
siblings [1][2]
+", "
+siblings [3][2]
);
break;

case 4:
System.out.println("the names of the 4th born child:");
System.out.println(
siblings [1][3]
);
break;

case 5:
System.out.println("the names of the 4th born child:");
System.out.println(
siblings [1][4]
);
break;

case 6:
System.out.println("the names of the 4th born child:");
System.out.println(
siblings [1][5]
);
break;

case 7:
System.out.println("the names of the 4th born child:");
System.out.println(
siblings [1][6]
);
}
}
}

最佳答案

你可以看我的例子,从这里开始寻找通用的编程方法,你的方法太硬编码了,你需要学习循环(while、for)、控制结构(if、else)。还有,看代码约定,你java写的有点别扭:https://howtodoinjava.com/java/basics/java-naming-conventions/

import java.util.Scanner;

//Note, class name must start from capital letter
//See code conventions: https://howtodoinjava.com/java/basics/java-naming-conventions/
public class Siblings {

public static void main (String [] argrs){
String [] family = { "Potter", "Weasley", "Grangley", "Dubmbledore", "Black", "Patil", "Creevey"};
String [] [] siblings = {
{"harry"},
{"bill", "charlie", "percry", "fred", "george", "ron", "ginny"},
{"hermione"},
{"Ablus","aberforth", "arrianna"},
{"Sirius", "regulus"},
{"padma", "pavarti"},
{"colin", "dennnis"}};


Scanner kb = new Scanner (System.in);
System.out.println("Enter a family name selecting one of those: ");
System.out.println("");
for(String name: family){
System.out.print(name);
System.out.print(" ");
}

String input = kb.next();//note I did not convert to lower case here


int i = 0;
String mySiblings[] = null;
for(String member: family){
if(input.equalsIgnoreCase(member)){//note how I ignore the case,, when checking
mySiblings = siblings[i];
break;
}
i++;
}

System.out.println("Siblings of "+input + " are:");

for(String sibling: mySiblings){
System.out.print(sibling);
System.out.print(" ");
}

//Continue your code here

}

}

关于Java 二维数组与数组并行的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59899208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com