考虑到用餐的膳食价格(膳食的基本费用),小费百分比(作为小费添加的膳食价格的百分比)和税收百分比(膳食价格作为税收的百分比),找到并打印餐的总费用。
条件:请务必使用精确的值进行计算,否则您可能会得到错误的舍入结果!
这是我的计划:
static void Main(String[] args)
{
double mealCost=Convert.ToDouble(Console.ReadLine());
int tipPercent=Convert.ToInt32(Console.ReadLine());
int taxPercent=Convert.ToInt32(Console.ReadLine());
double tip,tax;
tip=(mealCost*(tipPercent/100));
tax=(mealCost*(taxPercent/100));
double totalCost=mealCost+tip+tax;
Console.WriteLine("The total meal cost is {0} dollars",totalCost);
Console.ReadLine();
}
但我的输出为12。
我的预期输出为15。
如果我的样本输入是12.00 20 8
我的计算结果为tip=2.4
和tax=0.96
,totalCost=15.36
并且舍入的值是
(round)totalCost=15
。
但输出为12。
如何在C#中获得正确的输出。任何人都可以提供一些解决此问题的建议。
答案 0 :(得分:1)
您需要做的是,将tipPercent
和taxPercent
作为双值,或者在处理除法之前隐式将它们转换为double,如下所示:
tip = (mealCost * ((double)tipPercent / 100));
tax = (mealCost * ((double)taxPercent / 100));
然后,您将获得问题中指定的输入totalCost=15.36
。更智能的解决方案是:
double mealCost, tipPercent, taxPercent;
Console.WriteLine("Enter values for Meal Cost, Tip percentage and tax percentage");
if (!double.TryParse(Console.ReadLine(), out mealCost))
{
Console.WriteLine("Invalid input for meal Cost");
}
if (!double.TryParse(Console.ReadLine(), out tipPercent))
{
Console.WriteLine("Invalid input for Tip percentage");
}
if (!double.TryParse(Console.ReadLine(), out taxPercent))
{
Console.WriteLine("Invalid input for Tip tax Percent");
}
double tip = (mealCost * (tipPercent / 100));
double tax = (mealCost * (taxPercent / 100));
double totalCost = mealCost + tip + tax;
Console.WriteLine("The total meal cost is {0}", totalCost.ToString("C0"));
Console.ReadLine();
答案 1 :(得分:1)
首先,请将您的数据类型更改为public class RepoListFragment extends Fragment implements RepoListContract.View {
@BindView(R.id.rv_repo) RecyclerView rvRepo;
@Inject RepoListContract.Presenter presenter;
@Inject LinearLayoutManager layoutManager;
@Inject RepoListAdapter adapter;
public static RepoListFragment newInstance() {
return new RepoListFragment();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_repo_list, container, false);
ButterKnife.bind(this, v);
initRvRepo();
return v;
}
private void initRvRepo() {
rvRepo.setLayoutManager(layoutManager); // null
rvRepo.setAdapter(adapter); //null
}
@Override
public void onResume() {
super.onResume();
presenter.subscribe(); //NullPointerException
}
}
而不是decimal
,更适合与资金相关的内容。
其次,当您进行计算时,C#将尝试使用相同的数据类型返回,这会导致:
double
你有很多方法可以做到,比如施放为双倍:
tip=(mealCost*(tipPercent/100)); // it will turn tipPercent/100 to int, which is 0
tax=(mealCost*(taxPercent/100)); // same here
说明tip = (mealCost * ((double) tipPercent / 100));
到100
(告诉c#它是双倍的)
100D
或者,只需对tip = (meanCost * (tipPercent / 100D));
和tipPercent
taxPercent
答案 2 :(得分:1)
static void Main(String[] args)
{
double mealCost=Convert.ToDouble(Console.ReadLine());
int tipPercent=Convert.ToInt32(Console.ReadLine());
int taxPercent=Convert.ToInt32(Console.ReadLine());
double tip,tax;
tip=(mealCost*(tipPercent/100.0));//change 100 to 100.0
tax=(mealCost*(taxPercent/100.0));//change 100 to 100.0
double totalCost=mealCost+tip+tax;
Console.WriteLine("The total meal cost is {0} dollars",totalCost);
Console.ReadLine();
}
答案 3 :(得分:0)
由于您将0
值除以int
值,因此您获得int
。除法将结果舍入为零,结果的绝对值是小于两个操作数的商的绝对值的最大可能整数。您可以查看C#如何处理它here。
如果您重写提示,请使用以下计算税额。
tip = (mealCost * tipPercent / 100);
tax = (mealCost * taxPercent / 100);
答案 4 :(得分:0)
你的问题与这两行有关:
tip=(mealCost*(tipPercent/100));
tax=(mealCost*(taxPercent/100));
这些定义整数之间的操作并返回整数。强制转换为double或将100声明为double:100d