Java exercises [Basic]

I started solving exercises to study Java. I'm sorry if I made a mistake.

Basics

package com.company; import java.util.Scanner;

public class HundredKnocksBasic { public static void main(String arg[]) { // question0(); // question1(); // question2(); // question3(); // question4(); // question5(); // question6(); // question7(); // question8(); // question9(); // question10(); // question11(); // question12(); // question13(); // question14(); // question15(); // question16(); // question17(); // question18(); // question19(); }

Create a method that displays Hello World! When executed.


    private static void question0() {
        System.out.println("Question 0");
        System.out.println("Hello World!");
    }

Create a program that calculates 12345 + 23456 and displays the results.


    private static void question1() {
        System.out.println("Question 1");
        int a = 12345;
        int b = 23456;
        System.out.println(a + b);
    }

Create a program that displays the remainder of 12345 divided by 7.


    private static void question2() {
        System.out.println("Question 2");
        int a = 12345;
        int b = 7;
        System.out.println(a / b);
    }

Create a program that lets you enter an integer value and displays the input value.


    private static void question3() {
        System.out.println("Question 3");
        Scanner scanner = new Scanner(System.in);   //Initialize the Scanner class Instantiate the Scanner class
        //InputStream object, standard input(Input from the keyboard)
        System.out.println("Please enter a number");
        int num = scanner.nextInt();  //The part that receives input
        scanner.close();
        System.out.println(num);
    }

Create a program that lets you enter an integer value and displays the calculation result by multiplying the input value by three.


    private static void question4() {
        System.out.println("Question 4");
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter a number");
        int num = scanner.nextInt(); //The part that receives input
        scanner.close();
        System.out.println(num * 3);
    }

Create a program that lets you enter two integer values and finds the sum, difference, product, quotient, and remainder of those values. The difference and quotient are the result of subtracting or dividing the second value from the first value. Even if there is no remainder, it is good because it is displayed as 0.


    private static void question5() {
        System.out.println("Question 5");
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter the first number");
        int num1 = scanner.nextInt();  //The part that receives the first input
        System.out.println("Please enter the second number");
        int num2 = scanner.nextInt();  //The part that receives the second input
        scanner.close();
        System.out.println(num1 + num2);
        System.out.println(num1 - num2);
        System.out.println(num1 * num2);
        System.out.println(num1 / num2);
    }

Create a program that lets you enter an integer value and displays zero if the value is 0.


    private static void question6() {
        System.out.println("Question 6");
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter a number(0)");
        int num = scanner.nextInt(); //The part that receives input
        scanner.close();
        if (num == 0) { //If the value entered is 0
            System.out.println("zero");
        }
    }

Create a program that lets you enter an integer value and displays zero if the value is 0 and not zero if the value is not 0.


    private static void question7() {
        System.out.println("Q7");
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter a number(Other than 0)");
        int num = scanner.nextInt();
        scanner.close();
        if (num == 0) { //If the value entered is 0
            System.out.println("zero");
        } else {
            System.out.println("not zero");
        }
    }

Create a program that lets you enter an integer value and displays it as positive if the value is positive. However, 0 is not included positively.


    private static void question8() {
        System.out.println("Question 8");
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter a number(Correct)");
        int num = scanner.nextInt();
        scanner.close();
        if (num > 0) { //If the value is positive
            System.out.println("positive");
        }
    }

Create a program that lets you enter an integer value and displays positive if the value is positive, negative if the value is negative, and zero if the value is 0.


