语法 c# 的问题,无法使用 ojects/call get、set、
Issues with syntax c#, unable to use ojects/call get, set,
我是 C# 的新手,一直在鬼屋中从事迷宫游戏的项目 context.After 研究不同的方法我决定使用对象和链表。然而,尽管尝试了数周,但我仍在努力编写代码,并且在阅读文章和观看在线教程数小时后,我已经达到了我现在比以往任何时候都更加困惑的地步。我想使用这种方法而不是数组,因为我觉得这样更高效且更面向对象。
我正在考虑使用一个简单的 if/else 结构,但对于这种级别的编码,我觉得它太混乱了。
任何帮助、建设性的批评或想法将不胜感激,因为我不想在花了这么多时间后放弃,但我觉得它已经到了那个地步。到目前为止,这是我的代码。
提前致谢:)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace maze_3
{//open namespace
class Room
{//open class room
private string RoomName;
private Room N = null; // so that when i set the links they are automatically null unless i specify in main
private Room E = null;
private Room S = null;
private Room W = null;
public Room X { get; private set; } // the software implemented this
public void setName(string N) // so that i am able to set the name of the room
{
RoomName = N;
}
public void setN(Room X) // sets the north direction between the objects of the rooms
{
N = X;
}
public void setE(Room X)
{
E = X;
}
public void setW(Room X)
{
W = X;
}
public void setS(Room x)
{
S = X;
}
public Room getN() // want to get a direction from the user, in this case they would input a north direction
{
return N;
}
public Room getE()
{
return E;
}
public Room getS()
{
return S;
}
public Room getW()
{
return W;
}
static void Main(string[] args)// it is asking for a ; here but also says that I should declare it as extern, partial etc
class chamber// its telling me that a ; is expected ???
{//open class chamber
chamber gh = new chamber();// my objects that are the rooms in the haunted house.
chamber kit = new chamber();
chamber Pan = new chamber();
chamber Dun = new chamber();
chamber dr = new chamber();
chamber lib = new chamber();
chamber din = new chamber();
chamber sr = new chamber();
chamber weap = new chamber();
chamber tow = new chamber();
chamber gal = new chamber();
chamber tr = new chamber();
gh.RoomName("Great Hall"); //to set the object name as the Great Hall, all get and set links were available in class
gh.setW(dr); ///I want to set a west direction between my object gh to dr
gh.SetS(kit);// to set a south link between my two objects
dr.setName("Drawing Room");//states that all my setlinks are not valid in the current context- this is for all
dr.setS(lib); //it states the ; is not a valid token in class, struct or interface
kit.setName("Kitchen");// it states that my objects e.g kit is not valid in this current context-this is for all
kit.setS(pan);
pan.setName("Pantry");
pan.SetE(dun); /// this is a dead end in the game
lib.setName("Library ");
lib.setE(din);
din.setName("Dining Room");
din.setN(sr);
din.setE(gal);
din.setS(weap); //dead end in the game
sr.setName("Smoking Room");
sr.setE(tow);//dead end
gal.setName("Treasure Room");
gal.setS(tr)
/// </summary> so i wanted to have the actual game play to follow a linked list with a if else structure.
int finish = 0;
string choice;
Room current;
current=gh;
while (finish !=1) (finish ==0) //im confused as to which condition is best to use.
{Console.WriteLine("You are in room " + current.getRoomname() + "and you can move ");
if( current.getN() != null)
Console.WriteLine("North (N) ");
if( current.getE() != null)
Console.WriteLine("East (E) ");
Console.WriteLine("Please enter the direction you wish to go in ");
string choice = Console.ReadLine();
if (choice[0] == 'E') // or alternative means of getting in a choice from a string
current = current.getE();// to be able to move to next 'current'room
// i was going to do this for all the options.
if(current == tr // ie last room
exit = 1;
Console.Writeline ("Well done you have found the treasure room");
}//close chamber
}//close class rooom
} //close namespace
您创建了一个没有 main() 的 class 对象。一个项目只有一个主。因此,您要么需要将 Room 设为应用程序中的 main(),要么从 Room Class 中删除 main 方法。请参阅下面的代码以了解更改。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace maze_3
{
class Program
{
static void Main(string[] args)
{
Room home = new Room("White House");
home.CreateRoom("W", "West Wing");
}
}
class Room
{//open class room
private string RoomName;
private Room N = null; // so that when i set the links they are automatically null unless i specify in main
private Room E = null;
private Room S = null;
private Room W = null;
private List<Chamber> chambers = new List<Chamber>();
public Room() { }
public Room(string name)
{
this.RoomName = name;
}
public void CreateRoom(string direction, string name)
{
Room newRoom = new Room();
newRoom.RoomName = name;
switch (direction)
{
case "N":
N = newRoom;
break;
case "E":
E = newRoom;
break;
case "S":
S = newRoom;
break;
case "W":
W = newRoom;
break;
}
}
}//close class rooom
public class Chamber// its telling me that a ; is expected ???
{//open class chamber
string name = "";
}//close chamber
}
我是 C# 的新手,一直在鬼屋中从事迷宫游戏的项目 context.After 研究不同的方法我决定使用对象和链表。然而,尽管尝试了数周,但我仍在努力编写代码,并且在阅读文章和观看在线教程数小时后,我已经达到了我现在比以往任何时候都更加困惑的地步。我想使用这种方法而不是数组,因为我觉得这样更高效且更面向对象。 我正在考虑使用一个简单的 if/else 结构,但对于这种级别的编码,我觉得它太混乱了。 任何帮助、建设性的批评或想法将不胜感激,因为我不想在花了这么多时间后放弃,但我觉得它已经到了那个地步。到目前为止,这是我的代码。 提前致谢:)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace maze_3
{//open namespace
class Room
{//open class room
private string RoomName;
private Room N = null; // so that when i set the links they are automatically null unless i specify in main
private Room E = null;
private Room S = null;
private Room W = null;
public Room X { get; private set; } // the software implemented this
public void setName(string N) // so that i am able to set the name of the room
{
RoomName = N;
}
public void setN(Room X) // sets the north direction between the objects of the rooms
{
N = X;
}
public void setE(Room X)
{
E = X;
}
public void setW(Room X)
{
W = X;
}
public void setS(Room x)
{
S = X;
}
public Room getN() // want to get a direction from the user, in this case they would input a north direction
{
return N;
}
public Room getE()
{
return E;
}
public Room getS()
{
return S;
}
public Room getW()
{
return W;
}
static void Main(string[] args)// it is asking for a ; here but also says that I should declare it as extern, partial etc
class chamber// its telling me that a ; is expected ???
{//open class chamber
chamber gh = new chamber();// my objects that are the rooms in the haunted house.
chamber kit = new chamber();
chamber Pan = new chamber();
chamber Dun = new chamber();
chamber dr = new chamber();
chamber lib = new chamber();
chamber din = new chamber();
chamber sr = new chamber();
chamber weap = new chamber();
chamber tow = new chamber();
chamber gal = new chamber();
chamber tr = new chamber();
gh.RoomName("Great Hall"); //to set the object name as the Great Hall, all get and set links were available in class
gh.setW(dr); ///I want to set a west direction between my object gh to dr
gh.SetS(kit);// to set a south link between my two objects
dr.setName("Drawing Room");//states that all my setlinks are not valid in the current context- this is for all
dr.setS(lib); //it states the ; is not a valid token in class, struct or interface
kit.setName("Kitchen");// it states that my objects e.g kit is not valid in this current context-this is for all
kit.setS(pan);
pan.setName("Pantry");
pan.SetE(dun); /// this is a dead end in the game
lib.setName("Library ");
lib.setE(din);
din.setName("Dining Room");
din.setN(sr);
din.setE(gal);
din.setS(weap); //dead end in the game
sr.setName("Smoking Room");
sr.setE(tow);//dead end
gal.setName("Treasure Room");
gal.setS(tr)
/// </summary> so i wanted to have the actual game play to follow a linked list with a if else structure.
int finish = 0;
string choice;
Room current;
current=gh;
while (finish !=1) (finish ==0) //im confused as to which condition is best to use.
{Console.WriteLine("You are in room " + current.getRoomname() + "and you can move ");
if( current.getN() != null)
Console.WriteLine("North (N) ");
if( current.getE() != null)
Console.WriteLine("East (E) ");
Console.WriteLine("Please enter the direction you wish to go in ");
string choice = Console.ReadLine();
if (choice[0] == 'E') // or alternative means of getting in a choice from a string
current = current.getE();// to be able to move to next 'current'room
// i was going to do this for all the options.
if(current == tr // ie last room
exit = 1;
Console.Writeline ("Well done you have found the treasure room");
}//close chamber
}//close class rooom
} //close namespace
您创建了一个没有 main() 的 class 对象。一个项目只有一个主。因此,您要么需要将 Room 设为应用程序中的 main(),要么从 Room Class 中删除 main 方法。请参阅下面的代码以了解更改。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace maze_3
{
class Program
{
static void Main(string[] args)
{
Room home = new Room("White House");
home.CreateRoom("W", "West Wing");
}
}
class Room
{//open class room
private string RoomName;
private Room N = null; // so that when i set the links they are automatically null unless i specify in main
private Room E = null;
private Room S = null;
private Room W = null;
private List<Chamber> chambers = new List<Chamber>();
public Room() { }
public Room(string name)
{
this.RoomName = name;
}
public void CreateRoom(string direction, string name)
{
Room newRoom = new Room();
newRoom.RoomName = name;
switch (direction)
{
case "N":
N = newRoom;
break;
case "E":
E = newRoom;
break;
case "S":
S = newRoom;
break;
case "W":
W = newRoom;
break;
}
}
}//close class rooom
public class Chamber// its telling me that a ; is expected ???
{//open class chamber
string name = "";
}//close chamber
}