Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我不明白为什么我的程序没有循环和打印_Java_Oop_Loops - Fatal编程技术网

Java 我不明白为什么我的程序没有循环和打印

Java 我不明白为什么我的程序没有循环和打印,java,oop,loops,Java,Oop,Loops,程序只输出歌曲《墙上的九十九瓶啤酒》。它编译时没有错误,但在我的代码中没有发生任何事情。我认为这与我如何设置最后两种方法的参数以及我的实例变量有关,但我在理解这一点时遇到了一些困难 package beersong; public class BeerSong { private int numBeerBottles; private String numberInWords; private String secondNumberInWords; priva

程序只输出歌曲《墙上的九十九瓶啤酒》。它编译时没有错误,但在我的代码中没有发生任何事情。我认为这与我如何设置最后两种方法的参数以及我的实例变量有关,但我在理解这一点时遇到了一些困难

package beersong;

public class BeerSong

{
    private int numBeerBottles;
    private String numberInWords;
    private String secondNumberInWords;
    private int n;

    private String[] numbers = {"", "one", "two", "three", "four", "five", 
        "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen",
        "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
    };

    private String[] tens = {"", "", "Twenty", "Thirty", "Forty", "Fifty",
        "Sixty", "Seventy", "Eighty", "Ninety" 
    };

    //constructor
    public BeerSong (int numBeerBottles)
    {
        this.numBeerBottles = numBeerBottles;
    }

    //method to return each line of song
    public String convertNumberToWord(int n)
    {
        if(n<20)
        {
            this.numberInWords = numbers[n];
        }

        else if (n%10 == 0)
        {
            this.numberInWords = tens[n/10];
        }

        else 
        {
            this.numberInWords = (tens[(n - n%10)] + numbers[n%10]);
        }

        return this.numberInWords;
    }

    //method to get word for n-1 beer in song
    public String getSecondBeer(int n)
    {
        if((n-1)<20)
        {
            this.secondNumberInWords = numbers[(n-1)];
        }

        else if ((n-1)%10 == 0)
        {
            this.secondNumberInWords = tens[(n-1)/10];
        }

        else
        {
            this.secondNumberInWords = (tens[((n-1) - (n-1)%10)] + numbers[(n-1)%10]);
        }
        return this.secondNumberInWords;
    }

    //method to actually print song to screen
    public void printSong()
    {
        for (n=numBeerBottles; n==0; n--)
        {
        System.out.println(convertNumberToWord(n) + " bottles of beer on the "
                + "wall. " + convertNumberToWord(n) + " bottles of beer. "
                + "You take one down, pass it around "
                + getSecondBeer(n) + " bottles of beer on the wall");
        System.out.println();
        }
    }

    public static void main(String[] args) 
    {
        BeerSong newsong = new BeerSong(99);
        newsong.printSong();
    }
}

首次运行时,循环条件不太可能为真:

for(n=numBeerBottles; n==0; n--)
也就是说,为了执行循环,n必须是0。这似乎不正确,因为你在墙上背诵N瓶啤酒

你要写的是n必须大于或等于0

for(n = numBeerBottles; n >= 0; n--)
我怀疑您的for循环设置不正确

for (n=numBeerBottles; n==0; n--)
它基本上是说,对于n=numberbottles,而n==0不需要n-

试试看

for (n=numBeerBottles; n >= 0; n--)

相反…

我认为在这种情况下,while循环更直观,动词适合你在英语中所说的

n = numBeerBottles;
while (n >= 0) 
{
// code
   n--;
 }

n==0必须是n>=0: