java 使用 for 循环打印数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6312271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:20:05  来源:igfitidea点击:

Using a for-loop to print numbers

javafor-loop

提问by JDB

I'm trying to write a program that prints all numbers from 0 to 1,000 where the number mod 5 = 3. This is what I have so far

我正在尝试编写一个程序,打印从 0 到 1,000 的所有数字,其中数字 mod 5 = 3。这就是我目前所拥有的

public class NewMain {
public static void modNumbers(int i)
{

}

public static void main(String[] args) {

    for(int i = 0; i > 1000; i++)
    {
        if(i%5 = 3)
        {
        System.out.println(i);
        }
    }

 }
}

I'm not getting any output, so I know I'm doing something wrong. Should I have a return type in the method?

我没有得到任何输出,所以我知道我做错了什么。我应该在方法中有返回类型吗?

回答by Cooper

Change it from:

将其更改为:

for (int i = 0; i > 1000; i++)

to

for (int i = 0; i < 1000; i++)

Essentially, the loop never begins because the loop invariant is false (as i is less than 1000), and your print statement is never reached because the loop never initializes.

本质上,循环永远不会开始,因为循环不变式为假(因为 i 小于 1000),并且永远不会到达您的打印语句,因为循环永远不会初始化。

Additionally, as WTP mentioned, use == for numeric comparisons (an important note is that you use string1.equals(string2) or compareTo with Strings, just for your future reference).

此外,正如 WTP 所提到的,使用 == 进行数字比较(一个重要的注意事项是您将 string1.equals(string2) 或 compareTo 与字符串一起使用,仅供您将来参考)。

回答by Jochen Bedersdorfer

for(int i = 0; i < 1000; i++)

for(int i = 0; i < 1000; i++)

回答by melhosseiny

Here's what you have to do:

这是你必须做的:

  1. Change i > 1000to i <= 1000as istarts with 0 and increments up to 1000.
  2. Change i%5 = 3to i%5 == 3as the first is doing an assignment while the second is comparing for equality.
  1. 更改i > 1000i <= 1000asi从 0 开始并递增到 1000。
  2. 更改i%5 = 3为,i%5 == 3因为第一个正在执行任务,而第二个正在比较相等性。

回答by ratchet freak

if(i%5 == 3)

single = is for assignment

单个 = 用于赋值

回答by ratchet freak

This:

这:

if(i%5 = 3)

Has to be this:

必须是这样的:

if(i%5 == 3)

You are trying to set something instead of comparing it.

您正在尝试设置某些东西而不是比较它。

回答by Brian Patterson

Need to change the > in your for statement to a < and also the = in your if statement is an assignment operator. Use a comparative operator == or === ... Hope this helps. :)

需要将 for 语句中的 > 更改为 <,并且 if 语句中的 = 是赋值运算符。使用比较运算符 == 或 === ...希望这会有所帮助。:)

回答by Cosmin Cosmin

for (int i = 0; i <1000; i++)

for (int i = 0; i <1000; i++)

回答by idungotnosn

You have two problems:

你有两个问题:

1.

1.

for(int i = 0; i > 1000; i++)

The for loop only starts when i is greater than 1000, but the initial value for i is less than one thousand. The code will not work.

for 循环只在 i 大于 1000 时开始,但 i 的初始值小于 1000。该代码将不起作用。

2.

2.

if(i%5 = 3)

You need this to have two equal signs so that it becomes a statement that returns a true or false value. Your code would look like this after making the corrections:

您需要它具有两个等号,以便它成为返回真值或假值的语句。进行更正后,您的代码将如下所示:

for(int i = 0; i < 1000; i++)
{
    if(i%5 == 3)
    {
    System.out.println(i);
    }
}

回答by Karel Petranek

Another little detail:

还有一个小细节:

i%5 = 3

should be

应该

i%5 == 3