代码末尾的Grandtotal问题 - 代码是C# [英] Grandtotal issue at the end of the code - code is C#
本文介绍了代码末尾的Grandtotal问题 - 代码是C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
static void Main(string[] args)
{
//Declare Variables
Double purchasePrice = -1;
Double discountPrice = 0;
Double grandTotal = 0;
Double itemNumber = 1;
//Output outputs
Console.Write("Welcome to our Firework Stand Checkout");
//Do while loop
do
{
Console.WriteLine("Enter the purchase price of the item (Enter -1 to Quit): ");
purchasePrice = Convert.ToDouble(Console.ReadLine());
if (purchasePrice != -1)
{
if (purchasePrice >= 50)
{
discountPrice = purchasePrice * .90;
}
else
{
discountPrice = purchasePrice;
}
Console.WriteLine("Item number: ");
itemNumber = itemNumber + 1;
Console.WriteLine("Original cost is: " + purchasePrice);
Console.WriteLine("Discounted cost is: " + discountPrice);
}
} while (purchasePrice != -1);
Console.WriteLine("Your grand total is " + grandTotal);
grandTotal = purchasePrice + discountPrice;
Console.ReadLine();
我的尝试:
我已经尝试了我能想到的一切。我只是不确定如何从循环中获取信息来计算总计,如你所见。
What I have tried:
I have tried everything I could think of. I am just not sure how to get the information from the loop to calculate the grand total as you can see.
推荐答案
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Code_ProjectConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
float purchasePrice = -1;
float discountPrice = 0;
float grandTotal = 0;
float itemNumber = 1;
//Output outputs
Console.Write("Welcome to our Firework Stand Checkout\n \n");
//Do while loop
do
{
Console.WriteLine("Enter the purchase price of the item (Enter -1 to Quit): ");
purchasePrice = float.Parse(Console.ReadLine());
if (purchasePrice != -1)
{
if (purchasePrice >= 50)
{
discountPrice = purchasePrice * .90f;
}
else
{
discountPrice = purchasePrice;
}
grandTotal = grandTotal + discountPrice;
Console.WriteLine("Item number: ");
itemNumber = itemNumber + 1;
Console.WriteLine("Original cost is: " + purchasePrice);
Console.WriteLine("Discounted cost is: " + discountPrice);
}
} while (purchasePrice != -1);
Console.WriteLine("Your grand total is " + grandTotal);
Console.ReadLine();
}
}
}
这篇关于代码末尾的Grandtotal问题 - 代码是C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文