D
D
Dymetrey2017-09-30 16:20:04
C++ / C#
Dymetrey, 2017-09-30 16:20:04

How to make answer options (Solved)?

How to make response options with if and else? Type

Console.WriteLine ("1 or 2");
bool a = true
bool b = false

if (a){ 
Console.WriteLine ("Result 1");
}

else (b){
Console.WriteLine ("Result 2");
}
Console.ReadLine;

This is how it should work, but for some reason it doesn't work. Asks after elsa to put ;. I googled about this topic, but gave out such long examples where design is done and all such heresy. In short, I need a banal choice. Like if I wrote 1, it displayed such a text, I wrote 2, it displayed such and such a text.
ps Only that bool is highlighted where true
int a;
Console.WriteLine("When was the collapse of the USSR?");
a = Convert.ToInt32(Console.ReadLine());
if (a == 1991) {
Console.WriteLine("Correct!");
}
else{
Console.WriteLine("Not true");
}
This is how it works, I found the solution on YouTube

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir A, 2017-09-30
@hauptling

bool isOk;
if (isOk == true)
{ 
      std::cout << "result1";
}
else
{
      std::cout << "result2";
}

PS in c++ is the same as in any other language. you assign boolean logic 2, there is only true and false, and from 1 to 255 -> this is true
if you need to compare a specific number, then you need another data type, for example:
size_t answer;
if (answer == 1)
{
      std::cout << "result1";
}
else if(answer == 2)
{
      std::cout << "result2";
}

R
res2001, 2017-09-30
@res2001

In fact, it is not clear how the code should work, what you want to get from it.
Re-read the C# tutorial where the if statement is described. You are not using the else construct in it correctly.

if (a){
Console.WriteLine ("Result 1");
} else if(b){
Console.WriteLine ("Result 2");
}

D
Daniel, 2017-09-30
@Dyaminigo

You can do without bool in this case, instead, compare the entered and expected text.
Comparison example:
// Get the entered text by writing it to the variable text
string text = Console.ReadLine();
// Compare: if 1 is entered, then output "result 1"
if (text == "1")
Console.WriteLine("result 1");
// If the text is not equal to 1, but is equal to 2, then output result 2
else if (text == "2")
Console.WriteLine("result 2");
// If the text is neither 1 nor 2, then output something else
else
Console.WriteLine("Method not found");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question