有没有办法使用“for”循环遍历变量?

时间:2021-02-01 22:16:19

标签: java loops for-loop

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 循环可以打印每个变量,而无需仅使用四行来打印所有变量?

6 个答案:

答案 0 :(得分: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));
}

答案 1 :(得分:0)

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

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]);
}

答案 2 :(得分:0)

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]);
   }

答案 3 :(得分:0)

对于 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?

答案 4 :(得分:0)

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

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="./tictactoe.css">
</head>
<body>
   <script src="tictactoe.js"></script>
    <div class= "header">
    </div>

    <div class= "main">
        <div id = '0'>
            <div id = "00">x</div>
            <div id = "01">x</div>
            <div id = "02">x</div>
        </div>
        <div id = "1">
            <div id = "10">x</div>
            <div id = "11">x</div>
            <div id = "12">x</div>
        </div>
        <div class = "2">
            <div id = "20">x</div>
            <div id = "21">x</div>
            <div id = "22">x</div>
        </div>
    </div>

</body>
</html>

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

    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]);
    }

答案 5 :(得分:0)

使用映射来命名值:

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");