locked
Code help RRS feed

  • Question

  • Hello,

    I am new to Visual Studios C# and I am trying to write a code that describes a process of a machine. The program asks the user types in the current values of 2 sensors (0 for off, 1 for on). The problem that I am having is that when the user types a value of 0 for sensor 1 and a value of 1 for sensor 2, the program gives the output for sensor 1 and not sensor 2.  I was wondering how I would do that? (I am using .NET framework in Visual Studios C#). Thanks in advance. 

    Here is a sample of my code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace Question_1_Program
    {
        class Program
        {
            static async Task Main(string[] args)
            {
                int x, y;
                //User inputs the current value of sensors 1 & 2 using values 0 & 1 (0 = False/Off & 1 = True/On). 
                Console.WriteLine("Input the value for sensor one (0 = False/Off, 1 = True/On):");
                x = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine("Input the value for sensor two (0 = False/Off, 1 = True/On):");
                y = Convert.ToInt32(Console.ReadLine());

                if (x==1)
                //Delay is added between each message.
                Console.WriteLine("Turning off mixing motor 1. Please wait...");
                await Task.Delay(2500);
                Console.WriteLine("Closing valve 3, and opening valves 1 and 2. Please wait...");
                await Task.Delay(2500);
                Console.WriteLine("Filling up tank. Please wait...");
                await Task.Delay(2000);
                Console.WriteLine("Tank is 100% full!");
                await Task.Delay(1200);
                Console.WriteLine("Commencing shut down of valves 1 and 2. Please wait...");
                await Task.Delay(1200);
                Console.WriteLine("Valves 1 and 2 shutdown.");
                //Paint has now reached sensor 2. Sensor 2's process begins. 

                if (y == 1) 
                Console.WriteLine("Ensuring valves 1, 2, and 3 are sealed. Please wait...");
                await Task.Delay(2000);
                Console.WriteLine("Valves 1, 2 and 3 are sealed. Preparing mixing motor. Please wait...");
                await Task.Delay(4000);
                Console.WriteLine("Mixing motor initialized. Please wait 60 seconds...");
                await Task.Delay(60000);

                Console.ReadLine();
            }
        }
    }


    Wednesday, March 4, 2020 11:53 PM

Answers

  • Greetings Extra.

    I think your problem is you need braces (curly brackets) around your "if" blocks.

    if(x == 1) { // you need a pair of braces like this... // code for x == 1 goes here.

    } // and this. if(y == 1) { // code for y == 1 goes here. }

    Edit : If you do not have the braces, only the first line after the "if" will be executed if the condition is true.


    • Edited by Ante Meridian Thursday, March 5, 2020 12:16 AM Added the bit about the first line.
    • Marked as answer by Extra Account Thursday, March 5, 2020 12:23 AM
    Thursday, March 5, 2020 12:12 AM

All replies

  • Greetings Extra.

    I think your problem is you need braces (curly brackets) around your "if" blocks.

    if(x == 1) { // you need a pair of braces like this... // code for x == 1 goes here.

    } // and this. if(y == 1) { // code for y == 1 goes here. }

    Edit : If you do not have the braces, only the first line after the "if" will be executed if the condition is true.


    • Edited by Ante Meridian Thursday, March 5, 2020 12:16 AM Added the bit about the first line.
    • Marked as answer by Extra Account Thursday, March 5, 2020 12:23 AM
    Thursday, March 5, 2020 12:12 AM
  • Thanks so much. I can't believe I forgot those brackets!! That problem's been driving me crazy!!

    Once again, Thank you.

    Thursday, March 5, 2020 12:24 AM
  • One more Quick question. How would I add a countdown timer that shows the numbers counting down from 60 seconds to 0 seconds using the code from my first question?

    Thanks in advance. 

    Thursday, March 5, 2020 12:31 AM
  • Hi,

    There is an easy way to do this, which is to decompose Task.Delay().

      static async Task Main(string[] args)
            {
                int x, y;
                //User inputs the current value of sensors 1 & 2 using values 0 & 1 (0 = False/Off & 1 = True/On). 
                Console.WriteLine("Input the value for sensor one (0 = False/Off, 1 = True/On):");
                x = Convert.ToInt32(Console.ReadLine());
    
                Console.WriteLine("Input the value for sensor two (0 = False/Off, 1 = True/On):");
                y = Convert.ToInt32(Console.ReadLine());
    
                if (x == 1)
                {
                    //Delay is added between each message.
                    Console.WriteLine("Turning off mixing motor 1. Please wait...");
                    await Task.Delay(2500);
                    Console.WriteLine("Closing valve 3, and opening valves 1 and 2. Please wait...");
                    await Task.Delay(2500);
                    Console.WriteLine("Filling up tank. Please wait...");
                    await Task.Delay(2000);
                    Console.WriteLine("Tank is 100% full!");
                    await Task.Delay(1200);
                    Console.WriteLine("Commencing shut down of valves 1 and 2. Please wait...");
                    await Task.Delay(1200);
                    Console.WriteLine("Valves 1 and 2 shutdown.");
                    //Paint has now reached sensor 2. Sensor 2's process begins. 
                }
                if (y == 1)
                {
                    Console.WriteLine("Ensuring valves 1, 2, and 3 are sealed. Please wait...");
                    await Task.Delay(2000);
                    Console.WriteLine("Valves 1, 2 and 3 are sealed. Preparing mixing motor. Please wait...");
                    await Task.Delay(4000);
                    Console.WriteLine("Mixing motor initialized. Please wait 60 seconds...");
                    for (int a = 60; a >= 0; a--)
                    {
                        Console.WriteLine("Number: {0}", a);
                        await Task.Delay(1000);
                    }
                }
                Console.ReadLine();
            }

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 5, 2020 2:33 AM