Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

c# - building a vending machine interface as console app but getting error - can't convert a double to a bool

    static void Main(string[] args)
    {
        double creditsvalue;
        string inputString;

        double totalvalue;
        string InputString;

        double checkout;
        string INputString;

        Console.WriteLine("Hi");Console.WriteLine("The items and their prices are as follows:");
        Console.WriteLine("Chocolate bar at 0.80");
        Console.WriteLine("Soda can at 0.70"); 
        Console.WriteLine("Soda bottle at 1.25"); 
        Console.WriteLine("Crisps at 0.50");
        Console.WriteLine("Cookies at 1.10");
        
        Console.WriteLine("How many credits to you want to add to your account?"); 
        inputString = Console.ReadLine();
        creditsvalue = double.Parse(inputString);
        Console.WriteLine("is this ammount {0} correct", creditsvalue);
        Console.ReadLine();

        Console.WriteLine("Which items would you like to have?");
        InputString = Console.ReadLine();
        totalvalue = double.Parse(InputString);
        Console.WriteLine("The total is {0}", totalvalue);

        double accountvalue = creditsvalue - totalvalue;

        Console.WriteLine("Would you like to checkout or add more things?");
        INputString = Console.ReadLine();
        checkout = double.Parse(INputString);

        if (checkout);

    }

this is the code I have so far please help me out cause I am really stuck. it says that I can't convert a double to a bool and back if I try to change it into a bool.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First, change the type of the checkout from double to bool:

bool checkout;

And then change the parsing logic to something like this

Console.WriteLine("Would you like to checkout or add more things?");
INputString = Console.ReadLine();
checkout = INputString.ToLower().Equals("yes");

if (checkout) {
   // Your code here
}

Note: In the code we're expecting the user to write "yes" to checkout.
If you'd like to type something else, then just replace that word with your desired word.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...