英文:
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("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]);
}
}
答案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("Enter no of elements");
int n=sc.nextInt();
sc.nextLine(); //<-- This line
for(int i=0;i<n;i++) {
arr[i]=sc.nextLine();
}
for(int i=0;i<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("Enter no of elements");
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]);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论