Java Exercise "Intermediate"

Java Exercise "Intermediate"

Create a program that inputs an integer value and displays even if the value is even, and odd if the value is odd.

    private static void question40(int num1) {
        System.out.println("Q40");
        if (num1 % 2 == 0) {
            System.out.println("even");
        } else {
            System.out.println("odd");
        }
    }

Create a program that lets you enter an integer value and determines if the value is a single digit natural number or not.

    private static void question41(int num1) {
        System.out.println("Q41");
        if (num1 > 0 && num1 <= 9) {
            System.out.println(num1);
        }
    }

Enter three integer values, determine if those values are arranged in ascending order (may be equal), and create a program that displays OK if they are arranged in ascending order, and NG if not. ..

    private static void question42(int num1, int num2, int num3) {
        System.out.println("Q42");
//        if ((num1 < num2 && num2 < num3) || (num1 == num2 || num2 == num3)) {
        if (num1 <= num2 && num2 <= num3){
            System.out.println("ok");
        } else {
            System.out.println("NG");
        }
    }

Enter the coefficients a, b, c of the quadratic equation ax ^ 2 + bx + c = 0 (x ^ 2 means the square of x), and whether the solution of the quadratic equation is two real or multiple solutions, 2 Create a program that determines if there are two imaginary solutions.

     * <p>
     *Imaginary numbers are squared to negative numbers.
     *Numbers other than imaginary numbers are real numbers.
     *The multiple solution has one solution.
     */
    private static void question43(int a, int b, int c) {
        System.out.println("Q43");
        int d = b * b - 4 * a * c;
        if (d > 0) {
            System.out.print("Two real solutions");
        } else if (d == 0) {
            System.out.print("Multiple solution");
        } else {
            System.out.println("Imaginary solution");
        }
    }

Enter the amount you want to convert (in yen) and how many yen per dollar as an integer value, and create a program that calculates and displays how many dollars and cents the entered amount is. One cent is $ 1/100. The result can be an integer value (rounded down to the nearest cent).

    private static void question44(int money, int doller) {
        System.out.println("Q44");
        //Yen converted to dollars: Yen you want to convert ÷ Yen price per dollar
        int yen = money / doller;
        //1 cent is 1/$ 100
        //TODO Round off to see if it can be truncated properly
        System.out.println("How much is one dollar?");
        System.out.println(money + "Yen is" + yen + "In dollars" + "is");
    }

The initial fare is 610 yen up to 1700m, and after that there are taxis for 80 yen every 313m. Enter the distance you took this taxi in meters and create a program to calculate the fare.

    private static void question45(int m) {
        System.out.println("Q45");
        //610 yen up to 1700m
        int taxi = 610;
        int price = 0;
        //If it is 1700m or less, it will be 610 yen
        if (m < 1700) {
            price = taxi;
        } else {
            //Exceeded distance over 1700m
            double increase = m - 1700;
            //How many times every 313m has come
            double number = increase / 313;
            //Spending 80 yen on the number of times
            //Advance the number of times with the first decimal point
            price = Double.valueOf(taxi + (Math.ceil(number) * 80)).intValue();
        }
        System.out.println("Taxi fare" + price + "It's a yen");
    }

The admission fee for the Kamiyama Museum is 600 yen per person, but for groups of 5 or more it is 550 yen per person, and for groups of 20 or more it is 500 yen per person. Enter the number of people and create a program to calculate the total admission fee.

    private static void question46(int human) {
        System.out.println("Q46");
        int nyuzyou1 = 600;
        int nyuzyou2 = 550;
        int nyuzyou3 = 500;
        int price = 0;
        //600 yen for one person
        //500 yen per person for 20 or more
        if (human < 5) {
            price = human*nyuzyou1;
        //550 yen per person for 5 or more and less than 20
        }else if (human < 20) {
            price = human*nyuzyou2;
        //500 yen per person for 20 or more
        }else {
            price = human*nyuzyou3;
        }
        System.out.println("Admission is" +  price + "It's a yen");
    }

Enter two different integer values and store them in different variables. And after swapping the values of those variables

