首页 > 解决方案 > 并行二维数组和数组的Java问题

问题描述

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

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

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

标签: javaarrays

解决方案


你可以看我的例子,从这里开始寻找编程中的通用方法,你的方法编码太硬了,你需要学习循环(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

}

}


推荐阅读