Java programming basics practice-for statement

I made a program that I made when I was a newcomer, now in my second year. Today is a for statement.

Conclusion I'm not good at for sentences even after many years

problem

Please enter the first number>
10
Please enter the second number>
20
The sum from 10 to 20 is 165

Implementation

I made it as follows.

Check31.java


package practice;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Check31 {

	public static void main(String[] args) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Please enter the first integer");
		String line;
		try {
			line = reader.readLine();
				int num1 = Integer.parseInt(line);

		System.out.println("Please enter a second integer");
		line = reader.readLine();
		int num2 = Integer.parseInt(line);
		int answer = calc(num1,num2);
		System.out.println(num1 + "From" + num2 + "The sum up to" + answer + "is");
		} catch (NumberFormatException e) {
			System.out.println("Please enter an integer");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private static int calc(int num1, int num2) {
		int sum = 0;
		for (int i = num1; i <= num2; i++) {
			sum = sum + i;
		}
		return sum;

	}

}

Impressions

By the time we got here, the sum would be 11, 21, 175, 22527. .. for sentence is not good. I want to be able to complete a for statement in one shot. ..

Recommended Posts

Java programming basics practice-for statement
Java programming basics practice-switch statement
java programming basics
Java basics
Java basics
Java basics
Java JAR basics
Object-oriented (Java) basics
java Generic Programming
Java concurrency basics
Java for statement
Basics of java basics ② ~ if statement and switch statement ~
Java switch statement
[Java] for statement, while statement
Constraint programming in Java
Java instruction execution statement
[Java] for statement / extended for statement
Java programming (class method)
Java Network Basics (Communication)
(Memo) Java for statement
Basics of try-with-resources statement
Java programming (class structure)
All about Java programming
java competitive programming memo
Muscle Java Basics Day 1
Java Programming Thread Runnable
[Java basics] Let's make a triangle with a for statement
Basics of character operation (java)
[Java] Basic statement for beginners
Java programming (variables and data)
Java Development Basics-Practice ③ Advanced Programming-
☾ Java / Iterative statement and iterative control statement
Summary of Java language basics
Getting Started with Java Basics
Java Development Basics ~ Exercise (Array) ~
[Java11] Stream Usage Summary -Basics-
[Java basics] What is Class?
[Java] Basic terms in programming
Collection of programming selection tasks to make and remember (Java basics)
[Java] Branch enum with switch statement
Java Functional Programming Exercise Book --zipWith-
Introduction to Functional Programming (Java, Javascript)
Java learning memo (while statement, do-while statement)
[Java] Really scary switch statement story
Java study # 4 (conditional branching / if statement)
Summary of object-oriented programming using Java
Java Performance Chapter 5 Garbage Collection Basics