Create a program that displays the value of each variable. Simply changing the order and displaying is not enough. Be sure to swap the values of variables. In the case of the execution example below, first, from the state where 2 is input to the variable a and 5 is input to b, the value of a is changed to 5 and the value of b is changed to 2.

    private static void question47(int a, int b) {
        System.out.println("Q47");
        //Display the value of the variable before exchange
        System.out.println( "Before replacement: a=" + a + "  b=" + b );
        //Assign to a temporary variable
        int x = a;
        a = b;
        b = x;
        //After replacement
        System.out.println("After replacement: a=" + a + "  b=" + b);
    }

First, enter an integer value of 2 or more, calculate according to the following rules, display the number of calculations and the calculation result, and create a program that repeats until the calculation result becomes 1. Rule: If a value is even, halve it. If it is odd, multiply that value by 3 and add 1.

     *while statement

    private static void question48(int number) {
        System.out.println("Q48");
        System.out.println("number:" + number);
        for (int i = 1, answer = number; answer != 1; i++) {
            answer = answer % 2 == 0 ? answer / 2 : answer * 3 + 1;
            System.out.println(i + ": " + answer);
        }
    }

Create a program that displays the multiplication table using double iterations. You can't just display it without using double repetition.

     *Tab between values(\t)Use to make a gap.
     *TODO Don't put out the last tab

    private static void question49() {
        System.out.println("Q49");
        for (int i = 1; i < 10; i++) {
            for (int j = 1; j < 10; j++) {
               System.out.print(i * j + "\t");
            }
            System.out.println("");
        }
    }

Create a program that repeatedly displays values from 1 to 100, but when it is a multiple of 3, it displays foo, and when it is a multiple of 5, it displays bar instead of numbers. In addition, when it is a multiple of both 3 and 5, it is displayed as foobar.

     private static void question50() {
         System.out.println("Q50");
         for (int i = 1; i <= 100; i++) {
             if(i % 3 == 0 && i % 5 == 0) {
                 //When it is a multiple of 3 and a multiple of 5
                     System.out.println("foobar");
                     //When it is a multiple of 3
                 } else if(i % 3 == 0){
                     System.out.println("foo");
                 } else if(i % 5 == 0) {
                 System.out.println("bar");
             }else {
                 //If none of the above apply
                 System.out.println(i);
             }
         }
     }

I want to pay the specified amount with only 100-yen coins and 10-yen coins, as few as possible. Create a program that calculates and displays each number when you enter the amount.

    private static void question51(int money) {
        System.out.println("Q51");
        int hyaku = money/100;
        int zyuu = (money-(hyaku*100))/10;
        //100 yen ball
        System.out.println("100 yen ball" + hyaku + "It is a piece");
        //10-yen coin
        System.out.println("10-yen coin" + zyuu + "It is a piece");
    }

After entering the year, create a program to determine if the year is a leap year. Of the years divisible by 4, the year that is not divisible by 100 or divisible by 400 is a leap year.

    private static void question52(int year) {
        System.out.println("Q52");
        boolean check = false;
        //The remainder of dividing the year by 4 is 0
        if (year % 4 == 0 && year % 100 == 0 && year % 400 == 0) {
                    check = true;
            } else {
                check = true;
        }
        // String s = b ? "true" : "false";
        System.out.println(check ? "It's a leap year." : "It's not a leap year.");
    }

Create a program that factorizes the input values of natural numbers into prime factors and displays the results.


    private static void question53(int number) {
        System.out.println("Q53");
        System.out.print(number + "=");
        //The initial value of the For statement is 2 because the prime number is 2 or more.
        for (int i = 2; i <= number; i++) {
            if (number % i == 0) {
                //If divisible, i is a prime number.
                //If it is not divisible, it is not a prime number.
                number /= i;
                if (number == 1) {
                    System.out.print(i);
                }else {
                    System.out.print(i + "×");
                }
            }
        }
    }

Recommended Posts

Java Exercise "Intermediate"
Java Exercise "Beginner"
Java
[Java] Stream API --Stream intermediate processing
Java Development Basics ~ Exercise (Array) ~
Java
[Java] Stream API intermediate operation
Java Functional Programming Exercise Book --zipWith-
Java learning (0)
[Java] array
Java protected
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
Java array
[Java] ArrayDeque
java (method)
Java Day 2018
Java string
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java reflexes
java (interface)
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
java beginner 3
Java memo
java (encapsulation)
Java inheritance
[Java] Overload
Java basics
Decompile Java
java notes
java beginner
[java] interface
Java9 collection
Java basics
Java methods
Java diary