}期望的问题plz帮助 [英] } Expected problem plz help

查看:116
本文介绍了}期望的问题plz帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
namespace FirstConsoleProject
{
	class MainClass
	{
		public static void Main(string[] args)
		{
			Boolean turn = true;	
			Random ran = new Random ();
			int stone = ran.Next(15,31);
			string result;
			Console.WriteLine (stone);
			while (stone > 1) {
				if (turn) {
					PlayerTurn (out stone);
				} 
				else {
					ComputerTurn(out stone);
				}
				SwitchTurn(turn);
			}
			if (stone==0)
				result = (turn) ? "Player win!" : "Compuer win";
			else result = (turn) ? "Computer win!" : "Player win";

			Console.ReadLine ();
		}

		public static void SwitchTurn(out Boolean nowTurn) 
		{
			nowTurn = !nowTurn;
		}

		public static void PlayerTurn(out int stoneLeft)
		{
			private int num;
			Console.WriteLine("Player Turn");
			do{
				Console.Write ("Input the number of stones you want to remove: ");
				num = Convert.ToInt32 (Console.ReadLine ());
				if (num > stoneLeft)
					Console.WriteLine("Invalid input. Please re-enter.");
				else
					stoneLeft -= num;
			}
			while (num > stoneLeft); 
			Console.WriteLine ("Stones left = " + stoneLeft + "\n");
		}

		public static void ComputerTurn(out int stoneLeft)
		{
			private	int num;
			Console.WriteLine ("ComputerTurn");
			Random ran = new Random ();
			do{
				Console.Write ("Input the number of stones you want to remove: ");
				num = ran.Next(1,4);
				if (num > stoneLeft)
					Console.WriteLine("Invalid input. Please re-enter.");
				else
					stoneLeft -= num;
			}
			while (num > stoneLeft); 
			Console.WriteLine ("Stones left = " + stoneLeft + "\n");

		}
	}
}





这是我的代码,我一直收到PlayerTurn和ComputerTurn方法中的这些错误如下。有人可以帮我弄清楚什么是错的。我是C#的新手。谢谢!

}预期

无效令牌'>'

无效令牌'='

类型或命名空间定义,或预期文件结束



我尝试过:



只是找不到任何多余或缺失的开/关毯子plz帮助......我知道它有点长但是



This is my code and I keep receiving these error below in both "PlayerTurn" and "ComputerTurn" Method. Can someone help me figure out what's wrong. I am very new to C#. Thank you!
"} expected"
"invalid token '>'"
"invalid token '='"
"type or namespace definition, or end of file expected"

What I have tried:

just can't find any excess or missing open/close blanket plz help...i know it's a little bit long though

推荐答案

嗯 - 这是一个奇怪的,但它不是导致错误的括号 - 它是你在函数体内声明私有变量,而syustem试图找到一种礼貌的方式来告诉你这是错误的! />
更改这些:

Um - it's an odd one, but it's not the brackets that cause the error - it's that you are declaring private variables inside a function body, and the syustem is trying to find a polite way to tell you that's wrong!
Change these:
public static void PlayerTurn(out int stoneLeft)
{
    private int num;
    ...
}

public static void ComputerTurn(out int stoneLeft)
{
    private int num;
    ...
}



对于这些:


To these:

public static void PlayerTurn(out int stoneLeft)
{
    int num;
    ...
}

public static void ComputerTurn(out int stoneLeft)
{
    int num;
    ...
}

这些问题应该消失。

我强烈建议您在开始时不要使用1TB - 它使得很难看到代码块的开始或结束位置。最初习惯于K& R:

And those problems should go away.
And I'd strongly recommend against using 1TB while you are starting out - it makes it very difficult to see where code blocks start or end. Initially get used to K&R:

public void MyMethod()
{
    statments;
    if (condition)
    {
        statements;
    }
    else
    {
        statements;
    }
}

或更好(在我看来)Whitesmiths:

Or better (in my opinion) Whitesmiths:

public void MyMethod()
    {
    statments;
    if (condition)
        {
        statements;
        }
    else
        {
        statements;
        }
    }



它们让你更容易看出出了什么问题!


They make it a lot easier to see what is going wrong!


有很多东西都错了。让我们一步一步地完成它..



1)两者都在 PlayerTurn(..) ComputerTurn(..)

There's a lot of stuff wrong with this. Let's go through it step by step..

1) Both in PlayerTurn(..) and ComputerTurn(..) remove the private modifier from
private int num;



访问修饰符make对方法的本地标识符毫无意义,无论如何它们在方法之外是不可见的。



2)在 SwitchTurn(..)根据当前值修改变量 nowTurn 。因此,您不仅希望将其传递出方法,还需要将当前值传递给方法。所以你需要一个 ref modifer而不是 out 修饰符:


Access modifiers make no sense for local identifiers of a method, they're not visible outside the method anyway.

2) In SwitchTurn(..) you modify the variable nowTurn based on its current value. So you don't just want to pass it out of the method, you also want to pass the current value into the method. So you need a ref modifer instead of the out modifier:

public static void SwitchTurn(ref Boolean nowTurn)



和电话需要更改为:


And the call needs to be changed to:

SwitchTurn(ref turn);





3) PlayerTurn的 stoneLeft 参数完全相同(... ) ComputerTurn(..)



3) Exactly the same goes for the stoneLeft parameter of PlayerTurn(..) and ComputerTurn(..):

public static void PlayerTurn(ref int stoneLeft)
// and
public static void ComputerTurn(ref int stoneLeft)



和电话相同:


And the same for the calls:

PlayerTurn(ref stone);
// and
ComputerTurn(ref stone);





现在该程序至少可以编译。我不知道它是否会做你想要的,老实说,我不想分析这个。因为还有另一个问题 - 不是语法问题而是样式问题:所有那些 ref -modifiers使得遵循程序逻辑真的很头疼。您应该避免 out ref ,除非您非常确定它们有意义。有时候它们很有用而且很有意义,在这种情况下,绝对不是。



我建议你为你的游戏逻辑使用非静态类。在 Main(..)中创建一个实例,并在其上调用一个方法来启动游戏。几乎所有的变量都是class-instance-variables然后你就不需要那些 ref -modifiers。



Now the program at least compiles. I have no idea if it will do what you want and, honestly, I don't want to analyze this. Because there's another problem - not a syntactic problem but a style problem: All those ref-modifiers make it really a headache to follow the logic of your program. You should avoid out and ref unless you're very certain that they make sense. Sometimes they are useful and make sense, in this case, definitely not.

I would suggest you use a non-static class for your game logic. Create an instance of it in Main(..) and call a method on it to start the game. Almost all your variables would be class-instance-variables then and you wouldn't need those ref-modifiers.


这篇关于}期望的问题plz帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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