如何解决此代码中的错误 [英] how can is solve the error in this code

查看:95
本文介绍了如何解决此代码中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Drawing;
using System.WinForms;
using System.Collections;
namespace ShuffleGame
{
	public class Shuffle:Form
	{
		private Label lbl1= new Label();
		private Label lbl2= new Label();
		private Label lbl3= new Label();
		private Label lbl4= new Label();
		private Label lbl5= new Label();
		private Label lbl6= new Label();
		private Label lbl7= new Label();
		private Label lbl8= new Label();
		private Label lblBlank= new Label();
		private int[,] pos= {{0,0,0},{0,0,0},{0,0,0}};
		private int ar,ac;
		private int Hits;
		private Label lblHits = new Label();
		
		private Hashtable value = new Hashtable();
		
		
		public static int Main(string [] args)
		{
			Application.Run(new Shuffle());
			return 0;
		}
		
		public Shuffle()
		{
			ar=2;
			ac=2;
			Hits=0;
			
			this.Text="Shuffle Game";
			this.BackColor=Color.RoyalBlue;
			this.Size=new Size(310,370);
			
			lbl1.Text="1";
			//lbl1.Location=new Point(0,0);
			lbl1.BackColor=Color.LightGray;
			lbl1.Size= new Size(90,90);
			lbl1.Font=new Font("Times New Roman",14);
			
			
			lbl2.Text="2";
			//lbl2.Location=new Point(100,0);
			lbl2.BackColor=Color.LightGray;
			lbl2.Size= new Size(90,90);
			lbl2.Font=new Font("Times New Roman",14);
			
			
			lbl3.Text="3";
			//lbl3.Location=new Point(200,0);
			lbl3.BackColor=Color.LightGray;
			lbl3.Size= new Size(90,90);
			lbl3.Font=new Font("Times New Roman",14);
			
			lbl4.Text="4";
			//lbl4.Location=new Point(0,100);
			lbl4.BackColor=Color.LightGray;
			lbl4.Size= new Size(90,90);
			lbl4.Font=new Font("Times New Roman",14);
			
			lbl5.Text="5";
			//lbl5.Location=new Point(100,100);
			lbl5.BackColor=Color.LightGray;
			lbl5.Size= new Size(90,90);
			lbl5.Font=new Font("Times New Roman",14);
			
			lbl6.Text="6";
			//lbl6.Location=new Point(200,100);
			lbl6.BackColor=Color.LightGray;
			lbl6.Size= new Size(90,90);
			lbl6.Font=new Font("Times New Roman",14);
			
			lbl7.Text="7";
			//lbl7.Location=new Point(0,200);
			lbl7.BackColor=Color.LightGray;
			lbl7.Size= new Size(90,90);
			lbl7.Font=new Font("Times New Roman",14);
			
			lbl8.Text="8";
			//lbl8.Location=new Point(100,200);
			lbl8.BackColor=Color.LightGray;
			lbl8.Size= new Size(90,90);
			lbl8.Font=new Font("Times New Roman",14);
			
			lblBlank.Location=new Point(200,200);
			lblBlank.Size=new Size(90,90);
			PlaceRandom();
			
			lblHits.Location=new Point(0,310);
			lblHits.Text="Hits so far ---> " +Hits.ToString();
			lblHits.BackColor=Color.LightGray;
			lblHits.Size=new Size(350,40);
			
			this.Controls.Add(lbl1);
			this.Controls.Add(lbl2);
			this.Controls.Add(lbl3);
			this.Controls.Add(lbl4);
			this.Controls.Add(lbl5);
			this.Controls.Add(lbl6);
			this.Controls.Add(lbl7);
			this.Controls.Add(lbl8);
			this.Controls.Add(lblHits);
			this.Controls.Add(lblBlank);
			this.MouseDown += new MouseEventHandler(Win_Click);
			this.KeyDown+=new KeyEventHandler(Win_KeyDown);
		}
		
		private void Win_Click(Object sender, MouseEventArgs e)
		{
			Random rnd=new Random();
			
			MessageBox.Show(rnd.Next(8).ToString());
			MessageBox.Show("Hello");
		}
	
