D
D
Dmitry2020-09-17 17:29:30
Java
Dmitry, 2020-09-17 17:29:30

Why doesn't while loop work in java?

Please help with cycle.
I just started to learn ...
I read, googled, I still don’t understand.
Says "java: not a statement"

public static void main(String[] args) {
  
        int beerNum = 99;
        String word = "бутылок";

        while (beerNum > 0) {
            if (beerNum == 1) {
                word = "бутылка";
                beerNum = beerNum - 1;
            }
                System.out.println(beerNum + " " + word + " пива на стене");
                System.out.println(beerNum + " " + word + " пива.");
                System.out.println("возьми одну");
                System.out.println("пусти по кругу.");
                else (beerNum < 1) {
                System.out.println("Нет бутылок пива на стене.");
            }
            }

        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Orkhan, 2020-09-17
Hasanly @azerphoenix

This construction is incorrect.
else (beerNum < 1)
Either else { ... }
Eitherelse if (условие) {...}

M
Mikhail Simonishvili, 2020-09-25
@mr_molyar

You have an infinite loop, the number of bottles will not decrease, since their decrease occurs only when it is equal to 1, although initially there are 99 of them. Plus, the above remarks are also true.

public class Main {
    public static void main(String[] args) {
        int beerNum = 99;
        String word = "бутылок";

        while (beerNum > 0) {
            if (beerNum == 1) {
                word = "бутылка";
            }
            System.out.println(beerNum + " " + word + " пива на стене");
            System.out.println(beerNum + " " + word + " пива.");
            System.out.println("возьми одну");
            System.out.println("пусти по кругу.");
            beerNum = beerNum - 1;
        }
        System.out.println("Нет бутылок пива на стене.");
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question