When typing the following code, the console opens and I can input a value but when I press a key the .NET crashes and the console shuts down
I've tried to type only "text" + input and $ at the start of "text"
using System;
namespace Programmeren1Week2
{
class Program
{
const double BTW = 0.21;
static void Main(string[] args)
{
Console.WriteLine("Geef prijs:");
Console.ReadLine();
double invoer = double.Parse(Console.ReadLine());
double metBTW = invoer * BTW;
Console.WriteLine($"De prijs is {0}:" + invoer, "de btw is {1}:" + BTW, "Totaalprijs is {2}: " + metBTW);
Console.ReadKey();
}
}
}
I managed to reproduce your issue by passing in a text value where you ask for input. If by
I've tried to type only "text" + input and $ at the start of "text"
You mean you are typing "text" when prompted for input, that's your problem.
Your invoer
variable is a double, so it cannot accept strings as input. Try inputting a numerical value, and see if that fixes your problem.
That issue aside, there is one other thing that needs fixing.
As a few others have pointed out, you are formatting your string incorrectly. This will cause an issue if you fix your original error. To fix this, change that line to be:
Console.WriteLine($"The prize is {invoer}, the VAT is {BTW}: Total price is {metBTW}: ");
This should produce a functioning program.
It's probably that you are entering a string that cannot be parsed to a double in your ReadLine call.
If propose this alternative solution to handle this issue.
Console.WriteLine("Geef prijs:");
if(double.TryParse(Console.ReadLine(), out double invoer))
{
double metBTW = invoer * BTW;
Console.WriteLine($"De prijs is : {invoer} , de btw is : {BTW} , otaalprijs is : {metBTW}");
Console.ReadKey();
}
else
{
Console.WriteLine("Bad input");
}
Instead of using Parse use TryParse. (It returns true for valid values, and false for invalid values.) Use it like this:
double number;
if (Double.TryParse(Console.ReadLine(), out number))
{
// normal flow
}
else
{
// bad input (not a double, request another input)
}
Try this
Console.WriteLine(String.Format("De prijs is {0}: de btw is {1}: Totaalprijs is {2}: ", invoer, BTW, metBTW));
Or
This
Console.WriteLine($"De prijs is {invoer}: de btw is {BTW}: Totaalprijs is {metBTW}: " );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.