2 Commits

Author SHA1 Message Date
0cc90c94a3 10/19 2026-10-19 13:50:48 +09:00
2c6d1574e5 10/8コミット 2026-10-08 16:49:57 +09:00
163 changed files with 878 additions and 2231 deletions

View File

@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="output" path="build/classes"/>
</classpath>

View File

@@ -1 +0,0 @@
/build/

View File

@@ -5,19 +5,7 @@
<projects> <projects>
</projects> </projects>
<buildSpec> <buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec> </buildSpec>
<natures> <natures>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures> </natures>
</projectDescription> </projectDescription>

View File

@@ -1,9 +0,0 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
org.eclipse.jdt.core.compiler.compliance=21
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=21

View File

@@ -1,4 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<installed facet="java" version="21"/>
</faceted-project>

View File

@@ -1,11 +0,0 @@
public class Question1_1 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
System.out.println("`Hello,world`");
}
}

View File

@@ -1,16 +0,0 @@
public class Question1_2 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
//1行だけを無効化する方法
/*
複数行をまとめて無効化する方法
*/
System.out.println("テスト");
}
}

View File

@@ -1,16 +0,0 @@
public class Question1_3 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
//変数strを宣言して代入する
String str;
str = "こんにちは";
//出力
System.out.println(str);
}
}

View File

@@ -1,15 +0,0 @@
public class Question1_4 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
//変数numberを値10で初期化する
int number = 10;
//出力
System.out.println(number);
}
}

View File

@@ -1,15 +0,0 @@
public class Question1_5 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
//変数chを値Aで初期化する
char ch = 'A';
//出力
System.out.println(ch);
}
}

View File

@@ -1,15 +0,0 @@
public class Question1_6 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
//変数boを値trueで初期化する
boolean bo = true;
//出力
System.out.println(bo);
}
}

View File

@@ -1,15 +0,0 @@
public class Question1_7 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
//変数loを値1000000000000で初期化する
long lo = 1000000000000L;
//出力
System.out.println(lo);
}
}

View File

@@ -1,15 +0,0 @@
public class Question1_8 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
//変数flを値3.14で初期化する
float fl = 3.14F;
//出力
System.out.println(fl);
}
}

View File

@@ -1,42 +0,0 @@
public class Question1_9 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
//変数宣言
boolean bl; // true or false
char ch; // 16ビットUnicode文字 u0000-uFFFF
byte by; // 8ビット整数 -128~127 (符号ビット+2^7)
short sh; // 16ビット整数 -32,768~32,767 (符号ビット+2^15)
int num; // 32ビット整数 -2,147,483,648~2,147,483,647 (符号ビット+2^31)
long lo; // 64ビット整数 -9,223,372,036,854,775,808~9,223,372,036,854,775,807 (符号ビット+2^63)
float fl; // 32ビット単精度浮動小数点数
double db; // 64ビット倍精度浮動小数点数
String str; // 文字列を扱うクラス
//代入
bl = true;
ch = 'A';
by = 100;
sh = 10000;
num = 1000000000;
lo = 1000000000000000000L;
fl = 3.1415926F;
db = 1e2;
str = "DONE";
//出力
System.out.println(bl);
System.out.println(ch);
System.out.println(by);
System.out.println(sh);
System.out.println(num);
System.out.println(lo);
System.out.println(fl);
System.out.println(db);
System.out.println(str);
}
}

View File

@@ -1,18 +1,13 @@
public class Question2_4 { public class Question02_4 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
int num1 = 10;
// error double num2 = 3.14;
// int sum; double sum = num1 + num2;
// sum = 10 + 3.14;
double sum;
sum = 10 + 3.14;
System.out.println(sum); System.out.println(sum);
} }
} }

View File

@@ -3,21 +3,17 @@ public class Question2_1 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
int sum = 6 + 2;
int sum, diff, mulch, div, rem; int diff = 6 - 2;
int mulch = 6 * 2;
sum = 6 + 2; int div = 6 / 2;
diff = 6 - 2; int rem = 17 % 5;
mulch = 6 * 2;
div = 6 / 2;
rem = 17 % 5;
System.out.println("変数sum:" + sum); System.out.println("変数sum:" + sum);
System.out.println("変数diff:" + diff); System.out.println("変数diff:" + diff);
System.out.println("変数mulch:" + mulch); System.out.println("変数mulch:" + mulch);
System.out.println("変数div:" + div); System.out.println("変数div:" + div);
System.out.println("変数rem:" + rem); System.out.println("変数rem:" + rem);
} }
} }

View File

@@ -3,12 +3,10 @@ public class Question2_2 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
String lastname = "山田";
String firstname = "太郎";
String lastName = "太郎"; System.out.println(lastname + firstname);
String firstName = "山田";
System.out.println(firstName+lastName);
} }
} }

View File

@@ -3,13 +3,10 @@ public class Question2_3 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
int num = 10; int num = 10;
num = num + 5; num = num + 5;
System.out.println(num); System.out.println(num);
} }
} }

View File

@@ -3,13 +3,12 @@ public class Question2_5 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
int age = 20; int age = 20;
int diff = 5; int diff = 5;
System.out.println("山田さんは"+age+"歳です。"); System.out.println("山田さんは" + age + "歳です。");
System.out.println(diff+"年後には"+(age+diff)+"歳になります。"); System.out.println("5年後には" + (age + diff) + "歳になります。");
} }
} }

View File

@@ -3,12 +3,10 @@ public class Question2_6 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
double num = 9.0 / 2.0;
int numerator = 9;
int denominator = 2; System.out.println(num);
System.out.println( (float)numerator/(float)denominator );
} }
} }

View File

@@ -3,16 +3,9 @@ public class Question2_7 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
int num = (int) 1.0;
int num;
// error
// num = 1.0;
num = (int)1.0;
System.out.println(num);
System.out.println(num);
} }
} }

View File

@@ -3,15 +3,13 @@ public class Question2_8 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
int money = 1000; int money = 1000;
int price = 580; int price = 580;
float taxRate = 0.08F; double taxRate = 0.08;
money = money - (int)( price*(1.0+taxRate) );
System.out.println("残金は"+money+"円です。");
} int num = (int)(price * (1 + taxRate));
money = money - num;
System.out.println("残金は" + money + "円です。");
}
} }

1
Question03-if/bin/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/bin/

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<projectDescription> <projectDescription>
<name>Question14-Interface</name> <name>Question03-if</name>
<comment></comment> <comment></comment>
<projects> <projects>
</projects> </projects>

View File

@@ -0,0 +1,51 @@
# Question03 - 条件分岐
本資料の練習問題は、複数のプログラミング言語(Java / C# / PHP / Python)で共通して利用できるよう記述されています。静的型付け・動的型付けの違いや文字列の連結ルールなど、言語固有の文法仕様に基づき、問題文の表現をご使用の言語の仕様に適宜読み替えて進めてください。
## この問題のねらい
- 条件式(比較や論理演算)を使って、値の真偽を判定できること
- 条件が真の場合と偽の場合とで、異なる処理を行えること
- 複数の条件を「かつ」「または」で組み合わせた判定ができること
- 複数の分岐を段階的に組み合わせて、値の範囲に応じた処理を書き分けられること
以下の設問はそれぞれ独立したプログラムとして実装してください。
---
## 設問1
整数を保持できる変数 `num` を宣言し、`100` で初期化すること。そのうえで、「`num` の値が `100` と等しい」という条件が真の場合に `条件に合致しました` と出力する条件分岐を実装すること。
- 出力: `条件に合致しました`
---
## 設問2
整数を保持できる変数 `num` を宣言し、`65` で初期化すること。そのうえで、`num` の値が `65` 以上の場合は `合格です`、そうでない場合は `不合格です` と出力する条件分岐を実装すること。
- 出力: `合格です`
---
## 設問3
整数を保持できる変数 `num` が `10` で初期化されている。「`num` が2で割り切れる」かつ「`num` が5で割り切れる」という条件がともに真の場合に、`num` の値を含めた次の形式のメッセージを出力する条件分岐を実装すること。
- 出力: `10は2でも5でも割り切れます。`
---
## 設問4
画面に `降水確率を入力してください。(数字のみ)` と出力したあと、数値の入力を1つ受け取り、整数として変数 `num` に格納すること(標準的な入力手段を使ってよい)。
そのうえで、`num` の値に応じて次の内容を出力する条件分岐を実装すること。
- `num` が `0` より小さい、または `100` より大きい場合: `不正な値が入力されました`
- 上記に当てはまらず、`num` が `75` 以上の場合: `雨が降る確率が高いです`
- 上記に当てはまらず、`num` が `30` 以上 `75` 未満の場合: `雨が降るかもしれないです`
- 上記のいずれにも当てはまらない場合(`num` が `30` 未満): `雨はまず降らないでしょう`
- 補足: 範囲外の値(`0`未満または`100`より大きい)の判定は、他の条件より優先して行われること(範囲外の値を入力した場合に、誤って他のメッセージが出力されないようにすること)。

View File

@@ -1,15 +1,13 @@
public class Question3_1 { public class Qestion3_1 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
int num;
int num = 100; num = 100;
if(num == 100) {
if ( num == 100 ) {
System.out.println("条件に合致しました"); System.out.println("条件に合致しました");
} }
} }
} }

View File

@@ -3,12 +3,11 @@ public class Question3_2 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
int num = 65; int num = 65;
if(num >= 65) {
if ( num >= 65 ) {
System.out.println("合格です"); System.out.println("合格です");
} else { }
else {
System.out.println("不合格です"); System.out.println("不合格です");
} }

View File

@@ -3,13 +3,10 @@ public class Question3_3 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
int num = 10; int num = 10;
if (num % 2 == 0 && num % 5 == 0) {
if ( (num % 2 == 0 ) && ( num % 5 == 0 ) ) { System.out.println(num + "は2でも5でも割り切れます。");
System.out.println(num+"は"+2+"でも"+5+"でも割り切れます");
} }
} }
} }

View File

@@ -1,26 +1,48 @@
//標準入力のために必要
import java.util.Scanner; import java.util.Scanner;
public class Question3_4 { public class Question3_4 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
String[] main;
System.out.println("降水確率を入力してください。(数字のみ)"); main = new String[6];
Scanner sc = new Scanner(System.in); main[0] = "降水確率を入力してください。(数字のみ)";
int num = Integer.parseInt(sc.nextLine()); main[1] = "不正な値が入力されました";
sc.close(); main[2] = "雨が降る確率が高いです";
main[3] = "雨が降るかもしれないです";
main[4] = "雨はまず降らないでしょう";
main[5] = "数字を入力してください";
if ( (num<0) || (num>100) ) { //最初のコメント出力
System.out.println("不正な値が入力されました"); System.out.println(main[0]);
} else if ( num >= 75 ) {
System.out.println("雨が降る確率が高いです"); //標準入力取得と空白除去
} else if ( (num<75) && (num>=30) ) { Scanner scan = new Scanner(System.in);
System.out.println("雨が降るかもしれないです"); String str = scan.nextLine().strip();
} else {
System.out.println("雨はまず降らないでしょう"); // 数字チェック
if (!str.matches("-?\\d+")) {
System.out.println(main[5]);
return;
} }
int num = Integer.parseInt(str);
if (num < 0 || num > 100) {
System.out.println(main[1]);
} else if (num >= 75) {
System.out.println(main[2]);
} else if (num >= 30) {
System.out.println(main[3]);
} else {
System.out.println(main[4]);
}
} }
} }

1
Question04-while/bin/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/bin/

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<projectDescription> <projectDescription>
<name>Question16-Exception</name> <name>Question04-while</name>
<comment></comment> <comment></comment>
<projects> <projects>
</projects> </projects>

View File

@@ -0,0 +1,88 @@
# Question04 - 繰り返し
本資料の練習問題は、複数のプログラミング言語(Java / C# / PHP / Python)で共通して利用できるよう記述されています。静的型付け・動的型付けの違いや文字列の連結ルールなど、言語固有の文法仕様に基づき、問題文の表現をご使用の言語の仕様に適宜読み替えて進めてください。
## この問題のねらい
- ある条件を満たす間、処理を繰り返すことができること
- 繰り返しを続けるかどうかの判定条件を、目的に応じて設定できること
- 繰り返しの実行中に、その場で繰り返し自体を打ち切る(早期終了する)ことができること
- 繰り返しの中で変数の値を更新しながら、状態を進めていく処理を書けること
- 入力の受け取り・繰り返し・条件分岐を組み合わせて、簡単なゲームのような一連の処理を組み立てられること
以下の設問はそれぞれ独立したプログラムとして実装してください。
---
## 設問1
0~9のいずれかのランダムな整数を生成し、画面に出力する処理を、生成するたびに繰り返す実装を行うこと。
生成された値が `5` になった時点で繰り返し自体を終了するように、**繰り返しを続けるかどうかの判定条件**を使って終了させること(判定条件を設けなければ、際限なく繰り返され続けてしまう)。
- 出力: 0~9のいずれかの整数を1行に1つずつ出力し続け、`5` を出力した時点で繰り返しを終了する(ランダムな値を使うため、出力される行数や値そのものは実行のたびに変わる)。
---
## 設問2
整数を保持できる変数 `num` を宣言し、`2` で初期化すること。`num` の値が `1000` を超えるまで、繰り返しのたびに `num` の値を2倍にした値を `num` に代入し直すこと。繰り返しが終了したら、`num` の値を出力すること。
- 出力: `1024`
---
## 設問3
0~9のいずれかのランダムな整数を生成し、画面に出力する処理を繰り返す実装を行うこと。
繰り返しを続けるかどうかの判定条件は使わずに(=条件だけを見れば際限なく繰り返され続ける実装のまま)、生成された値が `5` であった場合に、繰り返しの実行中にその場で繰り返し処理を打ち切る(早期終了する)ように実装すること。
- 出力: 0~9のいずれかの整数を1行に1つずつ出力し続け、`5` を出力した時点で繰り返しを終了する(ランダムな値を使うため、出力される行数や値そのものは実行のたびに変わる)。
- 補足: 設問1とは異なり、繰り返し自体の継続条件は変更しないこと。
---
## 設問4
双六(すごろく)を模したプログラムを作成する。
- 双六の総マス数を表す整数変数 `map` を宣言し、`50` で初期化すること。
- 現在までに進んだマス数を表す整数変数 `progress` を宣言し、`0` で初期化すること。
`===== START =====` と出力したあと、`progress` が `map` に到達する(ゴールする)まで、次の処理を繰り返すこと。
1. `Enterキーを押してください。さいころをふります。` と出力し、何らかの入力(内容は問わない)を1行分待ち受ける。
2. 1~6のいずれかのランダムな整数を生成し、さいころの目とする。
3. 出た目の値を使って、`3の目が出ました。` のように、値を含めたメッセージを出力する。
4. `progress` に、出たさいころの目の値を加算する。
5. まだゴールしていない場合は、`残りのマスは47マスです。` のように、残りのマス数(`map` から `progress` を引いた値)を含めたメッセージを出力する。ゴールした場合はこのメッセージを出力しない。
繰り返しが終わったら `===== GOAL =====` と出力すること。
- 補足: さいころの目や繰り返し回数はランダムな値によって変わるため、出力される行数や具体的な値は実行のたびに変わってよい。上記のメッセージの形式・内容が満たされていることを確認する。
---
## 設問5
じゃんけんを行うプログラムを作成する。手は次の整数値で表す。
- `0`: グー
- `1`: チョキ
- `2`: パー
勝敗の状態を表す真偽値の変数 `winFlag` があり、初期値は「負け」を表す値になっている。プレイヤーが勝つまで、次の処理を繰り返すこと。
1. `じゃんけんを行います。`、`あなたの出す手を入力してください`、`0:グー, 1:チョキ, 2:パー` と出力し、数値の入力を1つ受け取って、プレイヤーの手を表す変数 `playerHand` に格納する。
2. `playerHand` の値に応じて、`あなたはグーを出しました`(`0`の場合)のように、選ばれた手が分かるメッセージを出力する。`1`(チョキ)・`2`(パー)の場合も同様に、対応する手の名前を含めたメッセージを出力する。
3. 0~2のいずれかのランダムな整数を生成し、対戦相手の手を表す変数 `npcHand` に格納する。
4. `npcHand` の値に応じて、`相手はグーを出しました`(`0`の場合)のように、対戦相手の手が分かるメッセージを出力する。`1`・`2`の場合も同様に対応する手の名前を含めたメッセージを出力する。
5. 次の勝敗ルールに従って `playerHand` と `npcHand` を比較し、じゃんけんの結果(勝ち・負け・引き分け)が分かるメッセージを出力すること。
- グー(`0`)はチョキ(`1`)に勝つ
- チョキ(`1`)はパー(`2`)に勝つ
- パー(`2`)はグー(`0`)に勝つ
- 同じ手同士は引き分け
6. プレイヤーが勝った場合は、繰り返しを終了してプログラムを終了させること。それ以外(負け・引き分け)の場合は、繰り返しの最後に空行を1行出力してから、次の回を繰り返すこと。
- 補足: 対戦相手の手や繰り返し回数はランダムな値によって変わるため、出力される行数や具体的な内容は実行のたびに変わってよい。

View File

@@ -3,13 +3,12 @@ public class Question4_1 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
int num = -1;
int num = 0;
while ( num!=5 ) { while (num != 5) {
num = (int)( Math.random()*10 ); num = (int)(Math.random() * 10);
System.out.println(num); System.out.println(num);
} }
} }
} }

View File

@@ -3,13 +3,12 @@ public class Question4_2 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
int num = 2;
int num = 2;
while ( num<=1000 ) {
num = num*2;
}
System.out.println(num);
} while (num <= 1000) {
num = num * 2;
}
System.out.println(num);
}
} }

View File

@@ -3,16 +3,15 @@ public class Question4_3 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
// ループ
int num = 0; while (true) {
while ( true ) { int num = (int)(Math.random() * 10);
num = (int)( Math.random()*10 );
System.out.println(num);
if ( num==5 ) {
break;
}
}
}
System.out.println(num);
if (num == 5) {
break;
}
}
}
} }

View File

@@ -1,32 +1,32 @@
import java.util.Scanner; import java.util.Scanner;
public class Question4_4 { public class Question4_4 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
int map = 50;
int map = 50; int progress = 0;
int progress = 0;
System.out.println("===== START =====");
while ( true ) {
System.out.println("Enterキーを押してください。さいころをふります。");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int num = (int)( Math.random()*6+1 );
System.out.println(num+"の目が出ました。");
progress = progress + num;
if ( map-progress<=0 ) { break; }
System.out.println("残りのマスは"+(map-progress)+"マスです。");
}
System.out.println("===== GOAL =====");
Scanner scan = new Scanner(System.in);
System.out.println("===== START =====");
while (progress < map) {
System.out.println("Enterキーを押してください。さいころをふります。");
scan.nextLine();
int dice = (int)(Math.random() * 6) + 1;
System.out.println(dice + "の目が出ました。");
progress += dice;
if (progress < map) {
System.out.println("残りのマスは" + (map - progress) + "マスです。");
}
}
System.out.println("===== GOAL =====");
scan.close();
}
} }
}

View File

@@ -1,67 +1,45 @@
import java.util.Scanner; import java.util.Scanner;
public class Question4_5 { public class Question4_5 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
Scanner scan = new Scanner(System.in);
boolean winflag = false;
while ( !winflag ) {
System.out.println("じゃんけんを行います。");
System.out.println("あなたの出す手を入力してください。");
System.out.println("0:グー, 1:チョキ, 2:パー");
Scanner sc = new Scanner(System.in);
int playerHand = sc.nextInt();
switch ( playerHand ) {
case 0 :
System.out.println("あなたはグーを出しました。");
break;
case 1 :
System.out.println("あなたはチョキを出しました。");
break;
case 2 :
System.out.println("あなたはパーを出しました。");
break;
// default :
// System.out.println("不正な入力です。");
// System.exit(0);
}
int npcHand = (int)( Math.random()*3 );
switch ( npcHand ) {
case 0 :
System.out.println("相手はグーを出しました。");
break;
case 1 :
System.out.println("相手はチョキを出しました。");
break;
case 2 :
System.out.println("相手はパーを出しました。");
break;
}
if ( playerHand == npcHand ) {
System.out.println("引き分けました。");
System.out.println("");
continue;
}
if ( ((playerHand==0)&&(npcHand==2)) ||
((playerHand==1)&&(npcHand==0)) ||
((playerHand==2)&&(npcHand==1)) ) {
System.out.println("負けました。");
System.out.println("");
continue;
} else {
System.out.println("勝ちました。");
winflag = true;
}
}
}
} String[] hands = {"グー", "チョキ", "パー"};
boolean winFlag = false;
while (!winFlag) {
System.out.println("じゃんけんを行います。");
System.out.println("あなたの出す手を入力してください");
System.out.println("0:グー, 1:チョキ, 2:パー");
int playerHand = scan.nextInt();
int npcHand = (int)(Math.random() * 3);
System.out.println("あなたは" + hands[playerHand] + "を出しました");
System.out.println("相手は" + hands[npcHand] + "を出しました");
if (playerHand == npcHand) {
System.out.println("引き分けです");
} else if (
(playerHand == 0 && npcHand == 1) ||
(playerHand == 1 && npcHand == 2) ||
(playerHand == 2 && npcHand == 0)
) {
System.out.println("あなたの勝ちです");
winFlag = true;
} else {
System.out.println("あなたの負けです");
}
if (!winFlag) {
System.out.println();
}
}
scan.close();
}
}

View File

@@ -1,35 +1,23 @@
import java.util.Random;
public class Question5_1 { public class Question5_1 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
System.out.println("今日の運勢を占います。"); System.out.println("今日の運勢を占います。");
int num = (int)( Math.random()*5+1 ); String[] omikuji = {"大吉", "吉", "中吉", "小吉", "末吉", "凶"};
// System.out.println(num);
switch (num) {
case 0 :
System.out.println("あなたの運勢は「大吉」です。");
break;
case 1 :
System.out.println("あなたの運勢は「吉」です。");
break;
case 2 :
System.out.println("あなたの運勢は「中吉」です。");
break;
case 3 :
System.out.println("あなたの運勢は「小吉」です。");
break;
case 4 :
System.out.println("あなたの運勢は「末吉」です。");
break;
case 5 :
System.out.println("あなたの運勢は「凶」です。");
break;
}
}
Random random = new Random();
int num = random.nextInt(6);
switch (num) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.println("あなたの運勢は「" + omikuji[num] + "」です");
break;
}
}
} }

View File

@@ -1,47 +1,44 @@
import java.util.Scanner; import java.util.Scanner;
public class Question5_2 { public class Question5_2 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
Scanner scan = new Scanner(System.in);
int num1, num2;
String choice;
Scanner sc = new Scanner(System.in);
System.out.println("1つ目の数値を入力してください");
num1 = sc.nextInt();
System.out.println("2つ目の数値を入力してください");
num2 = sc.nextInt();
System.out.println("計算方法を選択してください");
choice = sc.next();
sc.close();
switch (choice) {
case "+" :
System.out.println(num1+num2);
break;
case "-" :
System.out.println(num1-num2);
break;
case "*" :
System.out.println(num1*num2);
break;
case "/" :
System.out.println((double)num1/(double)num2);
break;
case "%" :
System.out.println(num1%num2);
break;
default :
System.out.println("計算に失敗しました");
break;
}
System.out.println("1つ目の数値を入力してください");
int num1 = scan.nextInt();
System.out.println("2つ目の数値を入力してください");
int num2 = scan.nextInt();
System.out.println("計算方法を選択してください");
String choice = scan.next();
switch (choice) {
case "+":
System.out.println(num1 + num2);
break;
case "-":
System.out.println(num1 - num2);
break;
case "*":
System.out.println(num1 * num2);
break;
case "/":
System.out.println(num1 / num2);
break;
case "%":
System.out.println(num1 % num2);
break;
default:
System.out.println("計算に失敗しました");
}
scan.close();
}
} }
}

View File

@@ -1,67 +1,67 @@
import java.util.Random;
import java.util.Scanner; import java.util.Scanner;
public class Question5_3 { public class Question5_3 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
Scanner scanner = new Scanner(System.in);
System.out.println("=========================================\n" Random random = new Random();
+ " ゲームの説明\n"
+ "=========================================\n"
+ "これからハイ&ローゲームを行います。\n"
+ "ハイ&ローゲームは、あなたのカードがディーラーのカードより大きいか小さいかを予想するゲームです。\n"
+ "\n"
+ "=========================================\n"
+ " ゲーム開始\n"
+ "=========================================");
int dealerCard = (int)( Math.random()*12+1 );
// dealerCard = ;
System.out.println("ディーラーのカードは"+dealerCard+"です");
int myCard = (int)( Math.random()*12+1 );
// myCard = ;
System.out.println("");
System.out.println("あなたのカードがディーラのカードより大きい場合は H 小さい場合は L を入力してください。");
System.out.println("※ カードの数値が同じ場合はあなたの勝ちです。");
String jedgement;
Scanner sc = new Scanner(System.in);
jedgement = sc.next();
sc.close();
System.out.println("あなたのカードは"+myCard+"です");
System.out.println("");
System.out.println("=========================================\n"
+ " 勝敗判定\n"
+ "=========================================");
switch (jedgement) {
case "H" :
if (myCard>=dealerCard) {
System.out.println("あなたの勝ちです");
break;
} else {
System.out.println("あなたの負けです");
break;
}
case "L" :
if (myCard<=dealerCard) {
System.out.println("あなたの勝ちです");
break;
} else {
System.out.println("あなたの負けです");
break;
}
default :
System.out.println("不正な値が入力されました");
break;
}
} System.out.println("=========================================");
System.out.println(" ゲームの説明");
System.out.println("=========================================");
System.out.println("これからハイ&ローゲームを行います。");
System.out.println("ハイ&ローゲームは、あなたのカードがディーラーのカードより大きいか小さいかを予想するゲームです。");
System.out.println();
System.out.println("=========================================");
System.out.println(" ゲーム開始");
System.out.println("=========================================");
System.out.println();
// ディーラーのカード
int dealerCard = random.nextInt(13) + 1;
System.out.println("ディーラーのカードは" + dealerCard + "です");
// 自分のカード
int myCard = random.nextInt(13) + 1;
System.out.println();
System.out.println("あなたのカードがディーラのカードより大きい場合は H 小さい場合は L を入力してください。");
System.out.println("※ カードの数値が同じ場合はあなたの勝ちです。");
String judgement = scanner.nextLine();
System.out.println("あなたのカードは" + myCard + "です");
System.out.println();
System.out.println("=========================================");
System.out.println(" 勝敗判定");
System.out.println("=========================================");
System.out.println();
switch (judgement) {
case "H":
if (myCard >= dealerCard) {
System.out.println("あなたの勝ちです");
} else {
System.out.println("あなたの負けです");
}
break;
case "L":
if (myCard == dealerCard || myCard < dealerCard) {
System.out.println("あなたの勝ちです");
} else {
System.out.println("あなたの負けです");
}
break;
default:
System.out.println("不正な値が入力されました");
}
scanner.close();
}
} }

View File

@@ -1 +1,2 @@
/build/ /build/
/bin/

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<projectDescription> <projectDescription>
<name>Question17-StackTrace</name> <name>Question06-for</name>
<comment></comment> <comment></comment>
<projects> <projects>
</projects> </projects>

View File

@@ -0,0 +1,100 @@
# Question06 - 条件分岐と繰り返し処理
本資料の練習問題は、複数のプログラミング言語(Java / C# / PHP / Python)で共通して利用できるよう記述されています。静的型付け・動的型付けの違いや文字列の連結ルールなど、言語固有の文法仕様に基づき、問題文の表現をご使用の言語の仕様に適宜読み替えて進めてください。
## この問題のねらい
- 繰り返し処理を使って、指定した範囲の値を順に処理できること
- 繰り返しの中で条件分岐を組み合わせ、条件を満たす場合だけ処理を行えること
- 繰り返しの中で、特定の条件のときだけその回の処理を打ち切れること
- 繰り返しを組み合わせて(繰り返しの中でさらに繰り返しを行って)、行と列からなる出力を作成できること
以下の設問はそれぞれ独立したプログラムとして実装してください。
---
## 設問1
繰り返し処理を使って、0から99までの整数を小さい順にすべて出力してください。
- 出力: 0から99までの整数を、小さい順に1つずつ出力する
---
## 設問2
以下の出力結果が得られるプログラムを作成してください。
```
■■■■■
```
- 出力: `■` を5個並べた文字列を1行出力する
---
## 設問3
0から100までの整数を1つずつ順に確認し、2で割り切れる整数(偶数)のときだけ、その値をそのまま出力してください。
- 対象範囲: 0以上100以下(0と100を含む)
- 出力: 対象範囲の中で2で割り切れる整数を、小さい順にそのまま出力する(0, 2, 4, ... , 100)
---
## 設問4
1から49までの整数を1つずつ順に確認し、次の規則に従って出力してください。
1. 3で割り切れる場合は `fizz` と出力する
2. 5で割り切れる場合は `buzz` と出力する
3. 3でも5でも割り切れる場合は `fizzbuzz` と出力する(この場合、`fizz` や `buzz` は出力しない)
4. 上記のいずれにも当てはまらない場合は、その整数の値をそのまま出力する
- 対象範囲: 1以上49以下
- 出力: 対象範囲の整数を小さい順に1つずつ確認し、それぞれについて上記の規則に従った結果を1つずつ出力する
---
## 設問5
0以上2以下の範囲のランダムな整数を1つ生成する、という試行を10回繰り返してください。生成した値が2の場合はその回では何も出力せず、2以外の場合はその値をそのまま出力してください。
- 期待される結果: 10回の試行のうち、生成された値が2以外だった回数分だけ、その値が出力される(値はランダムなため、実行するたびに出力される個数や内容は変わってよい)
- 補足: ランダムな整数の生成には、使用する言語の標準的な手段を用いてよい
---
## 設問6
以下の出力結果が得られるプログラムを作成してください。
```
■■■■■
■■■■■
■■■■■
■■■■■
■■■■■
```
- 出力: `■` を5個並べた行を、5行分出力する
---
## 設問7
1から9までのかけ算九九の表と同じ内容が得られるプログラムを作成してください。
- 出力: 1から9までの整数を1つずつ順に取り上げ、それぞれについて1倍から9倍までの積を順に並べた行を、9行分出力する。具体的な期待結果は以下の通り(見やすさのために桁を揃えているが、桁揃えなどの見た目上の書式は問わない。各行に、その行に対応する9個の積が1倍から9倍の順で含まれていればよい)。
```
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81
```

View File

@@ -3,11 +3,11 @@ public class Question6_1 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
//for文
for ( int i = 0 ; i<100 ; i++ ) { //「++」⇒インクリメント i+1
for (int i = 1; i <100; i++) {
System.out.println(i); System.out.println(i);
} }
} }
} }

View File

@@ -3,18 +3,9 @@ public class Question6_2 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
for (int i = 0; i < 5; i++) {
StringBuilder square = new StringBuilder(); System.out.print("■");
for ( int i=0 ; i<5 ; i++ ) { }
square.append('\u25A0'); // ■ = \u25A0
}
System.out.println(square);
// 1009
for ( int i=0 ; i<5 ; i++ ) {
System.out.print("\u25A0");
}
} }
} }

View File

@@ -3,13 +3,12 @@ public class Question6_3 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
//for文
for ( int i = 0 ; i<=100 ; i++ ) { for (int i = 0; i <= 100; i++) {
if ( i%2 == 0 ) { if (i % 2 == 0) {
System.out.println(i); System.out.println(i);
} }
} }
} }
} }

View File

@@ -3,22 +3,19 @@ public class Question6_4 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
for (int i = 1; i <= 50; i++) {
for ( int i = 1 ; i<50 ; i++ ) { if (i % 5 == 0 && i % 3 == 0) {
System.out.println("fizzbuzz");
if ( (i%3 == 0) && (i%5 == 0) ) { } else if (i % 5 == 0) {
System.out.println("fizzbuzz"); System.out.println("buzz");
continue; } else if (i % 3 == 0) {
} else if ( i%3==0 ) {
System.out.println("fizz"); System.out.println("fizz");
} else if ( i%5==0 ) {
System.out.println("buzz");
} else { } else {
System.out.println(i); System.out.println(i);
} }
} }
} }
} }

View File

