C#编写了一个猜拳游戏,为什么总是平局```不比较?
Playerp1=newPlayer();Playerp2=newPlayer();inti,j,counter1=0,counter2=0;charc,d;public...
Player p1 = new Player();
Player p2 = new Player();
int i, j, counter1 = 0, counter2 = 0;
char c,d;
public void Man_Com(string s1,string s2)
{
while(c!='n'&& c!='N')
{
p1.PlayedByMan();
p2.PlayedByComputer();
if ((i==1&&j==2)||(i==2&&j==3)||(i==3&&j==1))
{
Console.WriteLine("{0} says : I win!!!",s1);
counter1++;
}
else if ((i==1&&j==3)||(i==2&&j==1)||(i==3&&j==2))
{
Console.WriteLine("{0} says : I lose!!!", s1);
}
else
{
Console.WriteLine("{0} says : Tie……", s1);
}
Console.WriteLine("Do you wanna try again?(Y/N)");
c = Convert.ToChar(Console.ReadLine());
}
Console.WriteLine("You win {0} times!!!", counter1);
}
"1-石头,2-剪刀,3-布"
public void PlayedByMan()
{
int i;
Console.Write("请出拳:");
i = Convert.ToInt32(Console.ReadLine());
}
public void PlayedByComputer()
{
int j;
Random r = new Random();
j = r.Next(1, 4);
}
怎么把i,j传过来呢?````我这个写在了player类里面~`` 展开
Player p2 = new Player();
int i, j, counter1 = 0, counter2 = 0;
char c,d;
public void Man_Com(string s1,string s2)
{
while(c!='n'&& c!='N')
{
p1.PlayedByMan();
p2.PlayedByComputer();
if ((i==1&&j==2)||(i==2&&j==3)||(i==3&&j==1))
{
Console.WriteLine("{0} says : I win!!!",s1);
counter1++;
}
else if ((i==1&&j==3)||(i==2&&j==1)||(i==3&&j==2))
{
Console.WriteLine("{0} says : I lose!!!", s1);
}
else
{
Console.WriteLine("{0} says : Tie……", s1);
}
Console.WriteLine("Do you wanna try again?(Y/N)");
c = Convert.ToChar(Console.ReadLine());
}
Console.WriteLine("You win {0} times!!!", counter1);
}
"1-石头,2-剪刀,3-布"
public void PlayedByMan()
{
int i;
Console.Write("请出拳:");
i = Convert.ToInt32(Console.ReadLine());
}
public void PlayedByComputer()
{
int j;
Random r = new Random();
j = r.Next(1, 4);
}
怎么把i,j传过来呢?````我这个写在了player类里面~`` 展开
展开全部
i,j的值在哪,没有对他们赋值,每次比较的时候都不符合if ((i==1&&j==2)||(i==2&&j==3)||(i==3&&j==1))
{
Console.WriteLine("{0} says : I win!!!",s1);
counter1++;
}
else if ((i==1&&j==3)||(i==2&&j==1)||(i==3&&j==2))
{
Console.WriteLine("{0} says : I lose!!!", s1);
}
当然会最后Console.WriteLine("{0} says : Tie……", s1);
。
你可以在PlayedByMan(); PlayedByComputer();加上对i,j赋值,并注意变量的作用域,不然在外边得到的i和j都是空值。看你程序的样子,估计应该是变量作用域的问题。
{
Console.WriteLine("{0} says : I win!!!",s1);
counter1++;
}
else if ((i==1&&j==3)||(i==2&&j==1)||(i==3&&j==2))
{
Console.WriteLine("{0} says : I lose!!!", s1);
}
当然会最后Console.WriteLine("{0} says : Tie……", s1);
。
你可以在PlayedByMan(); PlayedByComputer();加上对i,j赋值,并注意变量的作用域,不然在外边得到的i和j都是空值。看你程序的样子,估计应该是变量作用域的问题。
展开全部
using System;
namespace W23
{
class begin1 //定义一个begin1的类;//
{
public static void Main() //主函数//
{
int rock1=3; //rock代表石头//
int jian1=2; //jian 代表剪子//
int bu1=1; //bu代表布//
System.Random rnd=new System.Random();
string aj=Console.ReadLine();
int nbr=(int) rnd.Next(1,3); //随机数//
string rnd1=Convert.ToString(nbr); //将rnd的int转化为string//
string rock=Convert.ToString(rock1); //同上//
string jian=Convert.ToString(jian1); //同上//
string bu=Convert.ToString(bu1);
if ((rnd1==rock)&&(aj==bu)||(rnd1==jian)&&(aj==rock)||(rnd1==bu)&&(aj==jian))
{
Console.WriteLine("你胜利了!");
}
if ((rnd1==rock)&&(aj==jian)||(rnd1==jian)&&(aj==bu)||(rnd1==bu)&&(aj==rock))
{
Console.WriteLine("我出局了!");
}
if ((rnd1==rock)&&(aj==rock)||(rnd1==jian)&&(aj==jian)||(rnd1==bu)&&(aj==bu))
{
Console.WriteLine("我的我是平手!");
}
}
}
}
我重新做了下。。
namespace W23
{
class begin1 //定义一个begin1的类;//
{
public static void Main() //主函数//
{
int rock1=3; //rock代表石头//
int jian1=2; //jian 代表剪子//
int bu1=1; //bu代表布//
System.Random rnd=new System.Random();
string aj=Console.ReadLine();
int nbr=(int) rnd.Next(1,3); //随机数//
string rnd1=Convert.ToString(nbr); //将rnd的int转化为string//
string rock=Convert.ToString(rock1); //同上//
string jian=Convert.ToString(jian1); //同上//
string bu=Convert.ToString(bu1);
if ((rnd1==rock)&&(aj==bu)||(rnd1==jian)&&(aj==rock)||(rnd1==bu)&&(aj==jian))
{
Console.WriteLine("你胜利了!");
}
if ((rnd1==rock)&&(aj==jian)||(rnd1==jian)&&(aj==bu)||(rnd1==bu)&&(aj==rock))
{
Console.WriteLine("我出局了!");
}
if ((rnd1==rock)&&(aj==rock)||(rnd1==jian)&&(aj==jian)||(rnd1==bu)&&(aj==bu))
{
Console.WriteLine("我的我是平手!");
}
}
}
}
我重新做了下。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询