简体   繁体   English

For 循环 - Java

[英]For Loops - Java

I'm sure this is super simple, but I keep getting nothing.我确定这非常简单,但我一直什么也没得到。 The instructions say:指令说:

"Write code that prints: firstNumber...2 1 Print a newline after each number. Ex: firstNumber = 3 outputs “编写打印代码:firstNumber...2 1 在每个数字后打印一个换行符。例如:firstNumber = 3 输出

3
2
1

Here's: the code:这是:代码:

public class ForLoops {
   public static void main (String [] args) {
      int firstNumber;
      int i;

      firstNumber = 3;

      for (i = 3; i <= 1; --i) {
         System.out.println(i);
     }
   }
}

My thought process is since it wants me to count down from 3 to 1, I make i equal to 3, then it will count down for everything until i equals or is less than 1 with the --i.我的思考过程是因为它想让我从 3 倒数到 1,我让 i 等于 3,然后它会倒计时,直到 i 等于或小于 1 与 --i。

If that makes sense.如果这是有道理的。

The problem is in the loop condition, that will never be True , Change the condition to this:问题出在循环条件中,永远不会为True ,将条件更改为:

i >= 1

Because in this condition:因为在这种情况下:

i <= 1

The current value of i is 3, and 3 is not smaller than ( < ) or equal to ( = ) 1 , so the condition will be False at the first place, and code inside for loop will never gets executed. i的当前值为 3,并且3不小于 ( < ) 或等于 ( = ) 1 ,因此条件首先为False ,并且永远不会执行for循环中的代码。

Also, you've created a variable firstNumber , which I believe is the starting point of counter variable so you can change this line:此外,您已经创建了一个变量firstNumber ,我相信它是计数器变量的起点,因此您可以更改此行:

for (i = 3; i <= 1; --i)

to this:对此:

for (i = firstNumber; i >= 1; --i)

You logic here你的逻辑在这里

for (i = 3; i <= 1; --i) {
     System.out.println(i);
}

is

a) start with i being three

b) if i <= 1
   then print
   decrement i

c) loop again

Obviously the loop will never be entered into显然永远不会进入循环

try尝试

for (i = 3; i >= 1; --i) {
     System.out.println(i);
}

You should write you code like:您应该编写如下代码:

public class ForLoops {
    public static void main (String [] args) {

       int firstNumber;
       int i;

       firstNumber = 3;

       for (i = firstNumber; i >= 1; --i) {
          System.out.println(i);
       }
   }
}

You have simple mistake in for loop condition.您在 for 循环条件中有一个简单的错误。

Shouldn't it be greater than or equal to (>=) as you are going in reverse direction.当您向相反方向前进时,它不应该大于或等于 (>=)。

change it to:将其更改为:

for (i = 3; i >= 1; --i)

Your mistake seems to be, that you used a <= (smaller or equal) which is never true (3 can never be smaller or equal to 1).你的错误似乎是,你使用了一个永远不正确的 <= (小于或等于)(3 永远不能小于或等于 1)。 So you have to use a >=所以你必须使用 >=

And you can't have space in the name of your variable ( int first Numer; has to be int firstNumer;)并且您的变量名称中不能有空格( int firstNumer; 必须是 int firstNumer;)

public class Test{
    public static void main(){
         int firstNumber;
         firstNumber = 3;

         for(int i = 3; i >= 1; i--){
              System.out.println(i);
         }
    }
}

Sorry for my bad spelling, but I am on my phone right now.抱歉我的拼写错误,但我现在正在使用手机。 I hope I could help you.我希望我能帮助你。

this is correc code:这是正确的代码:

   public class ForLoops {
    public static void main (String [] args) {
      int firstNumber;
      int i;

      firstNumber = 3;

      i = firstNumber;   

      for (i = 3; i <= 1; --i) {
         System.out.println(i);
     }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM