C
C#3y 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
SquirtleOP3y ago
trying to make a elevator program
TheRanger
TheRanger3y ago
can u show what you tried?
Squirtle
SquirtleOP3y 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
TheRanger3y ago
show what u tried @Squirtle
Squirtle
SquirtleOP3y ago
where can i paste the code
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX3y 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 User3y ago
Message Not Public
Sign In & Join Server To View
TheRanger
TheRanger3y ago
with $code ofcourse
MODiX
MODiX3y 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
SquirtleOP3y ago
BlazeBin - yrwbavpscnqn
A tool for sharing your source code with the world!
Squirtle
SquirtleOP3y 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
TheRanger3y ago
you need to store at what floor ur currently at to a variable
Squirtle
SquirtleOP3y ago
so how do i that ? should i use bool ? tried some things with bool
TheRanger
TheRanger3y ago
bool can't store numbers
Squirtle
SquirtleOP3y ago
ah int ?
TheRanger
TheRanger3y ago
yes
Squirtle
SquirtleOP3y ago
should i use if ?
TheRanger
TheRanger3y ago
yes ofcourse,
Squirtle
SquirtleOP3y ago
i should put the if under/above row 6 correct?
TheRanger
TheRanger3y 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
SquirtleOP3y ago
tried adding an int floor and then when you are in floor 1, floor gets increased to 1
TheRanger
TheRanger3y ago
also create methods to avoid repeating codes
Squirtle
SquirtleOP3y ago
like ? ah
TheRanger
TheRanger3y ago
u repeated alot of the same code in your cases
Squirtle
SquirtleOP3y ago
yeah true
TheRanger
TheRanger3y ago
whats only different is the welcome messages, so use a method parameter for that
Squirtle
SquirtleOP3y 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
TheRanger3y ago
u check before you enter the switch statement
Squirtle
SquirtleOP3y ago
how do i make it so i enter switch statement first before checking
TheRanger
TheRanger3y ago
why would u enter the switch statement before checking if u are in the same floor?
nahr
nahr3y 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
SquirtleOP3y ago
Dam thx Really appreciate kt
nahr
nahr3y ago
It's not perfection, but hopefully it can help you to continue your own refactor and test some funny stuff
TheRanger
TheRanger3y 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
SquirtleOP3y ago
never understood return what is the function of it
TheRanger
TheRanger3y 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
nahr3y ago
Good point
Squirtle
SquirtleOP3y ago
BlazeBin - wadkvvneariv
A tool for sharing your source code with the world!
Squirtle
SquirtleOP3y 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
TheRanger3y ago
when it fails to parse switcher becomes 0 by default you should wrap ur tryparse in a while loop
Squirtle
SquirtleOP3y ago
ah thx
TheRanger
TheRanger3y 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
}
}
Want results from more Discord servers?
Add your server