For loop在Java中没有输出。

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

For loop does not give any output in Java

问题

以下是翻译好的内容:

我对开发非常陌生,正在尝试编写一个小程序,但似乎无法让这个for循环工作起来:

public class test {

    public static void main(String[] args){

    double desejadaT5 = Console.userInput("What is the desejada value?");

        for (double i = desejadaT5; i <= 0; i = i * 0.367)
            System.out.println("value of i is " + i);

    }
}

这是我创建的Console类,用于接收用户输入。

import java.util.Scanner;

public class Console {

    public static double userInput(String prompt){

        Scanner scanner = new Scanner(System.in);
        double value;
        System.out.println(prompt);
        value = scanner.nextDouble();
        return value;
    }
}

有人可以告诉我为什么这没有给我任何输出吗?我本来期望当我将例如 333 输入到 desejadaT5 时,我会得到类似这样的输出: 333, 122, 44, 16, 5, 1.

感谢帮助。

英文:

I am very new to development and I am trying to write a little program, but I cannot seem to get this for loop working:

public class test {

    public static void main(String[] args){

    double desejadaT5 = Console.userInput(&quot;What is the desejada value?&quot;);

        for (double i = desejadaT5; i &lt;= 0; i = i * 0.367)
            System.out.println(&quot;value of i is &quot; + i);

    }
}

Here is the class Console I created to receive the user input.

import java.util.Scanner;

public class Console {

    public static double userInput(String prompt){

        Scanner scanner = new Scanner(System.in);
        double value;
        System.out.println(prompt);
        value = scanner.nextDouble();
        return value;
    }
}

Can anyone tell me why this is not giving me any output? I was expecting that when I put, for example, 333 in a desejadaT5, I would get an output like: 333, 122, 44, 16, 5, 1.

Thanks for the help.

答案1

得分: 1

如果您将输入输入为333,那么您的循环条件失败(i<=0),因为333不小于或等于0,这会使您退出循环。

英文:

If you are entering input as 333 then, your loop condition fails (i<=0) as 333 is not less than or equal to 0 and it throws you out of the loop

huangapple
  • 本文由 发表于 2020年8月18日 01:01:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63455399.html
匿名

发表评论

匿名网友

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

确定