简体   繁体   English

Java的新手,学习速度很快,但无法弄清楚为什么我的for循环不会打印

[英]New to Java, learning quick, but can't figure out why my for loops wont print

public class poundtokilogram {
public static void main(String[] args) {
    System.out.println("Kilograms     Pounds   |   Pounds     Kilograms");
    System.out.println("-----------------------------------------------");

    int kilos = 1;
    int pounds = 20;

    for ( kilos = 1; kilos > 200; kilos++ ) {
        for ( pounds = 20; pounds > 515; pounds++) {
            double kiloc = pounds * .453;
            double poundc = kilos * 2.2;
            System.out.print(kilos + "    " + poundc + " | " + pounds + "     " + kiloc + "\n");
        }
    }
}

} }

Here's the code, essentially I'm trying to print out a conversion chart with kilograms to pounds on one side and pounds to kilograms on the other (it's just an exercise in a book I'm learning from) and for some reason when I go to run this it will only print out the first two lines. 这是代码,从本质上来说,我试图打印出一张转换表,其中一侧为千克到磅,另一侧为磅到千克(这只是我正在学习的书中的一种练习),出于某种原因,运行它只会打印出前两行。 BTW, first time posting to StackOverflow, suggestions on how to be more clear would be appreciated. 顺便说一句,第一次发布到StackOverflow,关于如何变得更清晰的建议将不胜感激。

you are checking kilos are greater than 200 and pounds greater than 515 您正在检查kilos是否大于200,磅是否大于515

change this 改变这个

for ( kilos = 1; kilos > 200; kilos++ ) { 
for ( pounds = 20; pounds > 515; pounds++) {

to

for ( kilos = 1; kilos < 200; kilos++ )
for ( pounds = 20; pounds < 515; pounds++) {

Your outer loop is not even starting because the condition is that kilos must be greater than 200. I suppose you want to say: kilos < 200 您的外部循环甚至还没有开始,因为条件是kilos必须大于200。我想说的是: kilos < 200

Also, in the inner loop you have pounds > 515 but you are not modifying pounds before the condition is evaluated. 另外,在内部循环中, pounds > 515但在评估条件之前未修改磅。 Change the condition to pounds < 515 (that is what I think you want to execute) 将条件更改为pounds < 515 (我认为您要执行此操作)

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

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