我如何在for循环外使用i变量?

时间:2023-01-24 18:26:27

I want to use the value of 'i' in a certain point, like in the example below. What i have tried to do didnt work, what else can i do?

我想在某一点使用'i'的值,如下例所示。我试图做的不起作用,我还能做什么?

 import java.util.Scanner;
 public class Main {

    public static void main(String[] args) {
       Scanner reader = new Scanner(System.in);


       int i =1;
       int r;


       for(i=1;i<=100;i++){

          num = reader.nextInt();

          if (num == 55){
             r=i;
          }
          System.out.println(" 55 was typed in when i was equals to: "+r);
       }
    }
 }

1 个解决方案

#1


0  

I believe you have to initialize r where you declare it. Currently at Uni and learning myself though but I think that is it.

我相信你必须初始化你声明它的地方。目前在Uni学习自己,但我认为就是这样。

EDIT: Someone appears to have mentioned it in the comment too.

编辑:有人似乎也在评论中提到过它。

#1


0  

I believe you have to initialize r where you declare it. Currently at Uni and learning myself though but I think that is it.

我相信你必须初始化你声明它的地方。目前在Uni学习自己,但我认为就是这样。

EDIT: Someone appears to have mentioned it in the comment too.

编辑:有人似乎也在评论中提到过它。