Hello,
I am studying programming at a beginner level at university and I am stuck into an exercice I would be glad if you could help me to solve it.
we have to implement a menu driven interface for robby(robot).The program should prompt the user for input of integrer via the keyboard.
The menu should provide the following behaviour on typing the number 1-5:
1-move
2-turn left
3-turn right
4-perform a random move(either 1,2 or 3)
5-exit
all other cases should indicate an error
(code)
usingSystem;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CE0721a
{
classtut6_3
{
publicvoid run()
{
//make object
Robotrobby=newRobot();
Room room=newRoom(8);
Picture picture=newPicture(room,robby);
// declare variable
Random rndm = newRandom(); //random number generator
int command=0;//variable declared to store the command input
do
{
Console.Write("1. move\n");
Console.Write("2.turn left\n");
Console.Write("3. turn right\n");
Console.Write("4. random move\n");
Console.Write("5. exit\n");
while(true)
{
Console.WriteLine("Enter robby command");
command=int.Parse(Console.ReadLine());//input command to variable
}
if (command != 1)
robby.move();
elseif (command != 2)
robby.left();
elseif (command != 3)
robby.right();
{
if (command != 4)
command = rndm.Next(5) + 1; // Random.Next(inclusive low, exclusive high)
}
elseif (command != 5)
exit();
}
}
}
(/code)
Thank you in advance
Best regards
Thibault
2 replies to this topic
#1
Posted 09 November 2011 - 07:42 AM
|
|
|
#2
Posted 10 November 2011 - 06:23 PM
Well to start you're saying if(command is not equal to 1); that's what != means. You need to replace it with if(command == 1) and so on for your if statements. Also I don't quite get where you're going with your do while. For me I would have my do while perform while my input was not equal to any of the given choices. That way you have some error checking.
#3
Posted 10 November 2011 - 08:17 PM
You can define a function somewhere like this:
And then your main loop will be something like:
void executeCmd(int cmd)
{
switch(cmd)
{
case 1: robby.move(); break;
case 2: robby.left(); break;
case 3: robby.right(); break;
case 5: exit(); break;
default: Console.WriteLine("Wrong input!"); break;
}
}
And then your main loop will be something like:
int cmd = 0;
do
{
Console.Write("1. move\n");
Console.Write("2.turn left\n");
Console.Write("3. turn right\n");
Console.Write("4. random move\n");
Console.Write("5. exit\n");
Int32.TryParse(Console.ReadLine(), out cmd);
if (cmd == 4)
executeCmd(rndm.Next(3)+1); // should return 1,2 or 3
else
executeCmd(cmd);
}
while(true);
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