@@ -1,22 +1,17 @@
import java.util.Random;
public class Question6_5 { public class Question6_5 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
Random gacha = new Random();
int rand;
for ( int i = 0 ; i<10 ; i++) { for (int i = 0; i < 10; i++) {
int num = gacha.nextInt(3);
rand = (int)( Math.random()*3 );
if (num != 2) {
if ( rand==2 ) { System.out.println(num);
// no output }
} else {
System.out.println(rand);
}
} }
} }
} }

View File

@@ -3,23 +3,11 @@ public class Question6_6 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
for (int i = 0; i < 5; i++) {
StringBuilder square = new StringBuilder(); for (int j = 0; j < 5; j++) {
for ( int i=0 ; i<5 ; i++ ) { System.out.print("■");
square.append('\u25A0'); // ■ = \u25A0 }
} System.out.println();
for ( int i=0 ; i<5 ; i++ ) {
System.out.println(square);
}
// 1009
for (int j=0 ; j<5 ; j++ ) {
for ( int i=0 ; i<5 ; i++ ) {
System.out.print("\u25A0");
} }
System.out.println("");
}
} }
} }

View File

@@ -3,29 +3,14 @@ public class Question6_7 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
for (int i = 1; i <= 9; i++) {
StringBuilder sb = new StringBuilder();
for ( int i=1 ; i<=9 ; i++ ) { for (int k = 1; k <= 9; k++) {
for ( int j=1 ; j<=9 ; j++ ) { System.out.print(i * k + " ");
if ( j>=2 ) {
sb.append(" "); // *2以降には空白を追加する
}
sb.append(i*j); // 計算結果を文字列として追加していく
} }
System.out.println(sb);
sb.delete(0,sb.length()); // 追記した文字列をクリアする System.out.println();
}
// 1009
for ( int i=1 ; i<=9 ; i++ ) {
for ( int j=1 ; j<=9 ; j++ ) {
System.out.print(i*j);
if ( j==9 ) { continue; }
System.out.print(" ");
} }
System.out.println("");
}
} }
} }

View File

@@ -3,16 +3,15 @@ public class Question7_1 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
int[] num = new int[5];
int nums[] = new int[5];
nums[0] = 1;
nums[1] = 3;
nums[2] = 5;
nums[3] = 7;
nums[4] = 9;
System.out.println(nums[1]);
num[0] = 1;
num[1] = 3;
num[2] = 5;
num[3] = 7;
num[4] = 9;
System.out.println(num[1]);
} }
} }

View File

@@ -3,19 +3,15 @@ public class Question7_10 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
int[] numbers = new int[100];
int[] nums = new int[100];
// ArrayIndexOutOfBoundException
// for ( int i=1 ; i<=100 ; i++ ) {
// nums[i] = i+1;
// }
for ( int i=0 ; i<100 ; i++ ) {
nums[i] = i+1;
System.out.println(nums[i]);
}
} //格納
for (int i = 0; i < numbers.length; i++) {
} numbers[i] = i + 1;
}
//出力
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}

View File

@@ -3,11 +3,7 @@ public class Question7_2 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
int[] num = {1, 10, 100};
int[] nums = {1 ,10, 100}; System.out.println(num[0] + num[2]);
}
System.out.println(nums[0]+nums[2]);
}
} }

View File

@@ -3,13 +3,9 @@ public class Question7_3 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
String[] places = {"森", "山", "沼", "島", "平地"};
String[] str = {"森","山","沼","島","平地"}; for (int i = 0; i < places.length; i++) {
System.out.println(places[i]);
for ( int i=0 ; i<str.length ; i++ ) { }
System.out.println(str[i]); }
}
}
} }

View File

@@ -3,14 +3,14 @@ public class Question7_4 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
int[] num = new int[1000];
int nums[] = new int[1000];
for ( int i=1 ; i<=nums.length ; i++ ) {
nums[i-1] = i;
System.out.println(nums[i-1]);
}
} for (int i = 0; i < num.length; i++) {
num[i] = i + 1;
}
for (int i = 0; i < num.length; i++) {
System.out.println(num[i]);
}
}
} }

View File

@@ -3,12 +3,8 @@ public class Question7_5 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
String[] places = {"森", "山", "沼", "島", "平地"};
String[] str = {"森","山","沼","島","平地"}; int index = (int)(Math.random() * 5);
System.out.println(places[index]);
int num = (int)( Math.random()*5 ); }
System.out.println(str[num]);
}
} }

View File

@@ -3,12 +3,9 @@ public class Question7_6 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
String[] places = {"森", "山", "沼", "島", "平地"};
String[] str = {"森","山","沼","島","平地"}; for (int i = places.length - 1; i >= 0; i--) {
for ( int i=str.length ; i>0 ; i-- ) { System.out.println(places[i]);
System.out.println(str[i-1]); }
} }
}
} }

View File

@@ -3,17 +3,14 @@ public class Question7_7 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
String[][] kyoka = {
String[][] table = { {"国語", "社会", "国語", "理科", "社会"},
{"国語","社会","国語","理科","社会"}, {"英語", "数学", "英語", "数学", "国語"},
{"英語","数学","英語","数学","国語"}, {"理科", "国語", "理科", "国語", "理科"},
{"理科","国語","理科","国語","理科"}, {"社会", "社会", "数学", "社会", "数学"},
{"社会","社会","数学","社会","数学"}, {"数学", "英語", "英語", "理科", "英語"}
{"数学","英語","英語","理科","英語"} };
};
System.out.println(table[1][4]);
} System.out.println(kyoka[1][4]);
}
} }

View File

@@ -3,18 +3,15 @@ public class Question7_8 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
int[][] numbers = {
int[][] array = { {1, 3, 5, 7},
{1,3,5,7}, {11, 13, 17, 19, 23, 29}
{11,13,17,19,23,29} };
};
for ( int i=0 ; i<array.length ; i++) {
for ( int j=0 ; j<array[i].length ; j++ ) {
System.out.println(array[i][j]);
}
}
} for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers[i].length; j++) {
} System.out.println(numbers[i][j]);
}
}
}
}

View File

@@ -3,22 +3,23 @@ public class Question7_9 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
int[][] numbers = {
int[][] array = { {1, 3, 5, 7},
{1,3,5,7}, {11, 13, 17, 19, 23, 29}
{11,13,17,19,23,29} };
};
int count=0;
double sum=0;
for ( int i=0 ; i<array.length ; i++) {
for ( int j=0 ; j<array[i].length ; j++ ) {
sum += array[i][j];
count++;
}
}
System.out.println(sum/count);
} int sum = 0;
int count = 0;
} for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers[i].length; j++) {
sum += numbers[i][j];
count++;
}
}
double average = (double) sum / count;
System.out.println(average);
}
}

View File

@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="output" path="build/classes"/>
</classpath>

View File

@@ -1 +1,2 @@
/build/ /build/
/bin/

