C
C#2y ago
Squirtle

Need help

How do i make it so i cant get into the same case twice while being in a "while" loop
43 Replies
Squirtle
Squirtle2y ago
trying to make a elevator program
TheRanger
TheRanger2y ago
can u show what you tried?
Squirtle
Squirtle2y ago
and when im on floor one (using case and whiles) i want it to say that you are already in floor one and cant get to floor one agian
TheRanger
TheRanger2y ago
show what u tried @Squirtle
Squirtle
Squirtle2y ago
where can i paste the code
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2y ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
TheRanger
TheRanger2y ago
with $code ofcourse
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Squirtle
Squirtle2y ago
BlazeBin - yrwbavpscnqn
A tool for sharing your source code with the world!
Squirtle
Squirtle2y ago
i want it to make it so you cant get into the same floor if you already in case 1 for example i want to type out a message that says you area already in the "bla bla" area for an example dont know if that is possible tho with case
TheRanger
TheRanger2y ago
you need to store at what floor ur currently at to a variable
Squirtle
Squirtle2y ago
so how do i that ? should i use bool ? tried some things with bool
TheRanger
TheRanger2y ago
bool can't store numbers
Squirtle
Squirtle2y ago
ah int ?
TheRanger
TheRanger2y ago
yes
Squirtle
Squirtle2y ago
should i use if ?
TheRanger
TheRanger2y ago
yes ofcourse,
Squirtle
Squirtle2y ago
i should put the if under/above row 6 correct?
TheRanger
TheRanger2y ago
check if ur not in the same floor as the floor ur trying to go to that wouldn't work as you havent chosen the new floor yet
Squirtle
Squirtle2y ago
tried adding an int floor and then when you are in floor 1, floor gets increased to 1
TheRanger
TheRanger2y ago
also create methods to avoid repeating codes
Squirtle
Squirtle2y ago
like ? ah
TheRanger
TheRanger2y ago
u repeated alot of the same code in your cases
Squirtle
Squirtle2y ago
yeah true
TheRanger
TheRanger2y ago
whats only different is the welcome messages, so use a method parameter for that
Squirtle
Squirtle2y ago
true, true floor = 1; for (int i = 1; i < 10; i++) { Console.WriteLine(i + "Seconds"); Thread.Sleep(500); // stannar programkörningen } Console.Clear(); Console.WriteLine("Welcome to Sellingarea"); if (floor == switcher) { Console.WriteLine("You are already in the sellingarea"); } tried that but didnt work since switcher is already 1 and yeah hahah
TheRanger
TheRanger2y ago
u check before you enter the switch statement
Squirtle
Squirtle2y ago
how do i make it so i enter switch statement first before checking
TheRanger
TheRanger2y ago
why would u enter the switch statement before checking if u are in the same floor?
nahr
nahr2y ago
int switcher = 0;
int currentFloor = -1;

while (true)
{
Console.WriteLine("Elevator \nFloor 0: Entre \nFloor 1: Sellingarea \nFloor 2: IT-Department \nFloor 3: Project-management \nFloor 4: Boss");
switcher = ReadInput();
if (currentFloor != switcher)
Switch(switcher);
else
ErrorMsg(currentFloor);
currentFloor = switcher;
}

static void ErrorMsg(int currentFloor)
{
Console.Clear();
Console.WriteLine($"You are already at floor {currentFloor}");
}

static int ReadInput()
{
if (!int.TryParse(Console.ReadLine(), out int switcher) || switcher < 0 || switcher > 4)
Console.WriteLine("Type a button between 0-4 to get to the floors");

return switcher;
}

static void RunElevator(string floor)
{
for (int i = 1; i < 10; i++)
{
Console.WriteLine(i + "Seconds");
Thread.Sleep(500);

}
Console.Clear();
Console.WriteLine($"Welcome to {floor}");
}

static void Switch(int switcher)
{
switch (switcher)
{
case 0:
RunElevator("Entre");
break;
case 1:
RunElevator("Selling area");
break;
case 2:
RunElevator("IT-Department");
break;
case 3:
RunElevator("Project-managment");
break;
case 4:
RunElevator("the boss office");
break;
}
}
int switcher = 0;
int currentFloor = -1;

while (true)
{
Console.WriteLine("Elevator \nFloor 0: Entre \nFloor 1: Sellingarea \nFloor 2: IT-Department \nFloor 3: Project-management \nFloor 4: Boss");
switcher = ReadInput();
if (currentFloor != switcher)
Switch(switcher);
else
ErrorMsg(currentFloor);
currentFloor = switcher;
}

static void ErrorMsg(int currentFloor)
{
Console.Clear();
Console.WriteLine($"You are already at floor {currentFloor}");
}

static int ReadInput()
{
if (!int.TryParse(Console.ReadLine(), out int switcher) || switcher < 0 || switcher > 4)
Console.WriteLine("Type a button between 0-4 to get to the floors");

return switcher;
}

static void RunElevator(string floor)
{
for (int i = 1; i < 10; i++)
{
Console.WriteLine(i + "Seconds");
Thread.Sleep(500);

}
Console.Clear();
Console.WriteLine($"Welcome to {floor}");
}

static void Switch(int switcher)
{
switch (switcher)
{
case 0:
RunElevator("Entre");
break;
case 1:
RunElevator("Selling area");
break;
case 2:
RunElevator("IT-Department");
break;
case 3:
RunElevator("Project-managment");
break;
case 4:
RunElevator("the boss office");
break;
}
}
Squirtle
Squirtle2y ago
Dam thx Really appreciate kt
nahr
nahr2y ago
It's not perfection, but hopefully it can help you to continue your own refactor and test some funny stuff
TheRanger
TheRanger2y ago
ReadInput()'s if condition if false it would return the value of the switcher (a number equal than or less than 0 or a number more than 4
Squirtle
Squirtle2y ago
never understood return what is the function of it
TheRanger
TheRanger2y ago
int number = Sum(2, 2); // number is now equals 4
int Sum(int a, int b)
{
return a + b;
}
int number = Sum(2, 2); // number is now equals 4
int Sum(int a, int b)
{
return a + b;
}
you can get an idea from this example
nahr
nahr2y ago
Good point
Squirtle
Squirtle2y ago
BlazeBin - wadkvvneariv
A tool for sharing your source code with the world!
Squirtle
Squirtle2y ago
did it like this and it works amazingly but when i put in a letter it goes to case 0 thats the only problem i have otherwise its perfect in my eyes hahha
TheRanger
TheRanger2y ago
when it fails to parse switcher becomes 0 by default you should wrap ur tryparse in a while loop
Squirtle
Squirtle2y ago
ah thx
TheRanger
TheRanger2y ago
like this
while (true)
{
if (!int.TryParse(Console.ReadLine(), out switcher) || switcher < 0 || switcher > 4)
{
Console.WriteLine("Type a button between 0-4 to get to the floors");
}
else
{
break; // this breaks out of the while loop
}
}
while (true)
{
if (!int.TryParse(Console.ReadLine(), out switcher) || switcher < 0 || switcher > 4)
{
Console.WriteLine("Type a button between 0-4 to get to the floors");
}
else
{
break; // this breaks out of the while loop
}
}