LoginSignup
0
0

More than 5 years have passed since last update.

数あてゲーム .NET版+C版

Last updated at Posted at 2014-12-08

昨日投稿した「数あてゲーム」(http://qiita.com/kiuyas/items/7654ce645abc3fd7e143 )について、.NET版を作りましたのでお送りします!いずれもコンソールアプリケーションとしてつくり、Java版と比較しやすいようにしました。

ついでに、前に作成したC言語版も発掘したので載せます!こちらはいくらか前に作ったので、少々コーディングの仕方が違いますが、基本は同じです。

C#版

Program.cs
using System;
using System.Text;

namespace Kazuate
{
    class Program
    {
        static void Main(string[] args)
        {
            Random random = new Random();

            int ans = random.Next(1000);
            int count = 0;
            int userAns = 0;

            Console.WriteLine("0~999の数字を入力してください。");

            while (count < 10)
            {
                Console.Write("{0}> ", count + 1);
                userAns = getNumberInput();
                Console.Write("\t{0} ..... ", userAns);
                if (ans == userAns)
                {
                    Console.WriteLine("正解!");
                    break;
                }
                else if (ans < userAns)
                {
                    Console.WriteLine("大きすぎます。");
                }
                else
                {
                    Console.WriteLine("小さすぎます。");
                }
                count++;
            }

            if (ans != userAns)
            {
                Console.WriteLine("Game Over! 正解は " + ans + " でした。");
            }

            Console.WriteLine("何かキーを押してください。");
            Console.ReadLine();
        }

        private static int getNumberInput()
        {
            int userAns = -1;

            do
            {
                String s = getStringInput();
                if (!int.TryParse(s, out userAns))
                {
                    Console.Write("もう一度入力してください > ");
                    userAns = -1;
                }
            } while (userAns == -1);

            return userAns;
        }

        private static String getStringInput()
        {
            return Console.ReadLine();
        }
    }
}

VB.NET版

Module1.vb
Module Module1

    Sub Main()
        Dim random As New Random()

        Dim ans As Integer = random.Next(1000)
        Dim count As Integer = 0
        Dim userAns As Integer = 0

        Console.WriteLine("0~999の数字を入力してください。")

        While count < 10
            Console.Write("{0}> ", count + 1)
            userAns = getNumberInput()
            Console.Write(vbTab + "{0} ..... ", userAns)
            If (ans = userAns) Then
                Console.WriteLine("正解!")
                Exit While
            ElseIf (ans < userAns) Then
                Console.WriteLine("大きすぎます。")
            Else
                Console.WriteLine("小さすぎます。")
            End If
            count += 1
        End While

        If ans <> userAns Then
            Console.WriteLine("Game Over! 正解は {0} でした。", ans)
        End If

        Console.WriteLine("何かキーを押してください。")
        Console.ReadLine()
    End Sub

    Private Function getNumberInput() As Integer
        Dim userAns As Integer = -1

        Do
            Dim s As String = getStringInput()
            If Not Integer.TryParse(s, userAns) Then
                Console.Write("もう一度入力してください > ")
                userAns = -1
            End If
        Loop While (userAns = -1)

        Return userAns
    End Function

    Private Function getStringInput() As String
        Return Console.ReadLine()
    End Function
End Module

C言語版

Kazuate.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void) {
    int i;
    int a;
    int b;

    srand(time(NULL));
    a=(int)(rand()*1000.0/RAND_MAX);

    for(i=1;i<=10;i++) {
        printf("%d : ", i);
        scanf("%d", &b);

        if (a<b) {
            printf(" %d ... Larger\n", b);
        } else if (a>b) {
            printf(" %d ... Smaller\n", b);
        } else {
            printf(" %d ... Correct!\n", b);
            i=0;
            break;
        }
    }

    if (i > 0) {
        printf("Game Over! Answer: %d\n", a);
    }

    return EXIT_SUCCESS;
}
0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0