请求用户输入以创建两个数组 import java.util.Scanner; 公共类Ch7AssignArrays1{ 公共静态void main(字符串[]args){ int[]人口=新的int[5];//用于存储县人口的数组 String[]county=新字符串[5];//用于存储县名称的数组 扫描仪输入=新扫描仪(System.in); //请求用户输入县名称并存储在数组中 System.out.println(“请输入一个县的名称:”); county[0]=input.toString(); //要求用户输入上面输入的县人口,存储在数组中 System.out.println(“输入县人口:”+input.nextLine()); 填充[0]=input.nextInt(); 对于(int index=1;index

请求用户输入以创建两个数组 import java.util.Scanner; 公共类Ch7AssignArrays1{ 公共静态void main(字符串[]args){ int[]人口=新的int[5];//用于存储县人口的数组 String[]county=新字符串[5];//用于存储县名称的数组 扫描仪输入=新扫描仪(System.in); //请求用户输入县名称并存储在数组中 System.out.println(“请输入一个县的名称:”); county[0]=input.toString(); //要求用户输入上面输入的县人口,存储在数组中 System.out.println(“输入县人口:”+input.nextLine()); 填充[0]=input.nextInt(); 对于(int index=1;index,arrays,for-loop,user-input,Arrays,For Loop,User Input,我已经写了这段代码,当程序到达for循环时遇到了一个问题。我希望能够请求用户输入,存储到一个数组(县),然后再次请求输入并存储到另一个数组(人口),最后打印两个数组。这在for循环之外的input.nextLine上运行良好,每个问题都会被询问然后存储。但是在循环中,两个问题都会同时被问到,并且只对第二个数组(总体)进行输入。我为for循环外部找到的解决方案在循环内部不起作用。我可以在for循环中放入什么来解决这个问题 谢谢Mallory L,这是个好问题 完全回答这个问题需要一点时间 同时,在

我已经写了这段代码,当程序到达for循环时遇到了一个问题。我希望能够请求用户输入,存储到一个数组(县),然后再次请求输入并存储到另一个数组(人口),最后打印两个数组。这在for循环之外的input.nextLine上运行良好,每个问题都会被询问然后存储。但是在循环中,两个问题都会同时被问到,并且只对第二个数组(总体)进行输入。我为for循环外部找到的解决方案在循环内部不起作用。我可以在for循环中放入什么来解决这个问题

谢谢Mallory L,这是个好问题

完全回答这个问题需要一点时间

同时,在“for(int index=1;index 另外,示例输入和示例输出是什么? 例如,用户类型在“洛杉矶”为县,结果将是12000000


如果您希望用户继续添加更多的县和人口,直到用户想要停止,然后考虑使用while循环而不是for for循环。

< p>我的问题是我的代码行县[ 0 ] =输入。toStReg();应该是.nter()来解决多个用户输入问题。
import java.util.Scanner;

public class Ch7AssignArrays1 {

    public static void main(String[] args) {
        int[] population = new int[5];// array to store populations for counties
        String[] county = new String[5]; //array to store county names

        Scanner input = new Scanner(System.in);
        //ask user for county name and store in array
        System.out.println("Please enter the name of a county: ");
        county[0] = input.toString();
        //ask user for population of county entered above, store in array
        System.out.println("Enter the population of the county: " + input.nextLine());
        population[0] = input.nextInt();


        for(int index = 1; index < county.length && index < population.length; index++){
            System.out.println("Please enter the name of a county: ");
            county[index] = input.toString();
            //ask user for population of county entered above, store in array
            System.out.println("Enter the population of the county: ");
            System.out.println(input.hasNext());
            population[index] = input.nextInt();
        }
        for(int index = 0; index < county.length && index < population.length;index++){
        System.out.println(county[index] + "\t" + population[index]);
        }
    }

}