Question

[C#] I need to convert this if/else statements into Switch I have the code for the...

[C#] I need to convert this if/else statements into Switch

I have the code for the if/else but now need to turn it into a switch. The Console.WriteLine and all that is fine. I just need to convert the if/else.

     int x1, x2, y1, y2;
                Console.WriteLine("What is smallest value of x:");
                x1 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("What is largest value of x:");
                x2 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("What is smallest value of y:");
                y1 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("What is largest value of y:");
                y2 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Enter the operator:");
                string op = Console.ReadLine();
                Console.WriteLine("How many times should it happen?:");
                int n = Convert.ToInt32(Console.ReadLine());
                Random random = new Random();
                for (int i = 1; i <= n; i++)
                {
                    int x = random.Next(x1, x2);
                    int y = random.Next(y1, y2);
                    Console.Write("What is the result of " + x + " " + op + " " + y + " = ");
                    int ans = Convert.ToInt32(Console.ReadLine());
                    int result = 0;
                    if (op == "+")
                    {
                        result = x + y;
                    }
                    else if (op == "-")
                    {
                        result = x - y;
                    }
                    else if (op == "*")
                    {
                        result = x * y;
                    }
                    else if (op == "%")
                    {
                        result = x % y;
                    }
                    else
                    {
                        result = x / y;
                    }
                    if (result == ans)
                    {
                        Console.WriteLine("Congratulations");
                    }
                    else
                    {
                        Console.WriteLine("Wrong answer. Correct answer is " + result);
                    }
                }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have modified the code with switch statement

using System;

public class GFG{
   static public void Main (){
       //Code
       int x1, x2, y1, y2;
Console.WriteLine("What is smallest value of x:");
x1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("What is largest value of x:");
x2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("What is smallest value of y:");
y1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("What is largest value of y:");
y2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the operator:");
string op = Console.ReadLine();
Console.WriteLine("How many times should it happen?:");
int n = Convert.ToInt32(Console.ReadLine());
Random random = new Random();
for (int i = 1; i <= n; i++)
{
int x = random.Next(x1, x2);
int y = random.Next(y1, y2);
Console.Write("What is the result of " + x + " " + op + " " + y + " = ");
int ans = Convert.ToInt32(Console.ReadLine());
int result = 0;
switch(op)
{
case "+":
result = x + y;
break;
case "-":
result = x - y;
break;
case "*":
result = x * y;
break;
case "%":
result = x % y;
break;
case "/":
result = x / y;
break;
  
}
if (result == ans)
{
Console.WriteLine("Congratulations");
}
else
{
Console.WriteLine("Wrong answer. Correct answer is " + result);
}
}
   }
}

16 17 18 19 20 21 string op Console.ReadLine(); Console.WriteLine(How many times should it happen?:); int n -Convert.ToInt3Copy >RunRun+URL (Generates URL as well) Time(sec): 0.06 Memory(MB): 0.125 Output: Copy hat is smallest value of x hat is larIf you are satisfied please upvote or if you have any doubts post them in comment section

Add a comment
Know the answer?
Add Answer to:
[C#] I need to convert this if/else statements into Switch I have the code for the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello in C#. I need help understanding and knwoing what i missed in my program. The...

    Hello in C#. I need help understanding and knwoing what i missed in my program. The issue is, the program is supposed to have user input for 12 family members and at the end of the 12 it asks for user to input a name to search for. When i put a correct name, it always gives me a relative not found message. WHat did i miss. And can you adjust the new code so im able to see where...

  • Greetings everyone I am having a little trouble with this one. I bolded my switch case...

    Greetings everyone I am having a little trouble with this one. I bolded my switch case which should work but its currently not. If a user selected course option 1 twice it just says you enrolled in course 1 and course 1. It should stop it by executing case -2. Need a new set of eyes. - Thanks using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleRegisterStudent { class Program { static void Main(string[] args) { (new...

  • i need help fixing my code. here is the assignment: Make a class that contains the...

    i need help fixing my code. here is the assignment: Make a class that contains the following functions: isLong, isDouble, stringToLong, and stringToDouble. Each receives a string and returns the appropriate type (bool, bool, long, and double).During the rest of the semester you will paste this class into your programs and will no longer use any of the "standard" c# functions to convert strings to numbers. here is my code. it works for the long method but not the double:...

  • This C# program prints out a corresponding letter grade for a given mark. It uses a...

    This C# program prints out a corresponding letter grade for a given mark. It uses a method (called MarktoGrade) to determine the letter grade. MarktoGrade takes one integer call by value formal parameter (called mark) and returns a char value that is the letter grade. All of the input and output (except an error message) is done in Main. There is one syntax error in this program that you will need to fix before it will compile (the error is...

  • C#, I am getting error placing them all in one file pls set them in the...

    C#, I am getting error placing them all in one file pls set them in the order that all 3 work in one file. Thanks //Program 1 using System; class MatrixLibrary { public static int[,] Matrix_Multi(int[,] a, int [,]b){ int r1= a.GetLength(0); int c1 = a.GetLength(1); int c2 = b.GetLength(1); int r2 = b.GetLength(0); if(r2 != c2){ Console.WriteLine("Matrices cannot be multiplied together.\n"); return null;    }else { int[,] c = new int[r1, c2]; for (int i = 0; i <...

  • Need to write an IPO for this code sample code below what i have done in...

    Need to write an IPO for this code sample code below what i have done in IPO below that     //collects data and checks that its between the 24 hour and 59 min             // changes             Console.WriteLine("Enter starting hour : ");             int hour = int.Parse(Console.ReadLine());             while (hour < 0 || hour > 23)             {                 Console.WriteLine("\nEnter starting hour : ");                 hour = int.Parse(Console.ReadLine());             }             Console.WriteLine("\nEnter starting minute : ");             int minute =...

  • C++ code for CIS054 Create a program that uses a function to determine the length of...

    C++ code for CIS054 Create a program that uses a function to determine the length of a line by inputting the X,Y coordinates of the line endpoints. Show the result with a precision of four decimal places. The function prototype is to be specified as follows:     double LengthOfLine (double X1, double Y1, double X2, double Y2); // returns length of line by using this code: #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main(int argc, char* argv[])...

  • in a c# program I am trying to allow the area to be divided by a...

    in a c# program I am trying to allow the area to be divided by a set choice. the area is set as an int in another method and I am unable to call it into the method I need it in. Is there a way to call Int? I also need to set each case to have an integer value. For example if they select 1 it is set at a value of 5. This is what I have...

  • Problem: The code I have below compiled/debugged using C#, however I need to measure the time...

    Problem: The code I have below compiled/debugged using C#, however I need to measure the time it takes to do the 10,000,000 searches for each of the eight arrays. Could you compare the timings to the theoretical timings the algorithm binary search provides. Instructions prior to this: Implement a binary search function in C#. In this program we are to carry out the same 10,000,000 unsuccessful searches for eight different-sized arays, namely arrays of sizes 128, 512, 2,048, 8,192, 32,768,...

  • I need help with the following assignment: Write an application named DailyTemps that continuously prompts a...

    I need help with the following assignment: Write an application named DailyTemps that continuously prompts a user for a series of daily high temperatures until the user enters a sentinel value of 999. Valid temperatures range from -20 through 130 Fahrenheit. When the user enters a valid temperature, add it to a total; when the user enters an invalid temperature, display the error message: Valid temperatures range from -20 to 130. Please reenter temperature. Before the program ends, display the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT