英文:
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("What is the desejada value?");
for (double i = desejadaT5; i <= 0; i = i * 0.367)
System.out.println("value of i is " + 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论