用户输入到Java中的两个单独数组。

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

User input to 2 separate arrays in Java

问题

我对Java语言完全不熟悉。我还在学习和练习,但在解决一个问题时,我遇到了困难。我想做的是每行添加2个元素到2个单独的数组中。第一个元素放入X数组,第二个元素放入Y数组。

  1. public static void main (String[] args) throws IOException {
  2. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  3. int n = Integer.parseInt(reader.readLine());
  4. double[] X = new double[n];
  5. double[] Y = new double[n];
  6. String[] elem = reader.readLine().split(" ");
  7. for(int i = 0; i < n; i++) {
  8. X[i] = Double.parseDouble(elem[0]);
  9. Y[i] = Double.parseDouble(elem[1]);
  10. }
  11. System.out.println(Arrays.toString(X) + " " + Arrays.toString(Y));
  12. }

输入:

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.

  1. public static void main (String[] args) throws IOException {
  2. BufferedReader reader =new BufferedReader(new InputStreamReader(System.in));
  3. int n = Integer.parseInt(reader.readLine());
  4. double[] X = new double[n];
  5. double[] Y = new double[n];
  6. String[] elem = reader.readLine().split(&quot; &quot;);
  7. for(int i = 0; i &lt; n; i++)
  8. {
  9. X[i] = Integer.parseInt(elem[n]);
  10. Y[i] = Integer.parseInt(elem[n]);
  11. }
  12. System.out.println(X, Y);
  13. }

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]。例如:

  1. import java.io.*;
  2. import java.util.*;
  3. public class App {
  4. public static void main(String[] args) throws IOException {
  5. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  6. int n = Integer.parseInt(reader.readLine());
  7. double[] X = new double[n];
  8. double[] Y = new double[n];
  9. for (int i = 0; i < n; i++) {
  10. String[] elem = reader.readLine().split(" ");
  11. X[i] = Integer.parseInt(elem[0]);
  12. Y[i] = Integer.parseInt(elem[1]);
  13. }
  14. System.out.println(Arrays.toString(X));
  15. System.out.println(Arrays.toString(Y));
  16. }
  17. }
英文:

You have to read your lines inside the for loop. X[i] corresponds to elem[0]; Y[i] corresponds to elem[1]. For example:

  1. import java.io.*;
  2. import java.util.*;
  3. public class App {
  4. public static void main(String[] args) throws IOException {
  5. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  6. int n = Integer.parseInt(reader.readLine());
  7. double[] X = new double[n];
  8. double[] Y = new double[n];
  9. for (int i = 0; i &lt; n; i++) {
  10. String[] elem = reader.readLine().split(&quot; &quot;);
  11. X[i] = Integer.parseInt(elem[0]);
  12. Y[i] = Integer.parseInt(elem[1]);
  13. }
  14. System.out.println(Arrays.toString(X));
  15. System.out.println(Arrays.toString(Y));
  16. }
  17. }

答案2

得分: 0

这部分不正确:

  1. for(int i = 0; i &lt; n; i++)
  2. {
  3. X[i] = Integer.parseInt(elem[n]);
  4. Y[i] = Integer.parseInt(elem[n]);

看起来应该是这样的:

  1. for(int i = 0; (i - 1)&lt; n; i+= 2) // 每次迭代从elem中读取两个值,因此增加2。将检查条件改为i - 1,以修复奇数个条目的情况,否则会导致elem中在i/2 + 1位置上的ArrayOutOfBoundsException。
  2. {
  3. X[i/2] = Integer.parseInt(elem[i]);
  4. Y[i/2] = Integer.parseInt(elem[i] + 1);

(此处我的回答中存在错误,已经进行了修正)

或者可以选择:

  1. for(int i = 0; i &lt; n; i++)
  2. {
  3. if (i % 2 == 0) X[i/2] = Integer.parseInt(elem[i]);
  4. else Y[i/2] = Integer.parseInt(elem[i]);
英文:

This part is not right:

  1. for(int i = 0; i &lt; n; i++)
  2. {
  3. X[i] = Integer.parseInt(elem[n]);
  4. Y[i] = Integer.parseInt(elem[n]);

It seems like it should be

  1. for(int i = 0; (i - 1)&lt; 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
  2. {
  3. X[i/2] = Integer.parseInt(elem[i]);
  4. Y[i/2] = Integer.parseInt(elem[i] + 1);

(There was a bug in my answer here. It has been fixed)

Alternatively:

  1. for(int i = 0; i &lt; n; i++)
  2. {
  3. if (i % 2 == 0) X[i/2] = Integer.parseInt(elem[i]);
  4. else Y[i/2] = Integer.parseInt(elem[i]);

huangapple
  • 本文由 发表于 2020年9月28日 06:12:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64093791.html
匿名

发表评论

匿名网友

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

确定