timi先生 2020-05-28 18:31 采纳率: 50%
浏览 124

这个为什么会无限循环!

public class Solution {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int i = 0;
while(a!=-1){
i=i+a;
}
while(a==-1){
int j =i-1;
System.out.println(j);

}
 }
}
  • 写回答

1条回答 默认 最新

  • threenewbee 2020-05-28 22:52
    关注

    while(a!=-1){
    i=i+a;
    }
    你这个循环里没有修改a,判断条件又是a!=-1
    一旦进入,自然就死循环了

    评论

报告相同问题?