		private void Win_KeyDown(Object sender, KeyEventArgs k)
		{
			int temp;
			switch(k.KeyCode.ToInt32())
			{
				case 38:
					//"Up"
					if(ar<2)
					{
						temp=pos[ar,ac];
						pos[ar,ac]=pos[ar+1,ac];
						pos[ar+1,ac]=temp;
						Swap(pos[ar,ac],temp);
						ar++;
						Hits++;
					}
					
					break;
				case 37:
					//"Left"
					if(ac<2)
					{
						temp=pos[ar,ac];
						pos[ar,ac]=pos[ar,ac+1];
						pos[ar,ac+1]=temp;
						Swap(pos[ar,ac],temp);
						ac++;
						Hits++;
					}
					break;
				case 39:
					//"Right"
					if(ac>0)
					{
						temp=pos[ar,ac];
						pos[ar,ac]=pos[ar,ac-1];
						pos[ar,ac-1]=temp;
						Swap(pos[ar,ac],temp);
						ac--;
						Hits++;
					}
					break;
				case 40:
					//"Down"
					if(ar>0)
					{
						temp=pos[ar,ac];
						pos[ar,ac]=pos[ar-1,ac];
						pos[ar-1,ac]=temp;
						Swap(pos[ar,ac],temp);
						ar--;
						Hits++;
					}
					break;
			}	
			lblHits.Text="Hits so far ---> " +Hits.ToString();
			Boolean same=true;
			String arrEle="";
			for(int ctr=1,i=0;i<3;i++)
			{
				for(int j=0;j<3;j++,ctr++)
				{
					/*if(j==2 && i==2)
					{
						if(pos[i,j]==0)
							same=true;
							break;
							
					}
					else if(pos[i,j]!=ctr)
					{
						same=false;
						break;
						
					}*/
					if(pos[i,j]!=ctr)
					{
						same=false;
						break;
					}
					arrEle=arrEle+"\t"+pos[i,j];
					
				}
				arrEle=arrEle+"\n";
			}
				
				if(same)
				{
					MessageBox.Show("You have finished the game in " + Hits.ToString() + " hits","End of Game",2);
				}
		}
		
		private void Swap(int vNum, int blank)
		{
			Point temp;
	
			switch(vNum)
			{
				case 1:
					temp=lbl1.Location;	
					lbl1.Location=lblBlank.Location;
					lblBlank.Location=temp;
					break;
				case 2:
					temp=lbl2.Location;	
					lbl2.Location=lblBlank.Location;
					lblBlank.Location=temp;
					break;
				case 3:
					temp=lbl3.Location;	
					lbl3.Location=lblBlank.Location;
					lblBlank.Location=temp;
					break;
				case 4:
					temp=lbl4.Location;	
					lbl4.Location=lblBlank.Location;
					lblBlank.Location=temp;
					break;
				case 5:
					temp=lbl5.Location;	
					lbl5.Location=lblBlank.Location;
					lblBlank.Location=temp;
					break;
				case 6:
					temp=lbl6.Location;	
					lbl6.Location=lblBlank.Location;
					lblBlank.Location=temp;
					break;
				case 7:
					temp=lbl7.Location;	
					lbl7.Location=lblBlank.Location;
					lblBlank.Location=temp;
					break;
				case 8:
					temp=lbl8.Location;	
					lbl8.Location=lblBlank.Location;
					lblBlank.Location=temp;
					break;
			}
		}
		
		private void PlaceRandom()
		{
			int r,c;
			r=10;c=10;
			int i=0;
			ar=0;
			ac=0;
			Random rnd= new Random();
			int val;
			while(i<8)
			{	
				val=rnd.Next(9).ToInt32();
				if(numNotExists(val)==true && val>0)
				{
					pos[ar,ac]=val;
					
					switch(val)
					{
						case 1:
							lbl1.Location=new Point(c,r);
							
							break;
						case 2:
							lbl2.Location=new Point(c,r);
							
							break;
						case 3:
							lbl3.Location=new Point(c,r);
							
							break;
						case 4:
							lbl4.Location=new Point(c,r);
							
							break;
						case 5:
							lbl5.Location=new Point(c,r);
							
							break;
						case 6:
							lbl6.Location=new Point(c,r);
							
							break;
						case 7:
							lbl7.Location=new Point(c,r);
							
							break;
						case 8:
							lbl8.Location=new Point(c,r);
							
							break;
						default:
							
					}
					c+=100;
					ac++;
					if(ac>2)
					{
						ac=0;
						ar++;
					}	
					if(c>300)
					{
						c=10;
						r+=100;
					}
					i++;
				}
				else
					continue;
			}
			lblBlank.Location=new Point(c,r);
			pos[2,2]=9;
		}
		
		private Boolean numNotExists(int num)
		{
			for(int i=0;i<3;i++)
				for(int j=0;j<3;j++)
				{
					
					if(pos[i,j]==num)
						return false;
				}	
			
			return true;
		}
		
		
		
	}
}





它显示12-15错误显示你错过了使用指令或汇编引用

我使用visual studio cmd因为我无法在visual studio中调试此代码可以任何人告诉为什么???



its showing 12-15 errors its showing are u missing a using directive or an assembly reference
im using visual studio cmd because i cannot debug this code in visual studio can any one tell why???

推荐答案

而不是
using System.WinForms;

产生错误

Quote:

类型或命名空间名称'WinForms'在命名空间'System'中不存在(你是否遗漏了)汇编参考?)

The type or namespace name 'WinForms' does not exist in the namespace 'System' (are you missing an assembly reference?)



使用正确的版本


Use the correct version

using System.Windows.Forms;





这会让你只能解决4个编译错误。



无法调试的原因是因为代码无法编译(因此不会运行,因此无法调试)



- 继续使用sol ving你的代码问题......



现在报告的4个错误中的第一个是



That will get you down to only 4 compile errors.

The reason you can't debug is because the code won't compile (and therefore won't run and therefore cannot be debugged)

- to carry on with solving the problems with your code...

The first of the now 4 errors reported is

Quote:

'System.Windows.Forms.Keys'不包含'ToInt32'的定义,也没有扩展方法'ToInt32'接受类型为'System.Windows.Forms.Keys'的第一个参数'(你错过了使用指令或程序集引用吗?)

'System.Windows.Forms.Keys' does not contain a definition for 'ToInt32' and no extension method 'ToInt32' accepting a first argument of type 'System.Windows.Forms.Keys' could be found (are you missing a using directive or an assembly reference?)

总是首先解决列表中的第一个错误 - 你经常发现后续错误会改变或完全消失。



双击错误,它将带你到行

ALWAYS address the first error in the list first - you often find that the subsequent errors change or go away entirely.

Double-click on the error and it will take you to the line

switch (k.KeyCode.ToInt32())

首先你可能认为你需要放入一个using指令,但是解决这个问题真的很简单 - 错误信息已经告诉你这行代码究竟出了什么问题... System.Windows.Forms中没有这样的属性或方法.ToInt32 .Keys - 所以删掉那个lnie。



但是当你删除 .ToInt32()时,你现在得到7个错误而不是预期的3个!!这并不意味着您通过修复第一个错误而破坏了任何东西!转到显示第一个错误的行

At first you might think that you need to put in a using directive, but fixing this is really a no-brainer - the error message has told you exactly what is wrong with this line of code... there is no such property or method .ToInt32 in System.Windows.Forms.Keys - so remove that bit of the lnie.

But when you remove the .ToInt32() you now get 7 errors instead of the expected 3!! This does not mean that you have broken anything by fixing that first error! Go to the line showing the first error

Quote:

无法将类型'int'隐式转换为'System.Windows.Forms.Keys'。存在显式转换(您是否缺少演员表?)

Cannot implicitly convert type 'int' to 'System.Windows.Forms.Keys'. An explicit conversion exists (are you missing a cast?)

case 38:
     //"Up"

可以在这里放一个明确的演员,例如

You could put an explicit cast in here e.g.

case (Keys)38:
    //"Up"

但是你认为这看起来好不好?

but don't you think this looks much better?

case Keys.Up:

使用正确的 Keys 值替换该switch语句中的所有错误,您将得到3个错误。



按照与上面相同的方法查找导致问题的行并仔细阅读错误消息。有了这个错误,你做了类似我们的键问题 - 避免使用魔术数字,使用正确的值...在这种情况下尝试键入行 - 不要复制和粘贴。在您键入时,您会注意到IDE为您提供了提示和帮助。这里有一个解决问题的提示

Replace all the errors in that switch statement with the correct Keys values and you will get down to 3 errors.

Follow the same approach as above to find the line causing the problem and read the error message carefully. With this error you have done something similar to our Keys problem - avoid using "magic" numbers, use the proper values ... in this case try to type the line in - don't copy and paste. As you type you will notice that the IDE gives you hints and help. Here's a hint for fixing the problem

Quote:

2 = AbortRetryIgnore如果使用正确的类型

2 = AbortRetryIgnore if you use the correct type





这会让你留下一个错误......现在你知道如何处理那个。



That will leave you with one error left ... and you now know how to deal with that one.


这篇关于如何解决此代码中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