/*
* C#で数あてゲーム
*/
using System;
using System.Collections.Generic;
using System.Text;
class Program
{
static void Main(string[] args)
{
while (true)
{
//randnoにNewnumメソッド
int randno = Newnum(1, 101);
//トライ回数初期値1設定
int count = 1;
while (true)
{
//1.文字列を出力(ゲーム説明)
Console.Write("数あてゲームです。1から100の間で数字を予想し、入力してください。(0を押すと終了します):");
//2.変数定義
//入力する数字を読み込む
//Convert.ToInt32(数値) --> 指定した数値の文字列形式を等価の32ビット符号付き整数に変換
//Console.ReadLine() --> ユーザーの入力した値を読み込む
int input = Convert.ToInt32(Console.ReadLine());
//3.もし0が入力されたら
if (input == 0)
//4. 0を返す(ゲーム終了する)
return;
//4.それ以外なら
//5.答えの数値が入力された数値より大きかった場合、
else if (input < randno)
{
Console.WriteLine("もっと大きな数字ですよ");
//「正解まで●●回でした」の回数を記憶
++count;
//ループを継続
continue;
}
//6.答えの数値が入力された数値より小さかった場合、
else if (input > randno)
{
Console.WriteLine("もっと小さな数字ですよ");
++count;
continue;
}
//それ以外の場合(入力された数字と一致した場合)
else
{
// {0} -> randnoを出力
Console.WriteLine("ドンピシャ!正解は{0}でした。", randno);
Console.WriteLine("正解まで、" + count + "回でした。");
break;
}
}
}
}
//数字をランダムで引くメソッド
// Newnumメソッドを定義(最小値,最大値)
static int Newnum(int min, int max)
{
//数をランダムで引く関数を定義
Random random = new Random();
//random.Next -->
//最小値〜最大値の間でランダムの数字を引くものを返す
return random.Next(min, max);
//乱数を生成するときのテンプレ
//Random rnd = new Random();
//int randomNumber = rnd.Next(100);
}
}
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme
List of users who liked
01