Java I';我尝试使用2个用户输入来填充2d列表数组

Java I';我尝试使用2个用户输入来填充2d列表数组,java,Java,我正在尝试使用2个用户输入填充2d列表数组 我遇到的问题是,在下面的代码中,第一个for语句没有产生我期望的结果,第二个for语句正在做需要做的事情。此外,使用下面的代码,我无法关闭扫描仪 public static void main(String[] args) { ArrayList<String> listCon = new ArrayList<String>(); ArrayList<String> listCol = new Ar

我正在尝试使用2个用户输入填充2d列表数组

我遇到的问题是,在下面的代码中,第一个
for
语句没有产生我期望的结果,第二个
for
语句正在做需要做的事情。此外,使用下面的代码,我无法关闭扫描仪

public static void main(String[] args) {

    ArrayList<String> listCon = new ArrayList<String>();
    ArrayList<String> listCol = new ArrayList<String>();

    Scanner txtInput = new Scanner(System.in);

    char addTo = 'y';

    do {

        System.out.println("\nCurrent list is " + listCon + listCol + "\n");

        System.out.println("Would you like to add a country to the list?\n\t"
                            + "( y ) = YES\n\t( n ) = NO");

        addTo = txtInput.next().toLowerCase().charAt(0);

        if (addTo == 'y') {

            System.out.println("Enter country name: ");

            listCon.add(txtInput.next().toLowerCase());

            System.out.println("Enter colour: ");

            listCol.add(txtInput.next().toLowerCase()); 

        } else if (addTo == 'n') { 

            int i = 1;

            int countCon = listCon.size();

            if(countCon == 0) {

                System.out.println("No countries have been entered.");

            } else {

                String str = "country";

                if(countCon > 1) {  

                    str = "countries";
                }

                System.out.println("Thankyou for your input.  We found " + countCon + " " + 
                                    str + " in the list.");

                System.out.println("Listed " + str + ":\n");

                for(String n : listCon) {

                    char[] conDigit = n.toCharArray();

                    conDigit[0] = Character.toUpperCase(conDigit[0]);

                    n = new String(conDigit);

                    for(String b : listCol) {

                        char[] colDigit = b.toCharArray();

                        colDigit[0] = Character.toUpperCase(colDigit[0]);

                        b = new String(colDigit);

                        System.out.println("Country " + i + " : " + n + " - \t" + b);

                        i = i + 1;

                    }
                    break;  
                }
                break;
            }  
        } else { 
            System.out.println("Incorrect input detected.  please try again. \n");              
        }
    } while (true);
} 
      } 
publicstaticvoidmain(字符串[]args){
ArrayList listCon=新的ArrayList();
ArrayList listCol=新的ArrayList();
扫描仪txtInput=新扫描仪(System.in);
char addTo='y';
做{
System.out.println(“\n当前列表为“+listCon+listCol+”\n”);
System.out.println(“要将国家/地区添加到列表中吗?\n\t”
+“(y)=是\n\t(n)=否”);
addTo=txtInput.next().toLowerCase().charAt(0);
如果(addTo='y'){
System.out.println(“输入国家名称:”);
add(txtInput.next().toLowerCase());
System.out.println(“输入颜色:”);
添加(txtInput.next().toLowerCase());
}如果(addTo='n'){
int i=1;
int countCon=listCon.size();
如果(countCon==0){
System.out.println(“未输入任何国家/地区”);
}否则{
String str=“country”;
如果(countCon>1){
str=“国家”;
}
System.out.println(“感谢您的输入。我们找到了”+countCon+”+
str+“在列表中。”);
System.out.println(“列出的“+str+”:\n”);
for(字符串n:listCon){
char[]conditigt=n.toCharArray();
conDigit[0]=字符.toUpperCase(conDigit[0]);
n=新字符串(conDigit);
for(字符串b:listCol){
char[]colDigit=b.toCharArray();
colDigit[0]=字符.toUpperCase(colDigit[0]);
b=新字符串(colDigit);
System.out.println(“国家”+i+:“+n+”-\t”+b);
i=i+1;
}
打破
}
打破
}  
}否则{
System.out.println(“检测到不正确的输入。请重试。\n”);
}
}虽然(正确);
} 
} 

您需要从第一个
循环中删除额外的
break
,以进行迭代。否则,您将在第一次迭代后中断

for(String n : listCon) {
....
    for(String b : listCol) {
    ...
    }
    break;  //remove this! 
}
break;
编辑


结果是国家1:法国-蓝色国家2:英国- 白色国家3:爱尔兰-绿色

您需要像这样迭代:

for (int i = 0; i < listCon.size() && i < listCol.size(); i++) {
    String n = listCon.get(i);
    char[] conDigit = n.toCharArray();
    conDigit[0] = Character.toUpperCase(conDigit[0]);
    n = new String(conDigit);

    String b = listCol.get(i);
    char[] colDigit = b.toCharArray();
    colDigit[0] = Character.toUpperCase(colDigit[0]);
    b = new String(colDigit);

    System.out.println("Country " + i + " : " + n + " - \t" + b);
}
for(int i=0;i
当我删除该中断时,我得到的结果是-国家1:法国-蓝色国家2:法国-白色国家3:法国-绿色国家4:英国-蓝色国家5:英国-白色国家6:英国-绿色国家7:爱尔兰-蓝色国家8:爱尔兰-白色国家9:爱尔兰-绿色我所追求的结果是国家1:法国-蓝色国家2:英国-白人国家3:爱尔兰-格林我已经编辑了答案,以符合您的要求。您的问题是,您正在对每一行的每一列进行迭代。我已经向您展示了如何只迭代一次