简体   繁体   English

输入两个用户输入到两个 arrays

[英]input two user inputs to two arrays

I want to take 2 inputs from the user, then I want to add them to the following 2 arrays.我想从用户那里获取 2 个输入,然后我想将它们添加到以下 2 个 arrays 中。 I tried this code but this is not doing the job correctly.我尝试了这段代码,但这并没有正确地完成工作。 What should I do我应该怎么办

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
   
public class Input {
    
    public static void main(String args[]) {
        String[] runnersNames = {"Shiran", "Hasini", "Chanchala", "Priyankara", "Mayuri", "Sameera", "Supun"
                    , "Supuni", "Kavindu", "Nadeeka"};
        int[] minutes = {342, 448, 398, 399, 350, 321, 299, 378, 384, 440};
        System.out.println("Do you want to add more players? 1.Yes 2.No ");
        int n;
        Scanner sc = new Scanner(System.in);
        System.out.print("your choice : ");
        n = sc.nextInt();
    
        if (n == 1) {
    
            for (int i = 0; i < 10; i++) {
                System.out.println("Enter the player name : ");
                runnersNames[i] = sc.nextLine();
    
                for (int j = 0; j < i; j++) {
                    System.out.println("Enter the player runtime : ");
                    minutes[j] = sc.nextInt();
                }
    
    
            }
    
        }
    
    }
}

You are replacing the original names with newly added values inside the loop.您正在用循环内新添加的值替换原始名称。 Try not to start the loop from 0. EDIT:-You may also need to use a 2D array/list for minutes.尽量不要从 0 开始循环。编辑:-您可能还需要使用 2D 数组/列表几分钟。

If you want to add players to the these two variable, you have two options:如果您想玩家添加到这两个变量中,您有两种选择:
Create an array with more space,创建一个空间更大的数组,
or using a different data structure, like a List .或使用不同的数据结构,如List First option (creating an array that has more space):第一个选项(创建一个有更多空间的数组):

import java.util.Scanner;

public class Input {
    
    public static void main(String args[]) {
        String[] runnersNames = {"Shiran", "Hasini", "Chanchala", "Priyankara", "Mayuri", "Sameera", "Supun"
                , "Supuni", "Kavindu", "Nadeeka","","","","","","","","","",""};
        int[] minutes = {342, 448, 398, 399, 350, 321, 299, 378, 384, 440,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
        System.out.println("Do you want to add more players? 1.Yes 2.No ");
        int n;
        Scanner sc = new Scanner(System.in);
        System.out.print("your choice : ");
        n = sc.nextInt();
        
        if (n == 1) {
            
            for (int i = 0; i < 10; i++) {
                System.out.println("Enter the player name : ");
                runnersNames[i+10] = sc.nextLine();

                for (int j = 0; j < i; j++) {
                    System.out.println("Enter the player runtime : ");
                    minutes[j+10] = sc.nextInt();
                }
            
            }
        
        }
    
    }
}

The second option is a bit cleaner, I think:我认为第二个选项更干净一些:

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Input {
    
    public static void main(String args[]) {
        List<String> runnersNames = new ArrayList<>(Arrays.asList(new String[]{"Shiran", "Hasini", "Chanchala", "Priyankara", "Mayuri",
            "Sameera", "Supun", "Supuni", "Kavindu", "Nadeeka")});
        List<Integer> minutes = new ArrayList<>(Arrays.asList(new Integer[]{342, 448, 398, 399, 350, 321, 299, 378, 384, 440}));
        System.out.println("Do you want to add more players? 1.Yes 2.No ");
        int n;
        Scanner sc = new Scanner(System.in);
        System.out.print("your choice : ");
        n = sc.nextInt();
        
        if (n == 1) {
            
            for (int i = 0; i < 10; i++) {
                System.out.println("Enter the player name : ");
                runnersNames.add(sc.nextLine());
                
                for (int j = 0; j < i; j++) {
                    System.out.println("Enter the player runtime : ");
                    minutes.add(sc.nextInt());
                }
            
            }
        
        }
    
    }
}

You can then get an element of the list using the get(int index) method.然后,您可以使用get(int index)方法获取列表的元素。

minutes.get(0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM