今、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("終了します");
}
}
}
}
}