简体   繁体   English

Java中for循环内的if语句

[英]If statement inside a for loop in Java

I'm new in Java and trying to do some trivial stuffs and exercises, I came with this bump though, I want the word "ducks" to be singular when the output reaches 1 or 0, here is my code: 我是Java的新手,正在尝试做一些琐碎的事情和练习,但是我遇到了这个麻烦,我希望当输出达到1或0时,“ ducks”一词是单数,这是我的代码:

public class FiveLittleDucks {

    public static void main(String[] args) {

        String word = "";

        System.out.println("The story of the 5 little ducks");
        for(int duck = 5 ; duck>0 ; duck--) {
            if(duck == 1 || duck == 0) {
                word = "duck";
            } else {
                word = "ducks";
            }
            System.out.printf("%d little %s went out one day, over the hills and far away, mother duck said quack, quack, quack, quack", duck, word);
            System.out.printf(" but only %d little %s went back\n", duck-1, word);
        }
    }
}

here is the output: The story of the 5 little ducks 输出为:5只小鸭子的故事

5 little ducks went out one day, over the hills and far away, mother duck said quack, quack, quack, quack but only 4 little ducks went back 一天有5只小鸭子出山,在远处的山丘上,鸭妈妈说嘎嘎,嘎嘎,嘎嘎,嘎嘎,但只有4只鸭子回来了

4 little ducks went out one day, over the hills and far away, mother duck said quack, quack, quack, quack but only 3 little ducks went back 一天有4只小鸭子出山,在山上和远处,母鸭说嘎嘎,嘎嘎,嘎嘎,嘎嘎,但只有3只小鸭子回来了

3 little ducks went out one day, over the hills and far away, mother duck said quack, quack, quack, quack but only 2 little ducks went back 一天有3只小鸭子出山,在远处的山丘上,鸭妈妈说嘎嘎,嘎嘎,嘎嘎,嘎嘎,但只有两只鸭子回来了

2 little ducks went out one day, over the hills and far away, mother duck said quack, quack, quack, quack but only 1 little ducks went back 一天有2只小鸭子出山,在山上和远处,母鸭说嘎嘎,嘎嘎,嘎嘎,嘎嘎,但只有一只鸭子回到了

1 little duck went out one day, over the hills and far away, mother duck said quack, quack, quack, quack but only 0 little duck went back 一天有1只小鸭子出山,在远处的山丘上,母鸭说嘎嘎,嘎嘎,嘎嘎,嘎嘎,但只有0只小鸭子回来了

notice the "1 little ducks" is still in plural form whereas the bottom line is already in singular form.. thanks guys.. 注意“ 1只小鸭子”仍然是复数形式,而底线已经是单数形式..谢谢大家。

Each iteration of the loop prints two lines - one with the current counter of duck and one with duck-1 . 循环的每次迭代都会打印两行-一行使用duck的当前计数器,另一行使用duck-1 However, you assign a value to word based on duck , so when duck is 2 the word is "ducks" , even though duck-1 is 1. One way to solve this is to extract the calculation of the word from the loop and evaluate it individually: 但是,您为基于duck word分配了一个值,因此,即使duck-1为1,当duck为2时,单词也是"ducks" 。解决此问题的一种方法是从循环中提取单词的计算并求值它单独:

public static void main(String[] args) {
    System.out.println("The story of the 5 little ducks");
    for(int duck = 5 ; duck>0 ; duck--) {
        System.out.printf("%d little %s went out one day, over the hills and far away, mother duck said quack, quack, quack, quack", duck, ducksToWord(duck));
        System.out.printf(" but only %d little %s went back\n", duck-1, ducksToWord(duck-1));
    }
}

private static String ducksToWord(int duck) {
    if (duck == 1 || duck == 0) {
        return "duck";
    } 
    return "ducks";
}

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

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