View File

@@ -1,9 +0,0 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
org.eclipse.jdt.core.compiler.compliance=21
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=21

View File

@@ -1,4 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<installed facet="java" version="21"/>
</faceted-project>

View File

@@ -1,15 +0,0 @@
public class Question8_1 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
hello() ;
}
public static void hello() {
System.out.println("こんにちは");
}
}

View File

@@ -1,15 +0,0 @@
public class Question8_2 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
hello("山田");
}
public static void hello( String name ) {
System.out.println("こんにちは。"+name+"です。");
}
}

View File

@@ -1,18 +0,0 @@
public class Question8_3 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
String str;
str = hello();
System.out.println(str);
}
public static String hello() {
return "こんにちは";
}
}

View File

@@ -1,20 +0,0 @@
public class Question8_4 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
int sum;
int x = 1;
int y = 2;
sum = sum(x,y);
System.out.println(sum);
}
public static int sum( int x, int y ) {
return x+y;
}
}

View File

@@ -1,27 +0,0 @@
import java.util.Scanner;
public class Question8_5 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
int num1, num2;
Scanner sc = new Scanner(System.in);
System.out.println("1つ目の数値を入力してください");
num1 = sc.nextInt();
System.out.println("2つ目の数値を入力してください");
num2 = sc.nextInt();
sc.close();
System.out.println(sum(num1,num2));
}
public static int sum( int x, int y ) {
return x+y;
}
}

View File

@@ -1,24 +0,0 @@
public class Question8_6 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
int[] array = addArray(10,20);
System.out.println(array[0]);
System.out.println(array[1]);
}
public static int[] addArray( int x, int y ) {
int array[] = new int[2];
array[0] = x;
array[1] = y;
return array;
}
}

View File

@@ -1,30 +0,0 @@
public class Question8_7 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
printValue("こんにちは");
printValue(100);
printValue(10.1F);
printValue('A');
}
public static void printValue( String arg ) {
System.out.println("引数は"+arg+"です");
}
public static void printValue( int arg ) {
System.out.println("引数は"+arg+"です");
}
public static void printValue( float arg ) {
System.out.println("引数は"+arg+"です");
}
public static void printValue( char arg ) {
System.out.println("引数は"+arg+"です");
}
}

View File

@@ -1,25 +0,0 @@
public class Question8_8 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
System.out.println(sum(1,2));
System.out.println(sum(1,2,3));
System.out.println(sum(1,2,3,4));
}
public static int sum( int x1, int x2 ) {
return x1+x2;
}
public static int sum( int x1, int x2, int x3 ) {
return x1+x2+x3;
}
public static int sum( int x1, int x2, int x3, int x4 ) {
return x1+x2+x3+x4;
}
}

View File

@@ -1,12 +1,10 @@
package Question9_1;
public class Question9_1 { public class Question9_1 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
util util = new util();
Util util = new Util(); util.hello();
util.hello(); }
}
} }

View File

@@ -0,0 +1,7 @@
package Question9_1;
public class util {
void hello() {
System.out.println("こんにちは");
}
}

View File

@@ -1,8 +0,0 @@
public class Util {
public void hello() {
System.out.println("こんにちは");
}
}

View File

@@ -1,12 +1,10 @@
package common;
public class Question9_2 { public class Question9_2 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
common.util util = new common.util();
common.Util util = new common.Util(); util.showSum(10, 20);
util.showSum(1,2); }
}
} }

View File

@@ -1,13 +1,10 @@
import common.Util; package common;
public class Question9_3 { public class Question9_3 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
util util = new util();
Util util = new Util(); util.showSum(10, 20);
util.showSum(1,2); }
}
} }

View File

@@ -1,9 +0,0 @@
package common;
public class Util {
public void showSum(int num1,int num2) {
System.out.println(num1+num2);
}
}

View File

@@ -0,0 +1,7 @@
package common;
public class util {
public void showSum(int num1, int num2) {
System.out.println(num1 + num2);
}
}

View File

@@ -1,10 +1,8 @@
public class Calculation { public class Calculation {
int baseNum;
int baseNum;
void add(int num) {
public void add(int num) { baseNum += num;
this.baseNum += num; }
}
} }

View File

@@ -1,19 +1,17 @@
import java.util.Random;
public class Car { public class Car {
String color;
String color; int gasoline;
int gasoline;
public int run() {
this.gasoline--;
if (this.gasoline>=0) {
return (int)(Math.random()*15+1) ;
} else {
return -1;
}
}
int run() {
gasoline--;
if (gasoline >= 0) {
Random random = new Random();
return random.nextInt(15) + 1;
} else {
return -1;
}
}
} }

View File

@@ -3,13 +3,12 @@ public class Question10_1 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
User user = new User();
User user = new User();
user.id = 1000;
user.name = "山田太郎";
System.out.println(user.id);
System.out.println(user.name);
} user.id = 1000;
user.name = "山田太郎";
System.out.println(user.id);
System.out.println(user.name);
}
} }

View File

@@ -3,12 +3,12 @@ public class Question10_2 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
User user = new User();
User user = new User();
user.id = 2000;
user.name = "佐藤二郎";
user.printProfile();
}
user.id = 2000;
user.name = "佐藤二郎";
System.out.println(user.id);
System.out.println(user.name);
}
} }

View File

@@ -3,18 +3,16 @@ public class Question10_3 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
Student student = new Student();
Student student = new Student();
student.studentId = 10;
student.name = "田中二郎";
student.mail = "test@mail.com";
student.password = "pass";
System.out.println(student.studentId);
System.out.println(student.name);
System.out.println(student.mail);
System.out.println(student.password);
} student.studentId = 10;
student.name = "田中二郎";
student.mail = "test@mail.com";
student.password = "pass";
System.out.println(student.studentId);
System.out.println(student.name);
System.out.println(student.mail);
System.out.println(student.password);
}
} }

View File

@@ -3,13 +3,12 @@ public class Question10_4 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
Calculation cal = new Calculation();
Calculation calculation = new Calculation();
calculation.baseNum = 100;
calculation.add(100);
System.out.println(calculation.baseNum);
}
cal.baseNum = 100;
cal.add(100);
System.out.println(cal.baseNum);
}
} }

View File

@@ -3,14 +3,14 @@ public class Question10_5 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
Car car = new Car();
Car car = new Car();
car.color = "赤"; car.color = "赤";
car.gasoline = 50; car.gasoline = 50;
int progress = car.run();
System.out.println(car.color+"色の車は"+progress+"進みました"); int distance = car.run();
System.out.println(car.gasoline);
System.out.println(car.color + "色の車は" + distance + "進みました");
} System.out.println(car.gasoline);
}
} }

