英文:
User input to 2 separate arrays in Java
问题
我对Java语言完全不熟悉。我还在学习和练习,但在解决一个问题时,我遇到了困难。我想做的是每行添加2个元素到2个单独的数组中。第一个元素放入X数组,第二个元素放入Y数组。
public static void main (String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(reader.readLine());
double[] X = new double[n];
double[] Y = new double[n];
String[] elem = reader.readLine().split(" ");
for(int i = 0; i < n; i++) {
X[i] = Double.parseDouble(elem[0]);
Y[i] = Double.parseDouble(elem[1]);
}
System.out.println(Arrays.toString(X) + " " + Arrays.toString(Y));
}
输入:
3 4
1 1
2 3
其中第一列是X数组,第二列是Y数组。我多次尝试了代码,但仍然出现“Index 2 out of bounds for length 2”的错误。
英文:
I'm completely new to the Java language. I'm still learning and practicing it but while solving a problem, I got stuck at this. What I want to do is to add 2 elements per line into 2 separate arrays. The first element goes to the X array and the second element goes to the Y array.
public static void main (String[] args) throws IOException {
BufferedReader reader =new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(reader.readLine());
double[] X = new double[n];
double[] Y = new double[n];
String[] elem = reader.readLine().split(" ");
for(int i = 0; i < n; i++)
{
X[i] = Integer.parseInt(elem[n]);
Y[i] = Integer.parseInt(elem[n]);
}
System.out.println(X, Y);
}
input:
3 4
1 1
2 3
where the first column is array X and the second column is array Y. I played with code several times but it still gives me "Index 2 out of bounds for length 2".
答案1
得分: 1
你必须在 for 循环内部读取你的行。X[i]
对应于 elem[0]
;Y[i]
对应于 elem[1]
。例如:
import java.io.*;
import java.util.*;
public class App {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(reader.readLine());
double[] X = new double[n];
double[] Y = new double[n];
for (int i = 0; i < n; i++) {
String[] elem = reader.readLine().split(" ");
X[i] = Integer.parseInt(elem[0]);
Y[i] = Integer.parseInt(elem[1]);
}
System.out.println(Arrays.toString(X));
System.out.println(Arrays.toString(Y));
}
}
英文:
You have to read your lines inside the for loop. X[i]
corresponds to elem[0]
; Y[i]
corresponds to elem[1]
. For example:
import java.io.*;
import java.util.*;
public class App {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(reader.readLine());
double[] X = new double[n];
double[] Y = new double[n];
for (int i = 0; i < n; i++) {
String[] elem = reader.readLine().split(" ");
X[i] = Integer.parseInt(elem[0]);
Y[i] = Integer.parseInt(elem[1]);
}
System.out.println(Arrays.toString(X));
System.out.println(Arrays.toString(Y));
}
}
答案2
得分: 0
这部分不正确:
for(int i = 0; i < n; i++)
{
X[i] = Integer.parseInt(elem[n]);
Y[i] = Integer.parseInt(elem[n]);
看起来应该是这样的:
for(int i = 0; (i - 1)< n; i+= 2) // 每次迭代从elem中读取两个值,因此增加2。将检查条件改为i - 1,以修复奇数个条目的情况,否则会导致elem中在i/2 + 1位置上的ArrayOutOfBoundsException。
{
X[i/2] = Integer.parseInt(elem[i]);
Y[i/2] = Integer.parseInt(elem[i] + 1);
(此处我的回答中存在错误,已经进行了修正)
或者可以选择:
for(int i = 0; i < n; i++)
{
if (i % 2 == 0) X[i/2] = Integer.parseInt(elem[i]);
else Y[i/2] = Integer.parseInt(elem[i]);
英文:
This part is not right:
for(int i = 0; i < n; i++)
{
X[i] = Integer.parseInt(elem[n]);
Y[i] = Integer.parseInt(elem[n]);
It seems like it should be
for(int i = 0; (i - 1)< n; i+= 2) // adding two because you are reading two values from elem in every iteration. Changed check to i - 1 to fix case where an odd number of entries will cause an ArrayOutOfBoundsException in elem on i/2 + 1
{
X[i/2] = Integer.parseInt(elem[i]);
Y[i/2] = Integer.parseInt(elem[i] + 1);
(There was a bug in my answer here. It has been fixed)
Alternatively:
for(int i = 0; i < n; i++)
{
if (i % 2 == 0) X[i/2] = Integer.parseInt(elem[i]);
else Y[i/2] = Integer.parseInt(elem[i]);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论