008-005-004
参照型の引数:オブジェクトの変更
中級
問題説明
参照型の引数:オブジェクトの変更
この問題では: Counterオブジェクトを参照型引数として受け取り、increment()とincrementBy()メソッドでカウント値を変更し、結果を標準出力に表示するプログラムを作成します。
学習目標: 参照型をメソッドの引数として渡し、オブジェクトの状態を変更する方法を理解する
概要
Counterオブジェクトを引数に受け取り、カウントを増加させるメソッドを作成してください。
仕様
- Counter クラス: int count フィールド
- increment(Counter c) メソッド: c.count を1増加させる
- incrementBy(Counter c, int amount) メソッド: c.count を指定量増加させる
入力
初期値と増加量が入力されます
出力形式
Initial: 5
After increment: 6
After incrementBy: 9
テストケース例
※ 出力例はプログラミングの国際標準に準拠し英語で表示しています
入力:
5 3
期待される出力:
Initial: 5 After increment: 6 After incrementBy: 9
入力:
10 5
期待される出力:
Initial: 10 After increment: 11 After incrementBy: 16
入力:
0 1
期待される出力:
Initial: 0 After increment: 1 After incrementBy: 2
入力:
100 50
期待される出力:
Initial: 100 After increment: 101 After incrementBy: 151
❌ テストに失敗したケースがあります
あなたの解答
現在のモード:● 自分のコード
99
1
2
3
4
5
6
7
8
9
10
›
⌄
⌄
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// ここにコードを書いてください
sc.close();
}
}
0 B / 5 MB
残り 8 回実行可能
