【问题标题】:How can I take multiple integer input from scanner and store each integer in separate arrays?如何从扫描仪获取多个整数输入并将每个整数存储在单独的数组中?
【发布时间】:2017-06-02 22:49:26
【问题描述】:

我正在尝试创建一个程序,该程序允许用户添加任意​​数量的数字对,方法是首先接受用户输入询问他们想要完成多少总和(2 个数字的加法),此后创建两个用户输入的任何大小的数组,然后要求用户在一行上输入他们想要添加的每对数字,并将第一个值存储在一个数组中,将第二个值存储在第二个数组中每个输入。这就是我卡住的地方,我不知道如何将用户输入作为每行上的两个 int 值并将它们存储在每个数组的相应索引中,以便稍后添加。请看下面我的代码:

import java.util.Scanner;

public class SumsInLoop {

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter the amount of sums you would like to calculate: ");
    int n = sc.nextInt();

    int a[] = new int[n];
    int b[] = new int[n];

    String[] input = new String[2];
    System.out.println("Please enter the values you would like to sum as pairs of two numbers: ");
    for (int i = 0; i < a.length; i++) {
        input = sc.nextLine().split(" ");
            int[] intinput = Arrays.asList(input).stream().mapToInt(Integer::parseInt).toArray();
a = intinput[0];
b = intinput[1];
}
}

【问题讨论】:

    标签: java arrays loops


    【解决方案1】:

    我认为您需要以这种方式更改 2 行:

    a[i] = intinput[0];
    b[i] = intinput[1];
    

    【讨论】:

      【解决方案2】:

      您正在使用 nextLine() 方法,该方法对字符串输入很有用,但它不是整数或其他原始数据的最佳解决方案。

      另外,这行代码是错误的:

      a = intinput[0];
      

      因为您将整数值存储为整数数组。 为了尊重变量类型,您必须将该值存储在 a[i] 中。

      我会这样做:

      public class SumsInLoop {
      
          public static void main(String[] args) {
      
              Scanner sc = new Scanner(System.in);
      
              System.out.println("Enter the amount of sums you would like to calculate: ");
              int n = sc.nextInt();
      
              int a[] = new int[n];
              int b[] = new int[n];
      
              for (int i = 0; i < a.length; i++) {
                  System.out.println("Please enter the values you would like to sum as pairs of two numbers: ");
                  // Read pair and store it inside i-th position of a and b arrays 
                  System.out.println("Enter first number: ");
                  a[i] = sc.nextInt();
                  System.out.println("Enter second number: ");
                  b[i] = sc.nextInt();
              }
              // Close scanner
              sc.close();
      
              // Prints every sum
              for(int i = 0; i < a.length; i++){
                  System.out.println(i + "-th sum is: " + (a[i] + b[i]));
              }
          }
      }
      

      在这里,您可以使用特定于整数数据的 nextInt() 读取每一对。

      每次你将项目存储在数组的第 i 个位置,所以最后你可以将 a[i]b[i] 相加。

      结果示例:

      Enter the amount of sums you would like to calculate: 
      4
      Please enter the values you would like to sum as pairs of two numbers: 
      Enter first number: 
      2
      Enter second number: 
      1
      Please enter the values you would like to sum as pairs of two numbers: 
      Enter first number: 
      3
      Enter second number: 
      4
      Please enter the values you would like to sum as pairs of two numbers: 
      Enter first number: 
      5
      Enter second number: 
      6
      Please enter the values you would like to sum as pairs of two numbers: 
      Enter first number: 
      7
      Enter second number: 
      8
      0-th sum is: 3
      1-th sum is: 7
      2-th sum is: 11
      3-th sum is: 15
      

      【讨论】:

      • 哇,这比我想象的要简单得多!我想我必须使用嵌套循环从两个数组中的每个位置获取值,以将每个整数相加。感谢您花费时间和精力帮助我找到解决方案。
      • 很高兴能为您提供帮助
      • 请不要替别人做功课。从长远来看,这对他们没有帮助。给他们一些提示,让他们自己解决。而且在这种情况下,您没有解释为什么 OP 的原始解决方案是错误的。
      • 我将用更多细节编辑我的答案。实际上,我只是修复了一些几乎正确的代码,但是感谢您的建议,我将更改我的方法以获取将来的答案:)
      • 这不是我的作业,只是我试图解决的一个问题,以提高我的编程技能。 :)
      【解决方案3】:

      只需使用扫描仪的nextInt() 方法读取对,它使用所有空格符号,如分隔符(不仅是行尾):

      public class SumsInLoop {
      
          public static void main(String[] args) {
      
              Scanner sc = new Scanner(System.in);
      
              System.out.println("Enter the amount of sums you would like to calculate: ");
              int n = sc.nextInt();
      
              int a[] = new int[n];
              int b[] = new int[n];
      
              System.out.println("Please enter the values you would like to sum as pairs of two numbers: ");
              for (int i = 0; i < a.length; i++) {
                  a[i] = sc.nextInt();
                  b[i] = sc.nextInt();
              }
          }
      }
      

      【讨论】:

      • 请不要替别人做功课。从长远来看,这对他们没有帮助。给他们一些提示,让他们自己解决。
      猜你喜欢
      • 2018-10-29
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 2021-12-09
      • 1970-01-01
      • 2014-05-11
      • 1970-01-01
      相关资源
      最近更新 更多