Java 当尝试在for循环中递减i时,无限循环

Java 当尝试在for循环中递减i时,无限循环,java,Java,我有一个计数控制回路,用于添加用户输入的一系列5个数字。我在它里面有一个决策结构,可以拒绝任何奇数或负数,但可以向累加器中添加任何偶数正数。为了做到这一点,我告诉程序,如果输入了奇数或负数,则将I减1,这样循环仍然接受5个正偶数。相反,它最终接受无限多的数字(即陷入无限循环)。有人能解释为什么这不起作用/为什么它在我身上无限循环吗 这是我的密码: public static double numberRun(){ //variable to store user input String use

我有一个计数控制回路,用于添加用户输入的一系列5个数字。我在它里面有一个决策结构,可以拒绝任何奇数或负数,但可以向累加器中添加任何偶数正数。为了做到这一点,我告诉程序,如果输入了奇数或负数,则将I减1,这样循环仍然接受5个正偶数。相反,它最终接受无限多的数字(即陷入无限循环)。有人能解释为什么这不起作用/为什么它在我身上无限循环吗

这是我的密码:

public static double numberRun(){
//variable to store user input
String userNumString = "null";
int userNum = 0;
//variable to store total of all user inputted numbers
double accumulator = 0;
//setting up function to read inputs
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);   
//count for 5 loops
for(int i=1; i<=5; i++) {
//if the total of the numbers entered so far is < 100, accept more numbers -
- doesnt affect initial number because accumulator is initialized to 0
if (accumulator < 100) {
System.out.println("Please enter a number");
//count controlled loop to ensure no more than 5 numbers are entered
 try {
  userNumString = reader.readLine();
  userNum = Integer.parseInt(userNumString);
    System.out.println(userNum);
      } catch (Exception e){
  System.out.println("Error reading from user");
}
  //reject odd or negative numbers, if a number is rejected set counter 
  back 1 to ensure 5 valid numbers total are inputted
    if (0 <= userNum) {
      if ((userNum % 2) == 0) {
        accumulator = accumulator+userNum;
      } else
        System.out.println("I'm sorry, odd numbers are not allowed");
          i=i-1;
    } else if (userNum < 0) {
         System.out.println("I'm sorry, negative numbers are not allowed");
         i=i-1;
           }
    //when the total of all user inputted numbers is ≥ 100, stop the count 
    controlled loop (stop accepting numbers)
  } else if (accumulator >= 100) {
     i=5;
  }
   }
  //return the total for the run
  return accumulator;

}
publicstaticdoublenumberrun(){
//用于存储用户输入的变量
字符串userNumString=“null”;
int userNum=0;
//用于存储所有用户输入号码总数的变量
双累加器=0;
//设置读取输入的功能
InputStreamReader输入=新的InputStreamReader(System.in);
BufferedReader reader=新的BufferedReader(输入);
//计算5个循环
对于(int i=1;i
i=i-1
不是
else
的一部分。它每次都会执行

  • 使用代码格式化程序
  • 始终使用
    {}
    ,如果
    其他
    时,切勿裸露
  • i=i-1
    不是
    else
    的一部分。它每次都会执行

  • 使用代码格式化程序
  • 始终使用
    {}
    ,如果
    其他
    时,切勿裸露

  • 它将是0>=userNum而不是0我同意@Thilo的答案,你需要检查IFs和ELSEs的上下文;顺便说一句,重新插入你的代码。它将是0>=userNum而不是0我同意@Thilo的答案,你需要检查IFs和ELSEs的上下文;顺便说一句,重新插入你的代码。就是这样!谢谢!我正在使用Java博士,所以它告诉我什么'包含在我的大括号中(不确定这是否与代码格式化程序相同,我对此很陌生)。不确定我怎么会错过它,但我必须更加小心地将{}与我的else语句一起使用!此外,I=--我将做与I=(I-1)相同的事情,对吗?我对I--vs--I,I++vs++有点困惑,就是这样!谢谢!我使用的是Dr.Java,所以它告诉我大括号中包含了什么(不确定这是否与代码格式化程序相同,我对这个很陌生)。不确定我怎么会错过它,但我必须更加小心地使用{}用我的else语句!还有,i=--我将做与i=(i-1)相同的事情,对吗?我对i--vs--i和i++vs++i有点困惑
    } else
      System.out.println("I'm sorry, odd numbers are not allowed");
      i=i-1;