C# 从字符串中转换 Int
C# Casting Int from String
我正在努力了解如何才能拥有它
string rock = 1 for inputting
string paper = 2 for input
string scissors = 3 for input
所以我可以使用 int 1,2 和 3 打印字符串作为输出。
基本上,控制台应用程序有 3 个输入选项:1(rock)、2(paper) 或 3(scissors) 并且控制台需要从 1,2 或 3 中生成一个随机数,并确定您的输入是否超过计算机随机数。
string rock = (int)1;
string paper;
string scissors;
int input;
Random random = new Random();
int RandomNumber = random.Next(1,4);
bool i = true;
Console.WriteLine("-- Weapons Menu --");
Console.WriteLine("-------------------");
Console.WriteLine("1] Rock");
Console.WriteLine("2] Paper");
Console.WriteLine("3] Scissors");
Console.WriteLine("Choose Your Weapon [1, 2 or 3]:");
input = int.Parse(Console.ReadLine());
if (input == RandomNumber )
Console.WriteLine("You Tied The Computer This Round");
Console.WriteLine("Player Chose ")
任何帮助都将非常有用,谢谢。我尝试转换 int 但我不能。
- 使用
Int32.TryParse(Console.ReadLine(), out input);
将字符串转换为整数。
- 要将 Int 转换为字符串,您必须使用
input.ToString();
- 那么你可以使用
Dictionary<int, string>
来实现目标。
您的代码中的问题出在这一行:string rock = (int)1;
相反,你应该给 string rock = "1";
使用 Dictionary<int, string>
:
var choices = new Dictionary<int, string> { {1,"Rock"}, {2,"Paper"}, {3,"Scissors"} };
然后访问正确的元素:
Console.WriteLine("Player Chose {0}", choices[input]);
您需要对此进行一些验证,以确保您不会尝试访问不存在的元素,或者用户不会输入非数字值。
var random = new Random();
while (true)
{
int RandomNumber = random.Next(1, 4);
var userinput = Console.ReadLine();
if (Enumerable.Range(1, 3).Contains(Convert.ToInt16(userinput)))
if (userinput == RandomNumber.ToString())
Console.WriteLine("Tied");
else
Console.WriteLine("Check and see who won!");
else
Console.WriteLine("Try Again!");
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RockPaperScissors
{
class Program
{
static void Main(string[] args)
{
var choices = new Dictionary<int, string> { { 1, "Rock" }, { 2, "Paper" }, { 3, "Scissors" } };
int input;
var randomly = new Dictionary<int, string> { { 1, "Rock" }, { 2, "Paper" }, { 3, "Scissors" } };
Random random = new Random();
int RandomNumber = random.Next(1, 4);
int Ties;
int Wins;
int Losses;
do
{
//Counter
Wins = 0;
Losses = 0;
Ties = 0;
Console.WriteLine("-- Weapons Menu --");
Console.WriteLine("-------------------");
Console.WriteLine("1] Rock");
Console.WriteLine("2] Paper");
Console.WriteLine("3] Scissors");
Console.WriteLine("Choose Your Weapon [1, 2 or 3]:");
input = int.Parse(Console.ReadLine());
if (input == RandomNumber)
Console.WriteLine("You Tied The Computer");
Ties++;
//Winning
if (input == 1 && RandomNumber == 3)
Console.WriteLine("You Beat The Computer :)");
Wins++;
if (input == 2 && RandomNumber == 1)
Console.WriteLine("You Beat The Computer :)");
Wins++;
if (input == 3 && RandomNumber == 2)
Console.WriteLine("You Beat The Computer :)");
Wins++;
//Lossing
if (input == 1 && RandomNumber == 2)
Console.WriteLine("Sorry You Lost To The Computer :(");
Losses++;
if (input == 2 && RandomNumber == 3)
Console.WriteLine("You Beat The Computer :)");
Losses++;
if (input == 3 && RandomNumber == 1)
Console.WriteLine("You Beat The Computer :)");
Losses++;
Console.WriteLine(string.Format("Player Chose {0} : Computer Chose {1}", choices[input], randomly[RandomNumber]));
Console.WriteLine();
Console.WriteLine("<Press Any Key To Continue...>");
Console.WriteLine();
Console.WriteLine("Player Wins Computer Wins");
Console.WriteLine("---------- --------------");
Console.WriteLine(" {0} {1}", Wins, Losses);
Console.ReadKey();
}
while (input != 3) ;
Console.WriteLine("Thanks for playing!");
}enter code here
}
}
STILL CANNOT SHOW THE COUNTER WHY
我正在努力了解如何才能拥有它
string rock = 1 for inputting
string paper = 2 for input
string scissors = 3 for input
所以我可以使用 int 1,2 和 3 打印字符串作为输出。
基本上,控制台应用程序有 3 个输入选项:1(rock)、2(paper) 或 3(scissors) 并且控制台需要从 1,2 或 3 中生成一个随机数,并确定您的输入是否超过计算机随机数。
string rock = (int)1;
string paper;
string scissors;
int input;
Random random = new Random();
int RandomNumber = random.Next(1,4);
bool i = true;
Console.WriteLine("-- Weapons Menu --");
Console.WriteLine("-------------------");
Console.WriteLine("1] Rock");
Console.WriteLine("2] Paper");
Console.WriteLine("3] Scissors");
Console.WriteLine("Choose Your Weapon [1, 2 or 3]:");
input = int.Parse(Console.ReadLine());
if (input == RandomNumber )
Console.WriteLine("You Tied The Computer This Round");
Console.WriteLine("Player Chose ")
任何帮助都将非常有用,谢谢。我尝试转换 int 但我不能。
- 使用
Int32.TryParse(Console.ReadLine(), out input);
将字符串转换为整数。 - 要将 Int 转换为字符串,您必须使用
input.ToString();
- 那么你可以使用
Dictionary<int, string>
来实现目标。
您的代码中的问题出在这一行:string rock = (int)1;
相反,你应该给 string rock = "1";
使用 Dictionary<int, string>
:
var choices = new Dictionary<int, string> { {1,"Rock"}, {2,"Paper"}, {3,"Scissors"} };
然后访问正确的元素:
Console.WriteLine("Player Chose {0}", choices[input]);
您需要对此进行一些验证,以确保您不会尝试访问不存在的元素,或者用户不会输入非数字值。
var random = new Random();
while (true)
{
int RandomNumber = random.Next(1, 4);
var userinput = Console.ReadLine();
if (Enumerable.Range(1, 3).Contains(Convert.ToInt16(userinput)))
if (userinput == RandomNumber.ToString())
Console.WriteLine("Tied");
else
Console.WriteLine("Check and see who won!");
else
Console.WriteLine("Try Again!");
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RockPaperScissors
{
class Program
{
static void Main(string[] args)
{
var choices = new Dictionary<int, string> { { 1, "Rock" }, { 2, "Paper" }, { 3, "Scissors" } };
int input;
var randomly = new Dictionary<int, string> { { 1, "Rock" }, { 2, "Paper" }, { 3, "Scissors" } };
Random random = new Random();
int RandomNumber = random.Next(1, 4);
int Ties;
int Wins;
int Losses;
do
{
//Counter
Wins = 0;
Losses = 0;
Ties = 0;
Console.WriteLine("-- Weapons Menu --");
Console.WriteLine("-------------------");
Console.WriteLine("1] Rock");
Console.WriteLine("2] Paper");
Console.WriteLine("3] Scissors");
Console.WriteLine("Choose Your Weapon [1, 2 or 3]:");
input = int.Parse(Console.ReadLine());
if (input == RandomNumber)
Console.WriteLine("You Tied The Computer");
Ties++;
//Winning
if (input == 1 && RandomNumber == 3)
Console.WriteLine("You Beat The Computer :)");
Wins++;
if (input == 2 && RandomNumber == 1)
Console.WriteLine("You Beat The Computer :)");
Wins++;
if (input == 3 && RandomNumber == 2)
Console.WriteLine("You Beat The Computer :)");
Wins++;
//Lossing
if (input == 1 && RandomNumber == 2)
Console.WriteLine("Sorry You Lost To The Computer :(");
Losses++;
if (input == 2 && RandomNumber == 3)
Console.WriteLine("You Beat The Computer :)");
Losses++;
if (input == 3 && RandomNumber == 1)
Console.WriteLine("You Beat The Computer :)");
Losses++;
Console.WriteLine(string.Format("Player Chose {0} : Computer Chose {1}", choices[input], randomly[RandomNumber]));
Console.WriteLine();
Console.WriteLine("<Press Any Key To Continue...>");
Console.WriteLine();
Console.WriteLine("Player Wins Computer Wins");
Console.WriteLine("---------- --------------");
Console.WriteLine(" {0} {1}", Wins, Losses);
Console.ReadKey();
}
while (input != 3) ;
Console.WriteLine("Thanks for playing!");
}enter code here
}
}
STILL CANNOT SHOW THE COUNTER WHY