Home > Blockchain >  How to use a variable's value to change the same variable's value?
How to use a variable's value to change the same variable's value?

Time:02-20

So in pseudo code I was taught something like this was possible

  x <- 0
  y <- 0
 Repeat 
   Input, y
     x <- x   y
 Until x <- 20

But in C# it isn't working. My exact code is

while (choice == "yes") {

int TotalCost;

TicketChoice = Convert.ToInt32(Console.ReadLine());

if ( TicketChoice == 1 )
    {
        Console.WriteLine("Enter Number of Adults:");

        Adults = Convert.ToInt32(Console.ReadLine());

        TotalCost = TotalCost   (20 * Adults);

        

    }
}

And all of this is in a while loop. Is my syntax wrong or is this not possible in C#? It says use of unassigned local variable but I did declare it. I am new to coding so forgive me if I am asking an obvious question. I have yet to write code for all the other Ticket choices. Adults and TicketChoice are variables I declared before.

CodePudding user response:

use while loop

    var TotalCost = 0;
    do
    {
        Console.WriteLine("Enter Number of Adults:");

        var Adults = Convert.ToInt32(Console.ReadLine());

        TotalCost = TotalCost   (20 * Adults);

    } while (TotalCost < 1000);
  • Related