如何在for循环中通过除以2进行迭代(Java)

huangapple go评论67阅读模式
英文:

How to interate by divided by 2 in for loop (Java)

问题

我正在遇到一个问题,无法弄清楚如何在Java的for循环中以0.5为步长进行迭代。例如,(i*0.5) { 是不可能的。非常感谢您的帮助,如果问题看起来很愚蠢,我感到抱歉。

英文:

I am having trouble figuring out how i would iterate by 0.5 in a for loop in Java. For example, (i*0.5) { is not possible. Any help is appreciated, sorry if question seems dumb

答案1

得分: 2

	public static void main(String[] args)  {
		double i=1000;
		System.out.println("While循环的结果");
		while(i > 10) { //While loop
			System.out.println("数值 "+i);
			i = i*0.5;
		}
		
		System.out.println("For循环的结果");
		for(double j=1000; j>10; j=j*0.5) {
			System.out.println("数值 "+j);
		}		
	}

**输出**

    While循环的结果
    数值 1000.0
    数值 500.0
    数值 250.0
    数值 125.0
    数值 62.5
    数值 31.25
    数值 15.625
    
    For循环的结果
    数值 1000.0
    数值 500.0
    数值 250.0
    数值 125.0
    数值 62.5
    数值 31.25
    数值 15.625
英文:
public static void main(String[] args)  {
	double i=1000;
	System.out.println("Result of While loop");
	while(i > 10) { //While loop
		System.out.println("Value "+i);
		i = i*0.5;
	}
	
	System.out.println("Result of For loop");
	for(double j=1000; j>10; j=j*0.5) {
		System.out.println("Value "+j);
	}		
}

output

Result of While loop
Value 1000.0
Value 500.0
Value 250.0
Value 125.0
Value 62.5
Value 31.25
Value 15.625

Result of For loop
Value 1000.0
Value 500.0
Value 250.0
Value 125.0
Value 62.5
Value 31.25
Value 15.625

答案2

得分: 0

循环会一直迭代,直到满足某个条件,并且在每次迭代中,用于检查条件是否满足的变量会被修改。但是这个变量不一定是整数,也不一定每次迭代都要增加一。请看下面的代码:

public class MyClass {
    public static void main(String args[]) {
      for (float f = 0.0f; f <= 2; f = f+0.5f) {
          System.out.println(f);
      }
    }
}

输出

0.0

0.5

1.0

1.5

2.0

所以正如你所见,这会按照你想要的0.5的增量进行迭代。当然,你也可以进行递减操作。

英文:

Loops iterate until a condition is met, and on each iteration the variable that is checked to see if the condition is met is modified. But that variable doesn't have to be an integer, and it doesn't have to be incremented by one on each step. See the following code:

public class MyClass {
    public static void main(String args[]) {
      for (float f = 0.0f; f &lt;= 2; f = f+0.5f) {
          System.out.println(f);
      }
    }
}

Output

0.0

0.5

1.0

1.5

2.0

So as you can see this iterates in increments of 0.5 as you want. And obviously you can do decrements as well.

答案3

得分: 0

没有愚蠢的问题!这个的语法是:

for (var i = 10f; i > 2; i = i * 0.5f) {
    System.out.println(i);
}

其中的 "f" 告诉编译器这是一个浮点数,而不是双精度数。但当然你也可以使用双精度数。

英文:

No dumb question! The syntax for this is:

for (var i = 10f; i &gt; 2; i = i * 0.5f) {
    System.out.println(i);
}

The „f“ tells the compiler that it’s a float, not a double. But you can of course use double, too.

huangapple
  • 本文由 发表于 2020年10月9日 20:40:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64280283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定