C#数値判定について

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
ぼのりん
記事: 18
登録日時: 11年前

C#数値判定について

#1

投稿記事 by ぼのりん » 11年前

閲覧ありがとうございます

今、C#でコンソールアプリケーションのじゃんけんゲームを作っているのですが、
数字以外を打つとエラーがでてしまい解決方法に困ってます。

10進数の数字だけ入力を受け付けるようにしたいのですが、まだプログラミングを始めて日も浅いのでネットなどで調べてもわからず ここに質問させていただきます。

コード:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int player;
            int cpu;
            int a=1;

            string player1 = "";
            string cpu1    = "";

            Random rand = new Random();

            Console.WriteLine("じゃんけん始めるよ\n数字を入力してね\n1グー、2チョキ、3パー");

            while (a != 0) 
            {
                player = int.Parse(Console.ReadLine());
                cpu = rand.Next(1, 4);

                if (cpu == player)
                {
                    Console.WriteLine("あいこだよ、もう一度入力してね");
                    continue;
                }
                else if (player == 1 && cpu == 2)
                {
                    Console.WriteLine("プレイヤー、グー");
                    Console.WriteLine("CPU、チョキ");
                    Console.WriteLine("勝ち");
                }
                else if (player == 2 && cpu == 3)
                {
                    Console.WriteLine("プレイヤー、チョキ");
                    Console.WriteLine("CPU、パー");
                    Console.WriteLine("勝ち");
                }
                else if (player == 3 && cpu == 1)
                {
                    Console.WriteLine("プレイヤー、パー");
                    Console.WriteLine("CPU、グー");
                    Console.WriteLine("勝ち");
                }
                else if (player < 1 || player > 3)
                {
                    Console.WriteLine("1か2か3を入力してね");                    
                    continue;
                }
                else
                {
                    if (player == 1) player1 = player.ToString("グー");
                    if (player == 2) player1 = player.ToString("チョキ");
                    if (player == 3) player1 = player.ToString("パー");
                    if (cpu == 1) cpu1 = cpu.ToString("グー");
                    if (cpu == 2) cpu1 = cpu.ToString("チョキ");
                    if (cpu == 3) cpu1 = cpu.ToString("パー");
                    
                    Console.WriteLine("プレイヤー、{0}\nCPU、{1}",player1,cpu1);
                    Console.WriteLine("負け");
                }

                if (player != cpu)
                {                   
                    Console.WriteLine("\n終わるなら0を入力してね\n続ける場合は0以外の数字を入力してね");
                    a = int.Parse(Console.ReadLine());
                    Console.Clear();
                    Console.WriteLine("じゃんけん始めるよ\n数字を入力してね\n1グー、2チョキ、3パー");
                }
                
                if(a==0)
                {
                    Console.Clear();
                    Console.Write("終了します");
                }

            } 
            
        }
    }
}

           
回答お待ちしてます。

アバター
へにっくす
記事: 634
登録日時: 13年前
住所: 東京都

Re: C#数値判定について

#2

投稿記事 by へにっくす » 11年前

これ参考になりますでしょうか?
文字列が数値に変換できるか調べる - DOBON.NET

私ならInt32.TryParseかな。
written by へにっくす

ぼのりん
記事: 18
登録日時: 11年前

Re: C#数値判定について

#3

投稿記事 by ぼのりん » 11年前

じゃんけんゲームが完成したので、ソースコードを貼っておきます。

回答ありがとうございました。

コード:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int player=1;
            int cpu=1;
            int a=1;

            string player1 = "";
            string cpu1    = "";

            Random rand = new Random();

            Console.WriteLine("10進数の数字で入力してください");
            Console.WriteLine("じゃんけん始めるよ\n数字を入力してね\n1グー、2チョキ、3パー");

            while (a != 0) 
            {
                try
                {              
                    player = int.Parse(Console.ReadLine());
                    
                    cpu = rand.Next(1, 4);

                    if (cpu == player)
                    {
                        Console.WriteLine("あいこだよ、もう一度入力してね");
                        continue;
                    }
                    else if (player == 1 && cpu == 2)
                    {
                        Console.WriteLine("プレイヤー、グー");
                        Console.WriteLine("CPU、チョキ");
                        Console.WriteLine("勝ち");
                    }
                    else if (player == 2 && cpu == 3)
                    {
                        Console.WriteLine("プレイヤー、チョキ");
                        Console.WriteLine("CPU、パー");
                        Console.WriteLine("勝ち");
                    }
                    else if (player == 3 && cpu == 1)
                    {
                        Console.WriteLine("プレイヤー、パー");
                        Console.WriteLine("CPU、グー");
                        Console.WriteLine("勝ち");
                    }
                    else if (player < 1 || player > 3)
                    {
                        Console.WriteLine("1か2か3を入力してね");
                        continue;
                    }
                    else
                    {
                        if (player == 1) player1 = player.ToString("グー");
                        if (player == 2) player1 = player.ToString("チョキ");
                        if (player == 3) player1 = player.ToString("パー");
                        if (cpu == 1) cpu1 = cpu.ToString("グー");
                        if (cpu == 2) cpu1 = cpu.ToString("チョキ");
                        if (cpu == 3) cpu1 = cpu.ToString("パー");

                        Console.WriteLine("プレイヤー、{0}\nCPU、{1}", player1, cpu1);
                        Console.WriteLine("負け");
                    }

                    //終了分岐の呼び出し
                    bunki(ref player,ref cpu,ref a);

                    if (a == 0)
                    {
                        Console.Clear();
                        Console.Write("終了します");
                    }                    
                }
                catch (FormatException)
                {

                        Console.Clear();

                        Console.WriteLine("10進数の数字で入力してください");
                        Console.WriteLine("じゃんけん始めるよ\n数字を入力してね\n1グー、2チョキ、3パー");
                        continue;
                     }
                }             
            }

        //終了分岐のメソッド
        static void bunki(ref int player,ref int cpu,ref int a)
        {
            if (player != cpu)
            {
                Console.WriteLine("\n終わるなら0を入力してね\n続ける場合は0以外の数字を入力してね");
                a = int.Parse(Console.ReadLine());
                Console.Clear();
                Console.WriteLine("10進数の数字で入力してください");
                Console.WriteLine("じゃんけん始めるよ\n数字を入力してね\n1グー、2チョキ、3パー");
            }
        }
    }
}

            
        

閉鎖

“C言語何でも質問掲示板” へ戻る