[Studying Java Core] #3

Inwook Baek ·2021년 9월 29일
0

Java Study

목록 보기
3/6
post-thumbnail

Conditional Statement

The conditional statement is a construction that allows a program to perform different computations depending on the value of a Boolean expression. If it is true, the program performs one computation; otherwise, if it is false, the program performs another computation. Here are some examples of Boolean expressions: a > b, i - j == 1, and so on.

The single if-case

if (expression) {
    // body: do something
}

The if-else-cases

if (expression) {    
    // do something
} else {
    // do something else
} 

The if-else-if-cases

if (expression0) {
    // do something
} else if (expression1) {
    // do something else 1
// ...
} else if (expressionN) {
    // do something else N
}

Ternary Operator

The ternary operator is an operator which evaluates a condition and chooses one of two cases to execute. It is also called the conditional operator. The operator can be considered as a form of the if-then-else statement. The ternary operator should not be confused with the conditional statement, despite their similarity. This operator can be used in places where an expression is expected.

Sometimes the ternary operator is more readable and concise than the corresponding if statement.

Example:

int a = ...;
int b = ...;
int max = ...;

if (a > b) {
    max = a;
} else {
    max = b;
}

Ternary operator:

int max = a > b ? a : b;

The for-loop

Sometimes we need to repeat a block of code a certain number of times. To do this, Java provides the for-loop. This loop is often used to iterate over a range of values or through an array. If the number of iterations or the range borders are known, it is recommended to use the for-loop. If they are unknown, the while-loop may be the preferable solution.

for (initialization; condition; modification) {
    // do something
} 

Example 1:
Your task is to find the roots of a cubic equation.
The input contains four numbers: a, b, c, da,b,c,d.
Output all the integer roots between 0 and 1000 (inclusive) for the equation ax^3 + bx^2 + cx + d =0 in ascending order.

If the specified interval does not contain the roots of the equation, do not output anything.

Remember, that cubic equation always has 3 roots, meaning it can't have more than 3 integer roots. Keep this in mind in order to optimize the code.

Sample Input:

0
1
-6
5

Sample Output:

1
5

My Solution:

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
       
        Scanner scanner = new Scanner(System.in);
        
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int c = scanner.nextInt();
        int d = scanner.nextInt();
        
        for (int i = 0; i <= 1000; i++) {
            if (a * i * i * i + b * i * i + c * i + d == 0) {
                System.out.println(i);           
            }
        }
    }
}

Example 2:
Fizz Buzz is a classic programming problem. Here is its slightly modified version.

Write a program that takes two integers as the input: the beginning and the end of the interval (both numbers belong to the interval).

The program should output the numbers from this interval, but if the number is divisible by 3, you should output Fizz instead of it; if the number is divisible by 5, output Buzz; and if it is divisible both by 3 and by 5, output FizzBuzz.

Output each number or the word on a separate line.

Sample Input:

8 16

Sample Output:

8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16

My Solution:

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
       
        Scanner scanner = new Scanner(System.in);
        
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        
        for (int i = a; i <= b; i++) {
            if (i % 3 == 0 && i % 5 ==0) {
                System.out.println("FizzBuzz");
            } else if (i % 3 == 0) {
                System.out.println("Fizz");           
            } else if (i % 5 == 0) {
                System.out.println("Buzz");
            } else {
                System.out.println(i);
            }
        }
    }
}

Example 3:
Write a program that prints the product of all integer numbers from a to b (a < b).
Include a and exclude b from the product.

Sample Input:

100 105

Sample Output:

11035502400

My Solution:

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
       
        Scanner scanner = new Scanner(System.in);
        
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        long product = 1; 
        
        for (int i = a; i < b; i++) {
            product = product * i;
        }
        System.out.print(product);
    }
}

0개의 댓글