1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
//数あてゲーム〜♪
import java.io.*;
class KazuateGame{
public static void main(String[] args)
throws IOException{
BufferedReader br = new
BufferedReader(
new InputStreamReader(System.in));
//変数の宣言
String str;
int myAns, cmpAns;
int cnt = 0;
boolean gamePlay = true;
cmpAns = (int)(Math.random()*100 + 1);
//正解するまで無限ループする
while(gamePlay){
cnt++;
try{
System.out.print
("1〜100の間の整数を");
System.out.print
("入力してほしいです:");
str = br.readLine();
myAns = Integer.parseInt(str);
}
catch(NumberFormatException nfe){
//正しい値が入力されなかったので
//ループの最初に戻る
continue;
}
if(myAns<1 || myAns>100){
//正しい値が入力されなかったので
//ループの最初に戻る
continue;
}
else if(myAns < cmpAns){
System.out.println
("入力した数値より大きいです〜");
}
else if(myAns > cmpAns){
System.out.println
("入力した数値より小さいです〜");
}
else if(myAns == cmpAns){
/*
正解したのでフラグをfalseにする
breakをするのと一緒(ループから抜ける)
*/
gamePlay = false;
}
}
System.out.println("おみごとです〜 ");
System.out.println(cnt +"回目で正解です〜");
}
}
|
//数あてゲーム〜C#Ver♪
using System;
class KazuateGame{
static void Main(){
//変数の宣言
string str;
int myAns, cmpAns;
int cnt = 0;
bool gamePlay = true;
Random r = new Random();
cmpAns = r.Next(1,101); //答えを乱数で作る
//正解するまで無限ループする
while(gamePlay){
cnt++;
try{
Console.Write
("1〜100の間の整数を");
Console.Write
("入力してほしいです:");
str = Console.ReadLine();
myAns = Convert.ToInt32(str);
}
catch(FormatException){
//正しい値が入力されなかったので
//ループの最初に戻る
continue;
}
if(myAns<1 || myAns>100){
//正しい値が入力されなかったので
//ループの最初に戻る
continue;
}
else if(myAns < cmpAns){
Console.WriteLine
("入力した数値より大きいです〜");
}
else if(myAns > cmpAns){
Console.WriteLine
("入力した数値より小さいです〜");
}
else if(myAns == cmpAns){
/*
正解したのでフラグをfalseにする
breakをするのと一緒(ループから抜ける)
*/
gamePlay = false;
}
}
Console.WriteLine("おみごとです〜 ");
Console.WriteLine(cnt +"回目で正解です〜");
}
}
|