Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
扫描程序跳过循环Java中的第一个输入_Java_Loops - Fatal编程技术网

扫描程序跳过循环Java中的第一个输入

扫描程序跳过循环Java中的第一个输入,java,loops,Java,Loops,这是一个基本的名称排序程序。除了用户不能输入名字之外,一切都正常。代码如下: public static void main(String args[]){ Scanner sc = new Scanner(System.in); System.out.println("How many names do you want to sort"); int num = sc.nextInt(); String[] names = new String[num];

这是一个基本的名称排序程序。除了用户不能输入名字之外,一切都正常。代码如下:

public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    System.out.println("How many names do you want to sort");
    int num = sc.nextInt();
    String[] names = new String[num];
    for (int x = 0; x < names.length; x++){
        int pos = x+1;
        System.out.println("Enter name " + pos);
        //String temp = sc.nextLine();
        names[x] = sc.nextLine();
    }
    String sortedArray[] = sort(names);
    for (int i = 0; i < sortedArray.length; i++){
        System.out.print(sortedArray[i] + " ");
    }
}   
publicstaticvoidmain(字符串参数[]){
扫描仪sc=新的扫描仪(System.in);
System.out.println(“您要排序多少个名称”);
int num=sc.nextInt();
字符串[]名称=新字符串[num];
对于(int x=0;x
更新:我更改了代码,因此如果是第一次,它将调用sc.nextLine(),然后将输入设置为names[0] .next()的一个问题是,如果一个人的名字是两个单词,则将其视为两个名字。这是有效的更新代码:

public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    System.out.println("How many names do you want to sort");
    int num = sc.nextInt();
    String[] names = new String[num];
    //String[] temp = new String[names.length];
    for (int x = 0; x < names.length; x++) {
        int pos = x + 1;
        if (x == 0) {
            System.out.println("Enter name 1");
            sc.nextLine();
            names[0] = sc.nextLine();
        } else {
            System.out.println("Enter name " + pos);
            //String temp = sc.nextLine();
            names[x] = sc.nextLine();
        }
    }
    String sortedArray[] = sort(names);
    for (int i = 0; i < sortedArray.length; i++) {
        System.out.print(sortedArray[i] + " ");
    }
}
publicstaticvoidmain(字符串参数[]){
扫描仪sc=新的扫描仪(System.in);
System.out.println(“您要排序多少个名称”);
int num=sc.nextInt();
字符串[]名称=新字符串[num];
//String[]temp=新字符串[names.length];
对于(int x=0;x
使用
sc.next()而不是
sc.nextLine()

  • next()
    将从输入流中查找并返回下一个完整的令牌
  • nextLine()
另外,请查看
扫描仪#nextLine()
中的以下说明

使扫描器前进超过当前行并返回 被跳过了。此方法返回当前行的其余部分, 不包括末端的任何行分隔符。该位置设置为 下一行的开头

由于此方法继续在输入中搜索 行分隔符,它可以缓冲搜索该行的所有输入 如果没有行分隔符,则跳过

Scanner sc=新扫描仪(System.in);
System.out.println(“您要排序多少个名称”);
int num=sc.nextInt();
字符串[]名称=新字符串[num];
对于(int x=0;x
你得到答案了吗?如果没有,请写下你的答案,以便其他人从中受益。stackoverflow.com/help/accepted-answer
 Scanner sc = new Scanner(System.in);
    System.out.println("How many names do you want to sort");
    int num = sc.nextInt();
    String[] names = new String[num];
    for (int x = 0; x < names.length; x++){
        int pos = x+1;
        System.out.println("Enter name " + pos);
        //String temp = sc.nextLine();
        names[x] = sc.next();
    }
    /*String sortedArray[] = sort(names);
    for (int i = 0; i < sortedArray.length; i++){
        System.out.print(sortedArray[i] + " ");
    }*/