JetBrains Academy: Branching statements - Kamil-Jankowski/Learning-JAVA GitHub Wiki

JetBrains Academy: Branching statements

Even or odd:

Given a sequence of natural numbers. For each number of the sequence output "even" if the number is even, otherwise, "odd". If the number is equal to 0, the program must stop reading and processing numbers.

import java.util.*;

class EvenOrOdd {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int input = scanner.nextInt();

        while (input != 0){
            if(input % 2 == 0){
                System.out.println("even");
            } else {
                System.out.println("odd");
            }
            input = scanner.nextInt();
        }
    }
}

Bus tour:

A bus tour of Europe has been very successful. Due to an increase in the number of people that want to go on a tour, the tour company decided to increase the height of the bus. The new height of the bus is exactly N centimeters.

But the tour’s route runs under a lot of bridges, and there is a chance that the bus will crash into one of these bridges. Can you find out if this will happen?

The first line of the input contains the height of the bus and number of bridges under which the bus passes. The second line contains heights of these bridges.

You should output "Will not crash" if everything will be all right; otherwise, output "Will crash on bridge i" (where i is a number of a bridge) into which the bus will crash. If the height of a bridge equals the height of the bus, the bus will crash.

import java.util.*;

public class BusTour {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int busHeight = scanner.nextInt();
        int amountOfBridges = scanner.nextInt();

        for (int i = 0; i < amountOfBridges; i++){
            int bridgeHeight = scanner.nextInt();
            int bridgeNumber = i+1;
            if (busHeight >= bridgeHeight){
                System.out.println("Will crash on bridge " + bridgeNumber);
                break;
            } else {
                if (i == amountOfBridges-1){
                    System.out.println("Will not crash");
                }
            }
        }
    }
}

The (un)-ordered sequence:

Write a program that reads a sequence of integer numbers and outputs true if the sequence is ordered (in ascending or descending order), otherwise, false. Keep in mind, if a number has the same value as the following number, it does not break the order.

The sequence ends with 0. Do not consider this number as a part of the sequence. The sequence always has at least one number (excluding 0).

import java.util.*;

class Unordered {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<Integer> lista = new ArrayList<>();
        int input;
        boolean maleje = false;
        boolean rosnie = false;
        
        while (scanner.hasNextInt()){
            input = scanner.nextInt();
            if (input == 0){
                break;
            } else {
                lista.add(input);
            }
        }
        if (lista.size() > 1){
            for (int i = 0; i < lista.size()-1; i++){
            if (lista.get(i) - lista.get(i+1) > 0){
                    maleje = true;
                } else if (lista.get(i) - lista.get(i+1) < 0){
                    rosnie = true;
                }
            }
        
            if (maleje == rosnie){
                System.out.println("false");
            } else if (maleje ^ rosnie){
                System.out.println("true");
            }
        } else {
            System.out.println("true");
        }
    }
}

The integer barrier:

Write a program that reads a sequence of integer numbers in a loop and adds up all numbers. If a new number is equal to 0, the program must stop the loop and output the accumulated sum. When the sum is equal or exceeded 1000 (the barrier), the program should also stop and output the value equal to sum - 1000.

Note, the input can contain extra numbers. Just ignore them.

import java.util.Scanner;

class Barrier {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int input;
        int sum = 0;
        int barrier = 1000;

        while(scanner.hasNextInt()){
            input = scanner.nextInt();
            sum += input;
            if (input == 0){
                System.out.println(sum);
                break;
            }
            if (sum >= barrier){
                System.out.println(sum-barrier);
                break;
            }
        }
    }
}

The Sequence:

Write a program that prints a part of the sequence 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 ... (the number is repeated as many times, to what it equals to). The input to the program is a positive integer n: the number of the elements of the sequence the program should print. Output the sequence of numbers, written in a single line, space-separated.

import java.util.Scanner;

class Sequence {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int counter = 0;
        int i = 1;
	boolean running = true;

        while (i <= n && running) {
            int j = 1;
            while (j <= i){
                System.out.print(i + " ");
                counter++;
                j++;
            	if (counter >= n){
		    running = false;
		    break;
		}
            }
            i++;
        }
    }
}

⚠️ **GitHub.com Fallback** ⚠️