001-001-004

Variable Declaration: My Recipe Card Maker

Easy

Problem Description

Explanation

1. Background and Purpose

This problem teaches you the most fundamental concept in programming: 'variables'. A variable is like a 'named box' that temporarily stores data while a program runs. Using the familiar context of cooking recipes, you'll learn to declare, assign, and use variables in practice. Mastering this foundation will make it easier to understand more complex programs in the future.

2. Detailed Prerequisites

What is a Variable?

Think of a variable as a 'box for storing data'. You give the box a name, and later you can reference it as 'what's in this box'.

For example, when you write String dishName = "Curry";, you're putting the string "Curry" into a box named dishName.

Data Types

In Java, you must specify the type of data a variable will hold:

  • String: Text (sequences of characters like "Curry Rice")
  • int: Integers (whole numbers without decimals like 1, 2, 100)
  • double: Decimals (numbers with decimal points like 3.14)

Scanner Class

Used to receive keyboard input. Create a Scanner object with Scanner sc = new Scanner(System.in);, then read a line of text with sc.nextLine() or an integer with sc.nextInt().

3. Line-by-Line Code Explanation

Import Statement

import java.util.Scanner;
```java
Required declaration to use the Scanner class. It 'imports' Scanner from Java's standard library.

### Creating Scanner Object
```java
Scanner sc = new Scanner(System.in);
```java
`System.in` represents keyboard input. We pass this to Scanner to prepare for reading input.

### Storing Input in Variables
```java
String dishName = sc.nextLine();
int servings = sc.nextInt();
int time = sc.nextInt();
```java
- Store the dish name (string) in `dishName`
- Store the servings (integer) in `servings`
- Store the cooking time (integer) in `time`

At this point, all three variables hold the values entered by the user.

### Output
```java
System.out.println("| Dish: " + dishName + "...|");
```java
Concatenate strings and variables using `+` to output in a readable card format.

## 4. Common Mistakes and Fixes

### Mistake 1: nextInt() followed by nextLine() issue
```java
// Wrong order
int num = sc.nextInt();
String text = sc.nextLine(); // Gets empty string
```java
`nextInt()` doesn't consume the newline, so the next `nextLine()` reads an empty string.
```java
// Correct order (this problem uses nextLine() first)
String text = sc.nextLine();
int num = sc.nextInt();
```java

### Mistake 2: Type mismatch
```java
// Wrong
int dishName = sc.nextLine(); // Trying to put string in int
```java
```java
// Correct
String dishName = sc.nextLine();
```java

### Mistake 3: Variable name typos
```java
String dishName = sc.nextLine();
System.out.println(dishname); // Different case
```java
Java is case-sensitive. `dishName` and `dishname` are different variables.

## 5. Practical Debugging Tips

### Using println() for debugging
```java
System.out.println("DEBUG: dishName = " + dishName);
System.out.println("DEBUG: servings = " + servings);
```java
Display variable values mid-execution to verify they contain expected values.

### Common Error Messages
- `InputMismatchException`: Non-numeric input for `nextInt()`
- `NoSuchElementException`: Not enough input provided
- `cannot find symbol`: Typo in variable name

## 6. Advanced Topics

### Next Steps
- Conditional statements (if): Display "Quick", "Normal", "Elaborate" based on cooking time
- Loops (for): Input multiple recipes and display as a list

### Real-World Applications
Variables are the foundation of all programs. They're used in web apps, mobile apps, games, and all types of software.

## 7. Related Learning Topics
- 001-002: Variable Calculations (Category 001)
- 002-001: Data Types (Category 002)
- 005-001: Conditional Statements (Category 005).

Test Cases

※ Output examples follow programming industry standards

Input:
Curry Rice
4
45
Expected Output:
+------------------------+
|    MY RECIPE CARD      |
+------------------------+
| Dish: Curry Rice       |
| Servings: 4 people     |
| Time: 45 minutes       |
+------------------------+
Input:
Pasta
2
20
Expected Output:
+------------------------+
|    MY RECIPE CARD      |
+------------------------+
| Dish: Pasta            |
| Servings: 2 people     |
| Time: 20 minutes       |
+------------------------+
Input:
Soup
1
5
Expected Output:
+------------------------+
|    MY RECIPE CARD      |
+------------------------+
| Dish: Soup             |
| Servings: 1 people     |
| Time: 5 minutes        |
+------------------------+
Input:
Steak
abc
30
Expected Output:
Error: Invalid input
Input:
Pizza
-2
15
Expected Output:
Error: Invalid input
❌ 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 6 free executions remaining