且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我的小费/总费用计算器不能正常工作?

更新时间:2023-11-09 08:40:34

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



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

在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.