    private static void question9() {//Write with else if
        System.out.println("Q9");
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter a number");
        int num = scanner.nextInt();
        scanner.close();
        if (num > 0) { //If the value is positive
            System.out.println("positive");
        } else if (num < 0) {   //If the value is negative
            System.out.println("negative");
        } else {  //If the value entered is 0
            System.out.println("zero");
        }
    }

Create a program that lets you enter an integer value and displays that value as an absolute value. (If possible, change the value of the variable to an absolute value


    private static void question10() {
        System.out.println("Q10");
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter a number(Absolute value)");
        int num = scanner.nextInt();
        scanner.close();
       ###To get the absolute value, use the abs method of the Math class. Since the abs method is a static method, it can be called as it is.
        num = Math.abs(num);
        System.out.println(num);
    }

Create a program that repeats Hello World! 10 times.


    private static void question11() {
        System.out.println("Q11");
        //for (Initialization formula;Conditional expression;Renewal formula) {
        for (int i = 0; i < 10; i++) {
            System.out.println("Hello World!");
        }
    }

Create a program that lets you enter an integer value and repeatedly displays Hello World! As many times as that value.


    private static void question12() {
        System.out.println("Q12");
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter the number of repetitions");
        int num = scanner.nextInt();
        scanner.close();
        //Repeat as many times as entered
        for (int i = 0; i < num; i++) {
            System.out.println("Hello World!");
        }
    }

Create a program that inputs an integer value, increments the number by 1 from 0 to the input value, and displays it.


    private static void question13() {
        System.out.println("Q13");
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter a number");
        int num = scanner.nextInt();
        scanner.close();
        //Execute up to the entered number
        for (int i = 0; i < num; i++) {
            System.out.println(i);
        }
    }

Create a program that lets you enter an integer value and reduces the number by 1 from the input value to 0.


    private static void question14() {
        System.out.println("Q14");
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter a number");
        int num = scanner.nextInt();
        scanner.close();
        //Decrease by 1 from the entered number
        for (int i = 0; i < num; num--) {
            System.out.println(num);
        }
    }

Create a program that inputs an integer value and increments it by 2 from 0 to a value that does not exceed the input value.


    private static void question15() {
        System.out.println("Q15");
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter a number");
        int num = scanner.nextInt();
        scanner.close();
        //Increase by 2 up to the entered number
        for (int i = 0; i < num; i += 2) {
            System.out.println(i);
        }
    }

Let's input an integer value, if the input value is not 0, input it again, and if it is 0, create a program that ends.


    private static void question16() {//while statement
        System.out.println("Q16");
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter a number");
        int num = scanner.nextInt();
        scanner.close();
        while (num != 0) {
            System.out.println("Please enter the number again");
            num = scanner.nextInt();
            //Stop if the input value is 0
        }
        scanner.close();
        System.out.println("Finish");
    }

Declare an integer type array with 10 elements, set the initial value of the i-th element to i, and create a program that displays the values in order.


    private static void question17() {
        System.out.println("Q17");
        /*
Declaration of array and specification of number of elements
Type name Array variable name[] =new type name[Element count];
         */
        int bar[] = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; //Declare an array with 10 elements
        /*
Type name Variable name=initial value;
The initial value of the i-th element is i
         */
        for (int i = 0; i < bar.length; i++) {
            //Use length to get the number of elements in an array bar[i]Index of i
            System.out.println(bar[i]);
        }
    }

Declare an integer type array with 10 elements, let them input integer values, take all the elements of the array as input values, and create a program that displays the values of all the elements.


    private static void question18() {
        System.out.println("Q18");
        Scanner scanner = new Scanner(System.in);
        //Declare an array with 10 elements as a local variable
        int[] a = new int[10];
        System.out.println("Please enter a number");
        //Have the local variable b enter a value
        int b = scanner.nextInt();
        //Describe the elements of the array to be assigned to the left side, and describe the value to be assigned to the right side.
        //Array variable name[Sequence number] :=Value to substitute
        //Substitute the value obtained in b for a
        //Array length
        //Repeat for the length of the array
        for (int i = 0; i < a.length; i++) {
            a[i] = b;
            //Display array a
            System.out.println(a[i]);
        }
    }

Create a program that declares an integer type array with 5 elements, substitutes the integer values entered in order for all arrays, and displays the values of all elements.


    private static void question19() {
        System.out.println("Q19");
        Scanner sc = new Scanner(System.in);
        //Declare an array with 10 elements
        int bar[] = new int[10];
        bar[0] = sc.nextInt();
        bar[1] = sc.nextInt();
        bar[2] = sc.nextInt();
        bar[3] = sc.nextInt();
        bar[4] = sc.nextInt();
        bar[5] = sc.nextInt();
        bar[6] = sc.nextInt();
        bar[7] = sc.nextInt();
        bar[8] = sc.nextInt();
        bar[9] = sc.nextInt();
        //Get the number of elements
        for (int i = 0; i < bar.length; i++) {
            System.out.println("Array" + i + "Second value" + bar[i]);
        }
    }
}


Recommended Posts

Java exercises [Basic]
Java basic grammar
Java Beginner Exercises
[Java] Basic structure
[Java] [Basic] Glossary
Java basic grammar
Java basic grammar
java basic knowledge memo
[Java] Data type ①-Basic type
Java basic date manipulation
Java basic naming conventions
Java learning memo (basic)
[Java] Basic method notes
Java basic data types
Basic Java OOps concepts
Java basic learning content 7 (exception)
Basic Authentication with Java 11 HttpClient
Java basic syntax + α trap
[Java] Basic statement for beginners
Java basic learning content 5 (modifier)
[Java] Thymeleaf Basic (Spring Boot)
Implement Basic authentication in Java
Java
Java Basic Learning Content 8 (Java API)
[Beginner] Java basic "array" description
Java basic learning content 4 (repetition)
[Java] Basic terms in programming
[Java] Basic types and instruction notes
Java basic learning content 3 (operator / ternary operator)
Basic data types and reference types (Java)
Java basic learning content 9 (lambda expression)
Java basic learning content 2 (array / ArrayList)
Reading and writing Java basic files
Basic processing flow of java Stream
Java basic data types and reference types
[Basic knowledge of Java] Scope of variables
[Java] Exception types and basic processing
Basic structure of Java source code
Java learning (0)
Studying Java ―― 3
[Java] array
Profiling with Java Visual VM ~ Basic usage ~
Java protected
[Java] Annotation
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
java beginner 4
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5