【问题标题】:Is there a way to loop through a variables using a 'for' loop?有没有办法使用“for”循环来循环变量?
【发布时间】:2023-03-04 14:17:01
【问题描述】:
int oct1 = scan.nextInt();
int oct2 = scan.nextInt();
int oct3 = scan.nextInt();
int oct4 = scan.nextInt();

for(int i=1; i<5; i++){

  System.out.println(oct+i);
}

这是我的代码,基本上,我只是想知道如何循环遍历每个 oct 变量。

我显然知道当前的打印不起作用,但是有没有办法让for 循环可以打印每个变量,而无需仅使用四行来打印所有变量?

【问题讨论】:

标签: java loops for-loop


【解决方案1】:

理想情况下,要处理多个值并对其进行迭代,您应该使用List 实现,ArrayList 就是其中之一。这可能是另一种方式,更具可扩展性和效率。

List<Integer> octList = new ArrayList<Integer>();

octList.add(scan.nextInt());
octList.add(scan.nextInt());
octList.add(scan.nextInt());
octList.add(scan.nextInt());

for (int i = 0; i < octList.size(); i++) {
  System.out.println(octList.get(i));
}

【讨论】:

  • List&lt;Object&gt; 不是List&lt;primitive&gt;
  • 另外,请参阅this Q&A了解上述详细信息。
【解决方案2】:

您可以将它们存储在一个数组中,然后根据索引对其进行引用:

int[] arr = new int[]{scan.nextInt(), scan.nextInt(), scan.nextInt(), scan.nextInt(), scan.nextInt()};
for(int i = 0; i < arr.length; i++){
    System.out.println(arr[i]);
}

【讨论】:

    【解决方案3】:

    Oracle 有一个非常好的数组介绍教程。 Oracle Tutorial On Arrays

    有两种使用标准数组的方法。在我的第一个解决方案中,我假设值 oct1 ... oct4 已被填充。但是,在以下示例之后是另一个解决方案,它不假设这一点,并且也使用 Scanner 接受输入。

        int[] values ={oct1, oct2, oct3, oct4};
        // Two examples:
    
        // Using a 'for each' loop to display values
        for(int value : values){
            System.out.println(value);
        }
    
        // Using a standard 'for' loop to display values
        for(int i; i < values.length; i++){
          System.out.println(values[i]);
        }
    

    此解决方案使用数组并使用 Scanner 读取它,但您必须提前知道要读取多少个值,否则您将需要学习列表。

       int count = 4;
       int[] values = new int[count];
       Scanner scan = new Scanner(System.in);
    
       // Using a standard for loop to read in values
       for(int i; i < values.length; i++){
         System.out.println("Enter a number");
         values[i] = scan.nextInt();
       }
    
       // Using a standard 'for' loop. Assume this
       // would come later in the program because
       // you would otherwise read in and display 
       // the values in a single loop.
       for(int i; i < values.length;i++){
         System.out.println(values[i]);
       }
    

    【讨论】:

      【解决方案4】:

      对于local variablesmethod parameter 这是不可能的。您可以使用ArrayList 来存储值并进行迭代。

      例如,使用array

      // Declare an array of size 4
      int oct[] = new int[4];
      
      // Take input from user 4 times
      for(int i = 0; i < 4; i++){
          oct[i] = scan.nextInt();
      }
      
      // Print the array
      for(int i = 0; i < 4; i++){
          System.out.println(oct[i]);
      }
      

      同时检查 Is there any way to loop though variable names?

      【讨论】:

        【解决方案5】:

        有两种使用标准数组的方法。在我的第一个解决方案中,我假设值 oct1 ... oct4 已被填充。但是,下面是一个不假设这一点的解决方案。

            int[] values ={oct1, oct2, oct3, oct4};
            //Two Examples:
        
            //Using a for each loop
            for(int value : values){
                System.out.println(value);
            }
            
            //Using a standard for loop 
            for(int i; i < values.length;i++){
              System.out.println(values[i]);
            }
        

        此解决方案假定没有数组,但您必须提前知道要读取多少个值,否则您将需要学习列表。请记住在您正在使用此代码的类文件的顶部使用 import java,util.Scanner; 导入扫描仪。

           int count = 4;
           int[] values = new[count];
           Scanner scan = new Scanner(System.in);
                
            
           //Using a standard for loop
           //Assume this would go later in the
           //program or else use a single loop
           //to read in and display the values.
            for(int i; i < values.length;i++){
              System.out.println("Enter a number");
              values[i] = scan.nextInt();
            }
          
            //Using a standard for loop
            for(int i; i < values.length;i++){
              System.out.println(values[i]);
            }
        

        【讨论】:

          【解决方案6】:

          使用地图命名值:

          Map<String, Integer> values = new LinkedHashMap<>();
          for (int i = 1; i < 5; i++) {
              values.put("oct" + i, scan.nextInt());
          }
          
          for (Map.Entry<String, Integer> value : values) {
             System.out.println(value.getValue());
          }
          

          或者也输出他们的名字:

          for (Map.Entry<String, Integer> value : values) {
             System.out.println(value.getKey() + " = " + value.getValue());
          }
          

          然后得到它们:

          int v = values.get("oct2");
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-11-30
            • 1970-01-01
            • 2010-12-01
            相关资源
            最近更新 更多