在Java中对字符串数组进行迭代

huangapple go评论72阅读模式
英文:

String array iteration in Java

问题

在这个 Java 程序中,我试图获取字符串数组的元素数量(n),然后迭代那么多次来插入值到我的字符串数组中。但插入操作没有进行到最后,最后一次迭代没有执行。例如,如果 n=3,那么应该迭代三次(0、1、2)进行插入,但实际只迭代了两次。请帮忙理解以下代码:

import java.util.*;
public class Main
{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String[] arr=new String[60];
        System.out.println("Enter no of elements");
        int n=sc.nextInt();
        for(int i=0;i<n;i++) {
            arr[i]=sc.nextLine();
        }
        for(int i=0;i<n;i++)
            System.out.println(arr[i]);
    }
}
英文:

In this java program, i am trying to take no of elements (n) for my String array, and then iterating that much times to insert values in my String array. But insertion does not reach till end and does not execute the last iteration. For example if n=3 then it should iterate three times(0,1,2) for insertion but it does only twice. Please help to understand.enter code here

import java.util.*;
public class Main
{
    public static void main(String[] args) {
	    Scanner sc=new Scanner(System.in);
	    String[] arr=new String[60];
	    System.out.println(&quot;Enter no of elements&quot;);
	    int n=sc.nextInt();
	    for(int i=0;i&lt;n;i++) {
	        arr[i]=sc.nextLine();
	    }
	    for(int i=0;i&lt;n;i++)
	    System.out.println(arr[i]);
    }
}

答案1

得分: 0

问题与 .nextInt(); 函数有关。

当您键入一个数字并按下回车键时,程序会接收到数字和换行字符 (\n)。因此,接下来用 .nextLine() 读取的是换行符 '\n'。

作为解决方法,您可以将 .nextLine() 放在 .nextInt() 正下方。

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String[] arr = new String[60];
    System.out.println("输入元素数量");
    int n = sc.nextInt();
    sc.nextLine(); // <-- 这一行
    for (int i = 0; i < n; i++) {
        arr[i] = sc.nextLine();
    }
    for (int i = 0; i < n; i++)
        System.out.println(arr[i]);
}
英文:

The problem is related with .nextInt(); function.

When you type a number and press enter, the program receive the number and a break line character (\n). So, the next line read with .nextLine() is the '\n'.

As workaround you can place .nextLine() just below .nextInt()

public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    String[] arr=new String[60];
    System.out.println(&quot;Enter no of elements&quot;);
    int n=sc.nextInt();
    sc.nextLine(); //&lt;-- This line
    for(int i=0;i&lt;n;i++) {
        arr[i]=sc.nextLine();
    }
    for(int i=0;i&lt;n;i++)
    System.out.println(arr[i]);
}

答案2

得分: 0

所以我假设您每次在新行中输入您的输入(包括元素数量的值)。您可以继续使用.nextLine(),然后将其转换为int赋值给您的n变量,就像下面这样。

import java.util.*;
public class Main
{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] arr=new String[60];
        System.out.println("输入元素的数量");
        int n = Integer.parseInt(sc.nextLine());
        for(int i = 0;i < n; i++) {
            arr[i] = sc.nextLine();
        }
        for(int i = 0; i < n; i++)
        System.out.println(arr[i]);
    }
}
英文:

So I assume you enter your input in a new line each time (include the value of the number of elements). What you could do is just stick with .nextLine() and then convert that to an int for your n variable, like below.

import java.util.*;
public class Main
{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] arr=new String[60];
        System.out.println(&quot;Enter no of elements&quot;);
        int n = Integer.parseInt(sc.nextLine());
        for(int i = 0;i &lt; n; i++) {
            arr[i] = sc.nextLine();
        }
        for(int i = 0; i &lt; n; i++)
        System.out.println(arr[i]);
    }
}

huangapple
  • 本文由 发表于 2020年10月16日 19:57:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64388756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定