Java programming basics practice-array

problem

Create a program that displays the five numbers you entered in reverse order. Requirements

Please enter 5 numbers
1>10
2>20
3>30
4>40
5>50
num[4] = 50
num[3] = 40
num[2] = 30
num[1] = 20
num[0] = 10

Implementation

Check51.java


package practice;

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

public class Check51 {
	public static void main(String[] args) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Please enter 5 numbers");
		String line;
		try {
			ArrayList<Integer> nums = new ArrayList<Integer>();

			for( int i = 1; i <= 5; i++) {
				System.out.print(i+">");
				line = reader.readLine();
				int num = Integer.parseInt(line);
				nums.add(num);
			}

			for( int i = 5; i > 0; i--) {
				int j = i-1;
				System.out.println("num["+j+"] = "+nums.get(j));
			}
		} catch (NumberFormatException e) {
			System.out.println("Please enter an integer");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

I don't think it's smart to subtract 1 from i in for. .. The array starts at 0, so it wouldn't be clean without this.

Recommended Posts

Java programming basics practice-array
java programming basics
Java basics
Java programming basics practice-for statement
Java basics
Java programming basics practice-switch statement
Java basics
Java JAR basics
Object-oriented (Java) basics
java Generic Programming
Java concurrency basics
Constraint programming in Java
Java programming (class method)
Java Network Basics (Communication)
Java programming (class structure)
All about Java programming
java competitive programming memo
Muscle Java Basics Day 1
Java Programming Thread Runnable
Basics of character operation (java)
Java programming (variables and data)
Java Development Basics-Practice ③ Advanced Programming-
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
Java Functional Programming Exercise Book --zipWith-
Introduction to Functional Programming (Java, Javascript)
Collection of programming selection tasks to make and remember (Java basics)
Java
Java
Summary of object-oriented programming using Java
Java Performance Chapter 5 Garbage Collection Basics
Use OpenCV_Contrib (ArUco) in Java! (Part 2-Programming)
Memorandum of new graduate SES [Java basics]
Newcomer training using the Web-Basic programming using Java-
[GCD] Basics of parallel programming in Swift
[# 1 Java] Basics of Java-Major premise before studying-
Java Programming Style Guide for the Java 11 Era
Java programming (classes and instances, main methods)
[day: 5] I summarized the basics of Java
Looking back on the basics of Java
Introduction of New Generation Java Programming Guide (Java 10)
Java programming (static clauses and "class variables")
Introduction of New Generation Java Programming Guide (Java 11)
Java number guessing game (standard programming problem)
Basics of java basics ② ~ if statement and switch statement ~
Introduction of New Generation Java Programming Guide (Java 12)