006-002-002

Create Multiplication Practice Table

Medium

Problem Description

Explanation

1. Problem Overview

In this problem, you will create a multiplication practice table using nested for loops. Nested loops are a crucial technique when working with tables and grid-structured data. In real-world development, they are used in various scenarios such as data aggregation and multi-dimensional array processing.

Through this learning, you will understand how to perform repetitive processing within repetitive processing and build a foundation for writing more complex programs.

2. Prerequisites

Knowledge required to solve this problem:

  • for loop basics: Syntax of for (initialization; condition; update) { statement }
  • Variable declaration and usage: How to declare int variables and use them in operations
  • Scanner class: How to receive input from the keyboard
  • Conditional branching: Input validation using if statements
  • Arithmetic operators: Usage of multiplication (*) and comparison operators (<, >, <=, >=)

3. How Nested for Loops Work

In nested for loops, each time the outer loop executes once, the inner loop executes completely from start to finish.

Execution Flow (when n=2)

Outer loop i=1:
  Inner loop j=1: 1 x 1 = 1
  Inner loop j=2: 1 x 2 = 2
  ...
  Inner loop j=9: 1 x 9 = 9
  
Outer loop i=2:
  Inner loop j=1: 2 x 1 = 2
  Inner loop j=2: 2 x 2 = 4
  ...
  Inner loop j=9: 2 x 9 = 18
```java

## 4. Code Explanation (Step by Step)

### Step 1: Input Reception and Validation

```java
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();

if (n < 1 || n > 9) {
    System.out.println("Error: Input must be between 1 and 9");
    sc.close();
    return;
}
```java

First, create a Scanner object and read an integer from the keyboard. Next, validate that the input value is within the range of 1 to 9. If it's out of range, display an error message and terminate the process (early return pattern).

This validation is an example of "defensive programming" to prevent the program from behaving unexpectedly.

### Step 2: Outer Loop

```java
for (int i = 1; i <= n; i++) {
    // Process for each table
}
```java

The outer loop controls the table number from 1 to n. Variable i represents the current table number.

- Initialization: `int i = 1` (start from table 1)
- Condition: `i <= n` (repeat until table n)
- Update: `i++` (increment by 1 table)

### Step 3: Inner Loop

```java
for (int j = 1; j <= 9; j++) {
    int result = i * j;
    System.out.println(i + " x " + j + " = " + result);
}
```java

The inner loop multiplies each table by numbers from 1 to 9. Variable j represents "what to multiply by".

In each iteration, the calculation result of `i * j` is stored in variable result, which is then output in the specified format.

### Step 4: Resource Cleanup

```java
sc.close();
```java

Close the Scanner object when finished using it. This is an important process to prevent memory leaks.

## 5. Common Mistakes

### Mistake 1: Loop Condition Error

```java
// ❌ Wrong
for (int i = 1; i < n; i++) {  // Table n is not included
```java

**Reason**: With `i < n`, the condition becomes false when i equals n, so table n is not displayed.

```java
// ✅ Correct
for (int i = 1; i <= n; i++) {  // Include table n
```java

### Mistake 2: Variable Scope

```java
// ❌ Wrong
int result;
for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 9; j++) {
        result = i * j;  // Overwritten each time, but that's okay
    }
}
System.out.println(result);  // Only the last result is output
```java

**Reason**: Since the output is outside the loop, only the last calculation result is displayed.

```java
// ✅ Correct
for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 9; j++) {
        int result = i * j;
        System.out.println(i + " x " + j + " = " + result);
    }
}
```java

### Mistake 3: Missing Input Validation

```java
// ❌ Wrong
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {  // Strange behavior with n=0 or negative values
```java

**Reason**: Without input validation, the loop may not execute or could result in infinite loops with unexpected values.

```java
// ✅ Correct
if (n < 1 || n > 9) {
    System.out.println("Error: Input must be between 1 and 9");
    return;
}
```java

## 6. Advanced Topics

Hints for deeper learning:

- **Triple nested loops**: Used when handling 3-dimensional data (e.g., year, month, day)
- **break and continue statements**: Control flow within loops (exit loop or skip under specific conditions)
- **Enhanced for loop**: Concise way to process arrays and collections
- **Real-world applications**: Processing matrix data like spreadsheet software, image processing (pixel-level operations), etc.

## 7. Related Learning Topics

Topics to learn next or concepts related to this problem:

- **Arrays and nested loops**: Initialization and traversal of 2D arrays (Category 007)
- **Nested while loops**: Combining different iteration constructs (Category 006-003)
- **Using methods**: Organizing code by splitting nested loops into methods (Category 008).

Test Cases

※ Output examples follow programming industry standards

Input:
2
Expected Output:
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
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 = 42
6 x 8 = 48
6 x 9 = 54
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
9 x 1 = 9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81
Input:
0
Expected Output:
Error: Input must be between 1 and 9
Input:
10
Expected Output:
Error: Input must be between 1 and 9
❌ Some tests failed
❌ エラー発生

Your Solution

Current Mode: My Code
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Write your code here

sc.close();
}
}
0 B / 5 MB

You have 9 free executions remaining