美文网首页向java势力低头
head first java第二版第14页代码改进

head first java第二版第14页代码改进

作者: 李药师_hablee | 来源:发表于2020-06-25 12:47 被阅读0次

    原代码

    int beerNum=99;
            String word="bottles";
            
            while(beerNum>0) {
                if(beerNum==1) {
                    word="bottle";//单数的瓶子
                }
                System.out.println(beerNum+" "+word+" of beer on the wall");
                System.out.println(beerNum+" "+word+" of beer.");
                System.out.println("Take one down.");
                System.out.println("Pass it around.");
                beerNum = beerNum - 1;
                
                if(beerNum > 0) {
                    System.out.println(beerNum+" "+word+" of beer on the wall");
                }else {
                    System.out.println("No more bottles of beer on the wall");
                }
            }
    

    因为当有1个瓶子的时候,会输出bottles,所以改进了一下

    public static void main(String[] args) {
            // TODO Auto-generated method stub
            int beerNum=99;
            String word="bottles";
            
            while(beerNum>0) {
                if(beerNum==1) {
                    word="bottle";//单数的瓶子
                }
                System.out.println(beerNum+" "+word+" of beer on the wall");
                System.out.println(beerNum+" "+word+" of beer.");
                System.out.println("Take one down.");
                System.out.println("Pass it around.");
                beerNum = beerNum - 1;
                
    //          if(beerNum > 0) {
    //              System.out.println(beerNum+" "+word+" of beer on the wall");
    //          }else {
    //              System.out.println("No more botles of beer on the wall");
    //          }
            }
            System.out.println("No more botles of beer on the wall");
        }
    
    

    将while里面的判断注释掉,0个瓶子的结果拿到后面来

    相关文章

      网友评论

        本文标题:head first java第二版第14页代码改进

        本文链接:https://www.haomeiwen.com/subject/urdsfktx.html