Java Exercise "Beginner"

This is a beginner's edition following the basic edition.

Beginner's edition

Create a program that lets you enter two integer values, displays the result of dividing the first value by the second value, and then displays the result of multiplying the result by the second value. All calculations should be of integer type (if not divisible, the decimal point is automatically truncated).

    public static void question21(int num1, int num2) {
        System.out.println("Q21");
        int num3 = num1 / num2;
        //Display the result of dividing the first value by the second value
        System.out.println(num3);
        //Display the result of multiplying the result by a second value
        System.out.println(num3 * num2);
    }

Create a program that lets you enter an integer value and displays OK if the value is greater than 5 and less than 20.

     *
     * @param num1
     */
    public static void question22(int num1) {
        System.out.println("Q22");
        //If the value is greater than 5 and less than 20
        if (num1 > 5 && num1 < 20) {
            //If the value is greater than 5 and less than 20
            System.out.println("The first value is OK");
        }
    }

Create a program that lets you enter an integer value and displays OK if the value is -10 or less, or 10 or more.

     *
     * @param num1
     */
    public static void question23(int num1) {
        System.out.println("Q23");
        if (num1 <= -10 || num1 >= 10) {
            //value-10 or less, or 10 or more
            System.out.println("The first value is OK");
        }
    }

Let's enter a numerical value, and create a program that displays OK if the value is -10 or more and less than 0, or 10 or more, and NG otherwise.

     *
     * @param num1
     */
    public static void question24(int num1) {
        System.out.println("Q24");
        //value-When 10 or more and less than 0
        if ((num1 >= -10 && num1 < 0) || (num1 >= 10)) {
            System.out.println("The first value is OK");
        } else {
            System.out.println("It's NG");
        }
    }

Create a program that lets you enter an integer value and displays range 1 if the value is less than -10, range 2 if the value is -10 or more and less than 0, and range 3 if the value is 0 or more.


    public static void question25(int num1) {
        System.out.println("Q25");
        //value-If less than 10
        if (num1 < -10) {
            System.out.println("range 1");
            // -10 or more and less than 0
        } else if (num1 < 0) {
            System.out.println("range 2");
            //0 or more
        } else {
            System.out.println("range 3");
        }
    }

Use the swicth-case statement to create a program that prompts you to enter an integer value and displays one if the value is 1, two if it is 2, three if it is 3, and others otherwise.

     *
     * @param num1
     */
    public static void question26(int num1) {
        System.out.println("Q26");
        //Entry example num1(1,2,3,0)
        switch (num1) {
            //Processing when the value of the expression and the value num1 match
            case 1:
                System.out.println("one");
                break;
            case 2:
                System.out.println("two");
                break;
            case 3:
                System.out.println("three");
                break;
            //What to do when the value of an expression does not match the value of any case
            default:
                System.out.println("others");
        }
    }

Let's input an integer value and create a program that calculates and displays the sum from 1 to that value. However, if a value less than or equal to 0 is entered, it will be displayed as 0.

     *
     * @param num1
     */

    public static void question27(int num1) {
        System.out.println("Q27");
        for (int i = 1; i < num1; i++) {
            System.out.println(i);
        }
        if (num1 == 0) {
            System.out.println(0);
        }
    }

Create a program that lets you enter an integer value and displays the factorial of that value. However, if a value less than or equal to 0 is entered, 1 is displayed. (Factorial method)

     *
     * @param num1
     */
    public static void question28(int num1) {
        for (int i = 1; i <= 10; i++) {
            num1 = num1 * i;
        }
        System.out.println(num1);
    }

Repeatedly create a program that prompts you to enter integer values five times and displays the sum of those values.

     *
     * @param num1
     * @param num2
     * @param num3
     * @param num4
     * @param num5
     */

    public static void question29(int num1, int num2, int num3, int num4, int num5) {
        System.out.println("Q29");
        //Show total values
        System.out.println(num1 + num2 + num3 + num4 + num5);
    }

Create a program that lets you enter integer values and displays * for that number. If the input value is 0 or less, nothing needs to be written.

     *
     * @param num1
     */
    public static void question30(int num1) {
        System.out.println("Q30");
        for (int i = 0; i < num1; i++) {
            System.out.print("*");
        }
    }

Let's input an integer value and create a program that displays the number of * with spaces every 5th.

     *If the input value is 0 or less, nothing needs to be written.
     *
     * @param num1
     */
    public static void question31(int num1) {
        System.out.println("Q31");
        for (int i = 1; i < num1; i++) {
            System.out.print("*");
            // *To be blank every 5
            if (i % 5 == 0) {
                System.out.print("\t");
            }
        }
    }

Display from 1 to 20 in order, but if it is a multiple of 5, create a program that displays bar instead of numbers.

    public static void question32() {
        System.out.println("Q32");
        for (int i = 1; i < 20; i++) {
            //For multiples of 5
            if (i % 5 == 0) {
                System.out.println("bar");
            } else {
                //When not a multiple of 5
                System.out.println(i);
            }
        }
    }

Let's input an integer value and create a program that displays other than the input value from 1 to 9.

     *
     * @param num1
     */
    public static void question33(int num1) {
        System.out.println("Q33");
        for (int i = 1; i < 10; i++) {
            //If i is not the value you entered
            if (!(i == num1)) {
                System.out.println(i);
            }
        }
    }

Let's input an integer value and create a program that displays the input value and other than the input value +1 from 1 to 9. If the input value is 9, only 9 is not displayed.

     *
     * @param num1
     */
    public static void question34(int num1) {
        System.out.println("Q34");
        //Input value and input value+If not 1
        for (int i = 1; i < 10; i++) {
            if ((!(i == num1)) && (!(i == (num1 + 1)))) {
                System.out.println(i);
            }
        }
    }

Declare an integer array of size 10 initialized with {3, 7, 0, 8, 4, 1, 9, 6, 5, 2}, have it input an integer value, and the element number is the input value. Create a program that displays the values of array elements. Checking if the input value is outside the range of array elements may be omitted.

    public static void question35(int num1) {
        System.out.println("Q35");
        // {3, 7, 0, 8, 4, 1, 9, 6, 5, 2}Declare an integer array of size 10 initialized with
        int[] array = {3, 7, 0, 8, 4, 1, 9, 6, 5, 2};
        System.out.println(array[num1]);
    }

Declare an integer type array of size 10 initialized with {3, 7, 0, 8, 4, 1, 9, 6, 5, 2}, input two integer values, and the element number is the input value. Create a program that calculates and displays the product of the values of two array elements. Checking if the input value is outside the range of array elements may be omitted.

     *
     * @param num1
     */
    public static void question36(int num1, int num2) {
        System.out.println("Q36");
        // {3, 7, 0, 8, 4, 1, 9, 6, 5, 2}Declare an integer array of size 10 initialized with
        int[] array = {3, 7, 0, 8, 4, 1, 9, 6, 5, 2};
        System.out.println(array[num1] * array[num2]);
    }

Declare an integer type array of size 10 initialized with {3, 7, 0, 8, 4, 1, 9, 6, 5, 2}, let the integer value be input, and the element number is the input value array Create a program that refers to the value of an element and then refers to and displays the value of an array element whose element number is the referenced value. Checking if the input value is outside the range of array elements may be omitted.

    public static void question37(int num1) {
        System.out.println("Q37");
        // {3, 7, 0, 8, 4, 1, 9, 6, 5, 2}Declare an integer array of size 10 initialized with
        int[] array = {3, 7, 0, 8, 4, 1, 9, 6, 5, 2};
        int num2;
        num2 = array[num1];
        System.out.println(array[num2]);
    }

Declare an integer array of size 10 initialized with {3, 7, 0, 8, 4, 1, 9, 6, 5, 2}, initially set the reference element number to 0, and refer to this. The value of the array element of the element number is displayed, then the value of the array element is set as the next element number to be referenced, the value of the array element of this next reference element number is displayed, and the value of the array element is further set. Create a program that repeats 10 times with the element number to be referenced next.

    public static void question38() {
        System.out.println("Q38");
        // {3, 7, 0, 8, 4, 1, 9, 6, 5, 2}Declare an integer array of size 10 initialized with
        int[] array = new int[]{3, 7, 0, 8, 4, 1, 9, 6, 5, 2};
        //Display the value of the array element with the referenced element number 0
        int index = 0;
        //Repeat 10 times
        for (int i = 0; i < 10; i++) {
            //Assign the value of index to the element number index
            index = array[index];
            //Output the value of the array element when the element number is a
            System.out.println(index);
        }
    }

Declare an integer array of size 10 initialized with {3, 7, 0, 8, 4, 1, 9, 6, 5, 2}, and initially set the referenced element number to 0. Display the value obtained by subtracting the value of the array element of the next element number from the value of the array element of this reference element number, and increase the reference element number by 1. Create a program that repeats this procedure 9 times. )

    public static void question39() {
        System.out.println("Q39");
        // {3, 7, 0, 8, 4, 1, 9, 6, 5, 2}Declare an integer array of size 10 initialized with
        int[] array = new int[]{3, 7, 0, 8, 4, 1, 9, 6, 5, 2};
        //Repeat 9 times
        //Current element number index
        for( int index = 0; index < 9; index++){
            /**
             * index=When 0, currentValue=3,nextIndex=1,nextValue=7
             * index=When 1 currentValue=7,nextIndex=2,nextValue=0
             * index=When 2 currentValue=0,nextIndex=3,nextValue=8
             * */
            //Array element value of the element number to be referenced currentValue
            int currentValue = array[index];
            //Existing element number(index)The next element number of nextIndex
            int nextIndex = index + 1;
            //NextValue is the value of the array element with the next element number
            int nextValue = array[nextIndex];
            //Value obtained by subtracting the value of the array element of the next element number from the value of the array element of the element number
            System.out.println(currentValue - nextValue);
        }
    }
}

Recommended Posts

Java Exercise "Beginner"
java beginner 4
java beginner 3
java beginner
Java Beginner Exercises
Java Exercise "Intermediate"
Progate Java (Beginner) Review & Summary
Java starting from beginner, override
Java, instance starting from beginner
Java starting from beginner, inheritance
Java Development Basics ~ Exercise (Array) ~
Java
[Beginner] Java basic "array" description
Solve AtCoder Beginner Contest 151 in java
Differences between "beginner" Java and Kotlin
Solve AtCoder Beginner Contest 150 in java
[Java beginner] About abstraction and interface
Java Functional Programming Exercise Book --zipWith-
[Java beginner] == operator and equals method
Solve AtCoder Beginner Contest 175 in java
Java overload constructor starting from beginner
Solve AtCoder Beginner Contest 160 in java
Java, interface to start from beginner
Solve AtCoder Beginner Contest 152 in java
Solve AtCoder Beginner Contest 156 in java
Java beginner design pattern (Factory Method pattern)
Java learning (0)
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
Java starting from beginner, variables and types
Java array
Studying Java ―― 9
Java scratch scratch
java (constructor)
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Java string
java (array)
Java static
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java reflexes
Java memorandum
☾ Java / Collection
[Beginner] Java method / class / external library [Note 23]
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review
java framework
Java starting from beginner, nested / break / continue
[Java] Inheritance