View File

@@ -3,26 +3,34 @@ public class Question10_6 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
Car car = new Car();
car.color = "赤";
car.gasoline = 50;
int distance = 300;
int nRuns = 0;
while (true) {
distance -= car.run();
nRuns++;
if (car.gasoline<0) {
System.out.println(car.color+"色の車は、目的地に到達できませんでした。");
break;
}
if (distance<=0) {
System.out.println(car.color+"色の車は、目的地にまで"+nRuns+"時間かかりました。残りのガソリンは、"+car.gasoline+"リットルです。");
break;
}
}
}
Car car = new Car();
car.color = "赤";
car.gasoline = 50;
// 目的地までの距離
int destination = 300;
// run回数
int time = 0;
while (destination > 0) {
int distance = car.run();
if (distance == -1) {
System.out.println(car.color + "色の車は目的地に到達できませんでした");
return;
}
destination -= distance;
time++;
}
System.out.println(
car.color + "色の車は目的地にまで"
+ time
+ "時間かかりました。残りのガソリンは、"
+ car.gasoline
+ "リットルです");
}
} }

View File

@@ -3,31 +3,33 @@ public class Question10_7 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
// 配列作成
User[] userList = new User[3]; User[] userList = new User[3];
User user1 = new User();
User user2 = new User();
User user3 = new User();
user1.id = 1;
user1.name = "一郎";
user2.id = 2;
user2.name = "二郎";
user3.id = 3;
user3.name = "三郎";
userList[0] = user1;
userList[1] = user2;
userList[2] = user3;
// for (int i=0;i<userList.length;i++) {
// userList[i].printProfile();
// }
for (User user:userList) {
user.printProfile();
}
}
// インスタンス
User user1 = new User();
User user2 = new User();
User user3 = new User();
// user1
user1.id = 1;
user1.name = "一郎";
// user2
user2.id = 2;
user2.name = "二郎";
// user3
user3.id = 3;
user3.name = "三郎";
userList[0] = user1;
userList[1] = user2;
userList[2] = user3;
//順番に実行
for (int i = 0; i < userList.length; i++) {
userList[i].printProfile();
}
}
} }

View File

@@ -3,42 +3,53 @@ public class Question10_8 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
// 車作成
Car[] carList = new Car[3]; Car car1 = new Car();
car1.color = "赤";
Car car1 = new Car(); car1.gasoline = 50;
car1.color = "赤";
car1.gasoline = 50;
Car car2 = new Car();
car2.color = "青";
car2.gasoline = 60;
Car car3 = new Car();
car3.color = "黄";
car3.gasoline = 70;
carList[0] = car1;
carList[1] = car2;
carList[2] = car3;
for (Car car:carList) {
int distance = 300;
int nRuns = 0;
while (true) {
distance -= car.run();
nRuns++;
if (car.gasoline<0) {
System.out.println(car.color+"色の車は、目的地に到達できませんでした。");
break;
}
if (distance<=0) {
System.out.println(car.color+"色の車は、目的地にまで"+nRuns+"時間かかりました。残りのガソリンは、"+car.gasoline+"リットルです。");
break;
}
}
}
}
Car car2 = new Car();
car2.color = "青";
car2.gasoline = 60;
Car car3 = new Car();
car3.color = "黄";
car3.gasoline = 70;
// 配列
Car[] carList = {car1, car2, car3};
for (Car car : carList) {
int kyori = 300;
int time = 0;
while (kyori > 0) {
int gas = car.run();
if (gas == -1) {
break;
}
kyori -= gas;
time++;
}
if (kyori <= 0) {
System.out.println(
car.color + "色の車は、目的地にまで"
+ time
+ "時間かかりました。残りのガソリンは、"
+ car.gasoline
+ "リットルです"
);
} else {
System.out.println(
car.color + "色の車は、目的地に到達できませんでした"
);
}
}
}
} }

View File

@@ -1,7 +1,8 @@
public class Student { public class Student {
int studentId; int studentId;
String name, mail, password; String name;
String mail;
String password;
} }

View File

@@ -1,11 +1,10 @@
public class User { public class User {
int id;
int id; String name;
String name;
public void printProfile() {
System.out.println("ID"+this.id+":"+this.name);
}
void printProfile() {
System.out.println("ID" + id + ":" + name);
}
} }

View File

@@ -1,20 +0,0 @@
public class Atm {
int credit;
public Atm () {
this.credit = 100000;
}
public void withdraw( Wallet wallet , int money ) {
if ( this.credit < money ) {
System.out.println("預金残高不足です。現金の引き出しに失敗しました。");
} else {
this.credit = this.credit - money;
wallet.money = wallet.money + money;
System.out.println("ATMの残り残高は"+this.credit+"です");
}
}
}

View File

@@ -1,14 +0,0 @@
public class Cat {
String name;
public Cat() {
this.name = "タマ";
}
public Cat( String name ) {
this.name = name;
}
}

View File

@@ -1,10 +0,0 @@
public class Jam {
String material;
public Jam( String jam ) {
this.material = jam;
}
}

View File

@@ -3,18 +3,7 @@ public class Question11_1 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
Wallet wallet = new Wallet();
wallet.money = 10000;
User user = new User();
user.name = "太郎";
user.wallet = wallet;
user.confirmWallet();
user.useMoney(1000);
user.confirmWallet();
}
}
} }

View File

@@ -1,20 +0,0 @@
public class Question11_2 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
Wallet wallet = new Wallet();
wallet.money = 10000;
Atm atm = new Atm();
atm.withdraw(wallet,100000);
System.out.println(wallet.money);
atm.withdraw(wallet,100000);
System.out.println(wallet.money);
}
}

View File

@@ -1,15 +0,0 @@
public class Question11_3 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
Cat cat1 = new Cat();
System.out.println(cat1.name);
Cat cat2 = new Cat("ミケ");
System.out.println(cat2.name);
}
}

View File

@@ -1,17 +0,0 @@
public class Question11_4 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
Student student = new Student();
student.name = "太郎";
student.group = "A";
student.teacher = "山田";
student.studentProfile();
student.groupProfile();
}
}

View File

@@ -1,14 +0,0 @@
public class Question11_5 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
Jam jam = new Jam("ブルーベリー");
Toast toast = new Toast(jam);
toast.taste();
}
}

View File

@@ -1,16 +0,0 @@
public class Student {
String name;
static String group;
static String teacher;
public void studentProfile() {
System.out.println(this.name+"は"+this.group+"教室の生徒です。");
}
public void groupProfile() {
System.out.println(this.group+"教室の担任は"+this.teacher+"先生です。");
}
}

Some files were not shown because too many files have changed in this diff Show More