本文介绍了我如何编码2组三个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 < pre> #region使用语句 使用系统; 使用 System.Collections.Generic; 使用 System.ComponentModel; 使用 System.Data; 使用 System.Drawing; 使用 System.Linq; 使用 System.Text; 使用 System.Windows.Forms; #endregion 命名空间 DiceGame { public partial class Form1:Form { #region声明 图片[] diceImages; int [] dice; int [] diceResults; 随机兰特; #endregion #region初始化 public Form1() { InitializeComponent(); } private void Form1_Load( object sender,EventArgs e) { diceImages = new 图片[ 7 ]; diceImages [ 0 ] = Properties.Resources.dice_blank; diceImages [ 1 ] = Properties.Resources.dice_1; diceImages [ 2 ] = Properties.Resources.dice_2; diceImages [ 3 ] = Properties.Resources.dice_3; diceImages [ 4 ] = Properties.Resources.dice_4; diceImages [ 5 ] = Properties.Resources.dice_5; diceImages [ 6 ] = Properties.Resources.dice_6; dice = new int [ 6 ] { 0 , 0 , 0 , 0 , 0 , 0 }; diceResults = new int [ 6 ] { 0 , 0 , 0 , 0 , 0 , 0 }; rand = new Random(); } #endregion #region私有方法 私有 void btn_rollDice_Click( object sender,EventArgs e) { RollDice(); GetResults(); ResetResults(); } private void RollDice() { for ( int i = 0 ; i < dice.Length; i ++) { dice [i] = rand.Next( 1 , 5 + 1 ); diceResults [dice [i]] ++; } lbl_dice1.Image = diceImages [dice [ 0 ]]; lbl_dice2.Image = diceImages [dice [ 1 ]]; lbl_dice3.Image = diceImages [dice [ 2 ]]; lbl_dice4.Image = diceImages [dice [ 3 ]]; lbl_dice5.Image = diceImages [dice [ 4 ]]; lbl_dice6.Image = diceImages [dice [ 5 ]]; } 私人 void GetResults() { bool fiveKind = false ,fourKind = false ,highStraight = false , fullHouse = false ,threeKind = false ,twoSets = false , twoPair = false ,onePair = false ,hasSix = false ,haveFive = false , haveFour = false ,haveThree = false ,haveTwo = false ,haveOne = false ; for ( int i = 0 ; i < diceResults.Length; i ++) { if (diceResults [i] == 5 ) fiveKind = true ; else if (diceResults [i] == 4 ) fourKind = true ; else if (diceResults [ 1 ] == 1 && diceResults [ 2 ] == 1 && diceResults [ 3 ] == 1 && diceResults [ 4 ] == 1 && diceResults [ 5 ] == 1 ) highStraight = true ; else if (diceResults [i] == 3 ) { threeKind = true ; for ( int j = 0 ; j < diceResults.Length; j ++) { if (diceResults [j] == 3 ) twoSets = true ; } } 其他 如果(diceResults [i] = = 2 ) { onePair = true ; for ( int j = i + 1 ; j < diceResults.Length; j ++) { if (diceResults [j] == 2 ) twoPair = true ; } } } for ( int i = 0 ; i < dice.Length; i ++) { switch (dice [i]) { case 6 : haveSix = true ; break ; case 5 : haveFive = 真; break ; case 4 : haveFour = 真; break ; case 3 : haveThree = 真; break ; case 2 : haveTwo = 真; break ; case 1 : haveOne = 真; break ; } } if (fiveKind) lbl_displayResults.Text = 五种; else if (fourKind) lbl_displayResults.Text = 四种; else if (highStraight) lbl_displayResults.Text = High Straight; else if (twoSets) lbl_displayResults.Text = 两套三; else if (fullHouse) lbl_displayResults.Text = Full House; else if (threeKind) lbl_displayResults.Text = 三种; else if (twoPair) lbl_displayResults.Text = 两对; else if (onePair) lbl_displayResults.Text = 一对; else if (hasSix) lbl_displayResults.Text = Six High; else if (haveFive) lbl_displayResults.Text = Five High; else if (haveFour) lbl_displayResults.Text = 四高; else if (haveThree) lbl_displayResults.Text = Three High; else if (haveTwo) lbl_displayResults.Text = Two High; else if (haveOne) lbl_displayResults.Text = One High; } 私有 void ResetResults() { for ( int i = 0 ; i < diceResults.Length; i ++) diceResults [i] = 0 ; } #endregion } } 我尝试了什么: 无数不同的循环和if语句。永远得到三个!似乎我不能让编译器理解有两组不同的三个。解决方案 这段代码过于复杂: switch (dice [i]) { case 1 : diceResults [ 0 ] ++; break ; case 2 : diceResults [ 1 ] ++; break ; case 3 : diceResults [ 2 ] ++; break ; case 4 : diceResults [ 3 ] ++; break ; case 5 : diceResults [ 4 ] ++; break ; case 6 : diceResults [ 5 ] ++; break ; } 可替换为 diceResults [dice [i]] ++ ; Quote:如何编写2组三个? 据我所知,您的代码不会尝试搜索2组3组或3组2。请试一试。 当你不明白你的代码在做什么或为什么它做它的作用时,答案是 debugger 。 使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。 调试器 - 维基百科,免费的百科全书 [ ^ ] 掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ] 使用Visual Studio 2010进行基本调试 - YouTube [ ^ ] 调试器在这里向您展示您的代码正在做什么以及您的任务是与它应该做的比较。 调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期效果时,你就接近了一个错误。 否则if(diceResults [i] == 3) { threeKind = true; for(int j = 0; j< diceResults.Length; j ++) { if(diceResults [j] == 3) twoSets = true; } } 打印3种,显然我有2套三! <pre>#region Using Statementsusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;#endregionnamespace DiceGame{ public partial class Form1 : Form { #region Declaration Image[] diceImages; int[] dice; int[] diceResults; Random rand; #endregion #region Initialization public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { diceImages = new Image[7]; diceImages[0] = Properties.Resources.dice_blank; diceImages[1] = Properties.Resources.dice_1; diceImages[2] = Properties.Resources.dice_2; diceImages[3] = Properties.Resources.dice_3; diceImages[4] = Properties.Resources.dice_4; diceImages[5] = Properties.Resources.dice_5; diceImages[6] = Properties.Resources.dice_6; dice = new int[6] {0, 0, 0, 0, 0, 0 }; diceResults = new int[6] { 0, 0, 0, 0, 0, 0 }; rand = new Random(); } #endregion #region Private Methods private void btn_rollDice_Click(object sender, EventArgs e) { RollDice(); GetResults(); ResetResults(); } private void RollDice() { for (int i = 0; i < dice.Length; i++) { dice[i] = rand.Next(1, 5 + 1); diceResults[dice[i]]++; } lbl_dice1.Image = diceImages[dice[0]]; lbl_dice2.Image = diceImages[dice[1]]; lbl_dice3.Image = diceImages[dice[2]]; lbl_dice4.Image = diceImages[dice[3]]; lbl_dice5.Image = diceImages[dice[4]]; lbl_dice6.Image = diceImages[dice[5]]; } private void GetResults() { bool fiveKind = false, fourKind = false, highStraight = false, fullHouse = false, threeKind = false, twoSets = false, twoPair = false, onePair = false, haveSix = false, haveFive = false, haveFour = false, haveThree = false, haveTwo = false, haveOne = false; for (int i = 0; i < diceResults.Length; i++) { if (diceResults[i] == 5) fiveKind = true; else if (diceResults[i] == 4) fourKind = true; else if (diceResults[1] == 1 && diceResults[2] == 1 && diceResults[3] == 1 && diceResults[4] == 1 && diceResults[5] == 1) highStraight = true; else if (diceResults[i] == 3) { threeKind = true; for (int j = 0; j < diceResults.Length; j++) { if (diceResults[j] == 3) twoSets = true; } } else if (diceResults[i] == 2) { onePair = true; for (int j = i + 1; j < diceResults.Length; j++) { if (diceResults[j] == 2) twoPair = true; } } } for (int i = 0; i < dice.Length; i++) { switch (dice[i]) { case 6: haveSix = true; break; case 5: haveFive = true; break; case 4: haveFour = true; break; case 3: haveThree = true; break; case 2: haveTwo = true; break; case 1: haveOne = true; break; } } if (fiveKind) lbl_displayResults.Text = "Five of a Kind"; else if (fourKind) lbl_displayResults.Text = "Four of a Kind"; else if (highStraight) lbl_displayResults.Text = "High Straight"; else if (twoSets) lbl_displayResults.Text = "Two Sets of Three"; else if (fullHouse) lbl_displayResults.Text = "Full House"; else if (threeKind) lbl_displayResults.Text = "Three of a Kind"; else if (twoPair) lbl_displayResults.Text = "Two Pair"; else if (onePair) lbl_displayResults.Text = "One Pair"; else if (haveSix) lbl_displayResults.Text = "Six High"; else if (haveFive) lbl_displayResults.Text = "Five High"; else if (haveFour) lbl_displayResults.Text = "Four High"; else if (haveThree) lbl_displayResults.Text = "Three High"; else if (haveTwo) lbl_displayResults.Text = "Two High"; else if (haveOne) lbl_displayResults.Text = "One High"; } private void ResetResults() { for (int i = 0; i < diceResults.Length; i++) diceResults[i] = 0; } #endregion }}What I have tried:countless different loops and if statements. always getting Three of a kind! It seems the I cant get the compiler to understand there is 2 different sets of three. 解决方案 This code is overly complicated:switch (dice[i]){ case 1: diceResults[0]++; break; case 2: diceResults[1]++; break; case 3: diceResults[2]++; break; case 4: diceResults[3]++; break; case 5: diceResults[4]++; break; case 6: diceResults[5]++; break;}and can be replaced withdiceResults[dice[i]]++;Quote:How do I code 2 sets of three ?As I understand, your code makes no attempt to search for 2 sets of 3, or 3 sets of 2 either. Please show an attempt.When you don't understand what your code is doing or why it does what it does, the answer is debugger.Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.Debugger - Wikipedia, the free encyclopedia[^]Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]Basic Debugging with Visual Studio 2010 - YouTube[^]The debugger is here to show you what your code is doing and your task is to compare with what it should do.There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.else if (diceResults[i] == 3) { threeKind = true; for (int j = 0; j < diceResults.Length; j++) { if (diceResults[j] == 3) twoSets = true; } }prints 3 of a kind , when clearly I have 2 sets of three! 这篇关于我如何编码2组三个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-27 16:35