Contents

Baekjun 1000, A+B question (with.Java)

   Sep 17, 2024     1 min read

This is an article about Baekjun no. 1000 A+B problem (with.Java).

I want to solve the coding test problem, find out how to solve it differently from the retrospective of the problem I solved, and get to know.

Letโ€™s get to the problem first.

Problem

After receiving the two integers A and B, write a program that outputs A+B.

Input

A and B are given in the first line. (0 < A, B < 10)

Output

Output A+B on the first line.

problem solving

It receives two integers from the user and outputs the sum.

To do this, use a scanner class to process the input and output the results.

my solution to the problem

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner = new scanner (System.in ); // Create a scanner object to receive input
        inta = scan.nextInt(); // take the first integer and store it in variable a
        int b = scan.nextInt(); // take the second integer and store it in variable b
        System.out.print(a + b); // Output the sum of a and b
        scan.close(); // close the scanner object to release system resources
    }
}

Solution Description

We solved the problem and learned more about closing the scanner object.

If you do not close the scanner object, resources may not be released until the program is terminated.

This can lead to memory leaks, especially in long-running programs, which can waste system resources.

Conclusion

Through this simple program, I learned how to take and process user input from Java.

Understanding how to use a scanner class to receive input, process it, and output results is fundamental to Java programming.

Also, donโ€™t forget to use scan.close() to properly release used system resources.

Thank you!