我的小费/总费用计算器不能正常工作? [英] My tip/total cost calculator doesn't work right?

查看:109
本文介绍了我的小费/总费用计算器不能正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初将它的数字(账单的20%)和税(0.06)计算在餐的总数中,然后确认它将花费多少。今天我想让它计算税,并让小费是可选的(如果在快餐店。)我使用if / else if语句来做这个,但它似乎显示小费无论如何。任何帮助将不胜感激,但请尝试解释它,因为我相对较新的编码;这可能意味着这是一个简单的错误。代码如下。谢谢。



  var  meal = prompt( 你的用餐多少钱?

var tax = 0 06
var tip = 0 20

< span class =code-keyword> var withTax = meal +++ meal * tax
confirm( 你的总数是 + + withTax)
// 问题从此处开始,如果
var yesNo = prompt( 您是否要提供提示?
// 由于某种原因,它始终显示提示,如果
if (yesNo = yes){
var total = withTax +++ withTax * tip

confirm( 您的总数(加上提示)为 + + total)
}
// 以下代码无法运行?
else if (yesNo = no){

confirm( 好的,只需 + + withTax)
}





我尝试了什么:



我试过移动if和else if if around around ,并多次看了它。我在一个名为https://jsfiddle.net/的网站上运行代码,如果有帮助的话。

解决方案

你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到有一点它会停止你所期望的。

在Visual Studio 2010中掌握调试 - 初学者指南 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]



你为什么使用运算符 +++ ?它应该做什么(为你)?

要进行比较,请使用 == intead of = 用于分配。

JavaScript教程 [ ^ ]



应该应用更多简化

I originally had it figure in the tip (20% of bill) and tax (0.06) into the total of the meal and then confirm how much it would cost. Today I wanted to make it calculate the tax, and have the tip be optional (if at a fast food restaurant.) I used an if/ else if statement to do this, but it seems to display the tip no matter what. Any help would be appreciated, but please try and explain it simply as I am relatively new to coding; which probably means it is a simple mistake. The code is below. Thank you.

var meal = prompt("How much was your meal?")

var tax = 0.06
var tip = 0.20

var withTax = meal +++ meal * tax
confirm("Your total is" + " " + withTax)
  //problem starts here and ends at the else if
var yesNo = prompt("Would you like to include a tip?")
  //for some reason it always displays tip and won't run the else if
if (yesNo = "yes") {
  var total = withTax +++ withTax * tip

  confirm("Your total(plus tip) is" + " " + total)
}
//code below doesn't run?
else if (yesNo = "no") {

  confirm("Okay then, just" + " " + withTax)
}



What I have tried:

I have tried moving the if and else if around, and have looked it over several times. I am running the code in a website called https://jsfiddle.net/ if that helps.

解决方案

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

Why are you using the operator +++ ? and what should it do (for you) ?
To do comparison, use == intead of = which is for assignation.
JavaScript Tutorial[^]

More simplifications should be applied.


这篇关于我的小费/总费用计算器不能正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