所以我一直在尝试骰子的随机化,但似乎有一个问题。所以我的问题基本上是我做错了什么?代码如下。在设计页面上,我有2个图片框,每个图片框有6个骰子面。 我有2个标签显示你得到的骰子数量。我在图片框下方还有2个标签,表示胜利。我在中间还有一个标签,如果它是平局,就说领带。帮助将不胜感激!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Practice_randomizing
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Random Rand = new Random();
int Random = Rand.Next(6);
int player1;
player1 = 0;
int player2;
player2 = 0;
if
(Random == 0)
{
pictureBox1.Image = Properties.Resources.dice_11;
label1.Text = "You got a 1!";
pictureBox1.Visible = true;
}
else
if
(Random == 1)
{
pictureBox1.Image = Properties.Resources.dice_21;
label1.Text = "You got a 2!";
pictureBox1.Visible = true;
}
else
if
(Random == 2)
{
pictureBox1.Image = Properties.Resources.dice_31;
label1.Text = "You got a 3!";
pictureBox1.Visible = true;
}
else
if
(Random == 3)
{
pictureBox1.Image = Properties.Resources.dice_41;
label1.Text = "You got a 4!";
pictureBox1.Visible = true;
}
else
if
(Random == 4)
{
pictureBox1.Image = Properties.Resources.dice_51;
label1.Text = "You got a 5!";
pictureBox1.Visible = true;
}
else
{
pictureBox1.Image = Properties.Resources.dice_61;
label1.Text = "You got a 6!";
pictureBox1.Visible = true;
{
int dice2 = Rand.Next(6);
if
(dice2 == 0)
{
pictureBox2.Image = Properties.Resources.dice_11;
label5.Text = "You got a 1!";
pictureBox2.Visible = true;
}
else
if
(dice2 == 1)
{
pictureBox2.Image = Properties.Resources.dice_21;
label5.Text = "You got a 2!";
pictureBox2.Visible = true;
}
else
if
(dice2 == 2)
{
pictureBox2.Image = Properties.Resources.dice_31;
label5.Text = "You got a 3!";
pictureBox2.Visible = true;
}
else
if
(dice2 == 3)
{
pictureBox2.Image = Properties.Resources.dice_41;
label5.Text = "You got a 4!";
pictureBox2.Visible = true;
}
else
if
(dice2 == 4)
{
pictureBox2.Image = Properties.Resources.dice_51;
label5.Text = "You got a 5!";
pictureBox2.Visible = true;
}
else
{
pictureBox2.Image = Properties.Resources.dice_61;
label5.Text = "You got a 6!";
pictureBox2.Visible = true;
if
(Random == 0 && dice2 == 0)
label4.Text = "Tie!";
if
(Random == 0 && dice2 == 0)
label4.Text = "Tie!";
else
if
(Random == 1 && dice2 == 1)
label4.Text = "Tie!";
else
if
(Random == 2 && dice2 == 2)
label4.Text = "Tie!";
else
if
(Random == 3 && dice2 == 3)
label4.Text = "Tie!";
else
if
(Random == 4 && dice2 == 4)
label4.Text = "Tie!";
else
if
(Random == 5 && dice2 == 5)
label4.Text = "Tie!";
else
if
(Random == 1 && dice2 == 0)
{
label2.Text = "Win!";
player1++;
label4.Text = " " + player1;
}
else
if
(Random == 1 && dice2 == 2)
label3.Text = "win!";
else
if
(Random == 1 && dice2 == 3)
label3.Text = "win!";
else
if
(Random == 1 && dice2 == 4)
label3.Text = "win!";
else
if
(Random == 1 && dice2 == 5)
{ label2.Text = "win!";
player1++;
label6.Text = " " + player1;
}
else
if
(Random == 2 && dice2 == 0)
label2.Text = "Win!";
else
if
(Random == 2 && dice2 == 1)
label2.Text = "win!";
else
if
(Random == 2 && dice2 == 3)
label3.Text = "win!";
else
if
(Random == 2 && dice2 == 4)
label2.Text = "win!";
else
if
(Random == 2 && dice2 == 5)
label2.Text = "win!";
else
if
(Random == 3 && dice2 == 0)
label2.Text = "win!";
else
if
(Random == 3 && dice2 == 1)
label2.Text = "win!";
else
if
(Random == 3 && dice2 == 2)
label2.Text = "win!";
else
if
(Random == 3 && dice2 == 4)
label3.Text = "win!";
else
if
(Random == 3 && dice2 == 5)
label3.Text = "win!";
else
if
(Random == 4 && dice2 == 0)
label2.Text = "win!";
else
if
(Random == 4 && dice2 == 1)
label2.Text = "win!";
else
if
(Random == 4 && dice2 == 2)
label2.Text = "win!";
else
if
(Random == 4 && dice2 == 3)
label2.Text = "win!";
else
if
(Random == 4 && dice2 == 5)
label3.Text = "win!";
else
if
(Random == 5 && dice2 == 0)
label2.Text = "win!";
else
if
(Random == 5 && dice2 == 1)
label2.Text = "win!";
else
if
(Random == 5 && dice2 == 2)
label2.Text = "win!";
else
if
(Random == 5 && dice2 == 3)
label2.Text = "win!";
else
if
(Random == 5 && dice2 == 4)
label2.Text = "win!";
label2.Text = " ";
label3.Text = " ";
label4.Text = " ";
}
}
}
}
}
}
答案 0 :(得分:1)
你还没有关闭if ... else
语句的链,所以选择第二个骰子的代码在第一个骰子是6的代码中,并且用于检查获胜者的代码在代码中当第二个骰子是6时。
使用}
关闭else
这两个if ... else
语句链的最后{{1}}个,以便始终运行以下代码。
答案 1 :(得分:0)
唯一的问题似乎是IF ... ELSE和ELSE IF。当你编写了else代码时,你给了一个换行符,因此它变成了
else
if
(condition)
根据我的理解是另一个条件,其中有另一个If。因此你的程序似乎是错误的。请仅在需要时提供新行,并尝试遵循该语言的语法。希望下面的代码可以帮助你
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Practice_randomizing
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Random Rand = new Random();
int Random = Rand.Next(6);
int player1;
player1 = 0;
int player2;
player2 = 0;
if
(Random == 0)
{
pictureBox1.Image = Properties.Resources.dice_11;
label1.Text = "You got a 1!";
pictureBox1.Visible = true;
}
else if(Random == 1)
{
pictureBox1.Image = Properties.Resources.dice_21;
label1.Text = "You got a 2!";
pictureBox1.Visible = true;
}
else if(Random == 2)
{
pictureBox1.Image = Properties.Resources.dice_31;
label1.Text = "You got a 3!";
pictureBox1.Visible = true;
}
else if(Random == 3)
{
pictureBox1.Image = Properties.Resources.dice_41;
label1.Text = "You got a 4!";
pictureBox1.Visible = true;
}
else if(Random == 4)
{
pictureBox1.Image = Properties.Resources.dice_51;
label1.Text = "You got a 5!";
pictureBox1.Visible = true;
}
else
{
pictureBox1.Image = Properties.Resources.dice_61;
label1.Text = "You got a 6!";
pictureBox1.Visible = true;
{
int dice2 = Rand.Next(6);
if(dice2 == 0)
{
pictureBox2.Image = Properties.Resources.dice_11;
label5.Text = "You got a 1!";
pictureBox2.Visible = true;
}
else if(dice2 == 1)
{
pictureBox2.Image = Properties.Resources.dice_21;
label5.Text = "You got a 2!";
pictureBox2.Visible = true;
}
else if(dice2 == 2)
{
pictureBox2.Image = Properties.Resources.dice_31;
label5.Text = "You got a 3!";
pictureBox2.Visible = true;
}
else if(dice2 == 3)
{
pictureBox2.Image = Properties.Resources.dice_41;
label5.Text = "You got a 4!";
pictureBox2.Visible = true;
}
else if(dice2 == 4)
{
pictureBox2.Image = Properties.Resources.dice_51;
label5.Text = "You got a 5!";
pictureBox2.Visible = true;
}
else
{
pictureBox2.Image = Properties.Resources.dice_61;
label5.Text = "You got a 6!";
pictureBox2.Visible = true;
if(Random == 0 && dice2 == 0)
label4.Text = "Tie!";
if(Random == 0 && dice2 == 0)
label4.Text = "Tie!";
else if(Random == 1 && dice2 == 1)
label4.Text = "Tie!";
else if(Random == 2 && dice2 == 2)
label4.Text = "Tie!";
else if(Random == 3 && dice2 == 3)
label4.Text = "Tie!";
else if(Random == 4 && dice2 == 4)
label4.Text = "Tie!";
else if(Random == 5 && dice2 == 5)
label4.Text = "Tie!";
else if(Random == 1 && dice2 == 0)
{
label2.Text = "Win!";
player1++;
label4.Text = " " + player1;
}
else if(Random == 1 && dice2 == 2)
label3.Text = "win!";
else if(Random == 1 && dice2 == 3)
label3.Text = "win!";
else if(Random == 1 && dice2 == 4)
label3.Text = "win!";
else if(Random == 1 && dice2 == 5)
{
label2.Text = "win!";
player1++;
label6.Text = " " + player1;
}
else if(Random == 2 && dice2 == 0)
label2.Text = "Win!";
else if(Random == 2 && dice2 == 1)
label2.Text = "win!";
else if(Random == 2 && dice2 == 3)
label3.Text = "win!";
else if(Random == 2 && dice2 == 4)
label2.Text = "win!";
else if(Random == 2 && dice2 == 5)
label2.Text = "win!";
else if(Random == 3 && dice2 == 0)
label2.Text = "win!";
else if(Random == 3 && dice2 == 1)
label2.Text = "win!";
else if(Random == 3 && dice2 == 2)
label2.Text = "win!";
else if(Random == 3 && dice2 == 4)
label3.Text = "win!";
else if(Random == 3 && dice2 == 5)
label3.Text = "win!";
else if(Random == 4 && dice2 == 0)
label2.Text = "win!";
else if(Random == 4 && dice2 == 1)
label2.Text = "win!";
else if(Random == 4 && dice2 == 2)
label2.Text = "win!";
else if(Random == 4 && dice2 == 3)
label2.Text = "win!";
else if(Random == 4 && dice2 == 5)
label3.Text = "win!";
else if(Random == 5 && dice2 == 0)
label2.Text = "win!";
else if(Random == 5 && dice2 == 1)
label2.Text = "win!";
else if(Random == 5 && dice2 == 2)
label2.Text = "win!";
else if(Random == 5 && dice2 == 3)
label2.Text = "win!";
else if(Random == 5 && dice2 == 4)
label2.Text = "win!";
label2.Text = " ";
label3.Text = " ";
label4.Text = " ";
}
}
}
}
}
}
答案 2 :(得分:0)
您还需要将Random的实例移出到Class级别,以便在每次单击Button时使用相同的实例:
private Random Rand = new Random();
private void button1_Click(object sender, EventArgs e)
{
// ... rest of your code ...
}