I wrote the following VBS
dim a,b,c
a=inputbox("a","please input")
b=inputbox("b","please input")
c=a+b
msgbox(c)
But the final result is 11. I know that the "+" on the second-to-last line is definitely not used as an operator. How should I write this code correctly?
You will definitely be disdainful, cut, such a simple program, who won't? Then please don’t read down first, write this program independently, and see if it’s right. Note that my title is adding two numbers, not two integers.
There are two satisfactory answers, one given by my friend:
dim a,b,c
a=inputbox("a","please input")
b=inputbox("b","please input")
c=CInt(a)+CInt(b)
msgbox(c)
Another one is given by someone with the nickname "garbled code":
dim a,b,c
a=int(inputbox("a","please input"))
b=int(inputbox("b","please input"))
c=a+b
msgbox c
Obviously, one uses the CInt function and the other uses the Int function.
CInt Function
When the fractional part of a value is exactly 0.5, the CInt function rounds to the closest even number. For example, 0.5 rounds to 0, 1.5 rounds to 2, and 3.5 rounds to 4.
CInt differs from the Fix and Int functions, which truncate, rather than round, the fractional part of a number.
CInt is different from Fix and Int functions that delete the decimal part of a numeric value, but uses rounding. When the fractional part is exactly equal to 0.5, CInt always rounds it to an even number closest to that number. For example, 0.5 rounds to 0, and 1.5 rounds to 2, and 3.5 rounds to 4.
Int, Fix Functions
Both Int and Fix remove the fractional part of number and return the resulting integer value.
The difference between Int and Fix is that if number is negative, Int returns the first negative integer less than or equal to number, whereas Fix returns the first negative integer greater than or equal to number. For example, Int converts -8.4 to -9, and Fix converts -8.4 to -8.
Both the Int and Fix functions delete the decimal part of the number parameter and return the result expressed as an integer. The difference between Int and Fix functions is that if the number parameter is negative, the Int function returns the first negative integer less than or equal to number, while the Fix function returns the first negative integer greater than or equal to the number parameter. For example, Int converts -8.4 to -9, while the Fix function converts -8.4 to -8.
One is rounding and the other is deleting the decimal part. Which one is more clever?
In fact, neither of them is very clever. Although inputting the integer (two 1s) given by the questioner can output the correct answer; if the input is a decimal, neither program obviously will output the correct answer.
Here is an answer I think is more "smart":
Dim a,b,c
a = InputBox("a","please input")
b = InputBox("b","please input")
c = 0 + a + b
MsgBox c
That's right, just an extra 0. I believe you already know the difference between + sign and & sign in VBS, but I will simply copy the document:
Although it can also be used+Operators concatenate two strings, but you should still use&Operators perform string concatenation to avoid confusion and provide easy-to-understand code.
This is because in use+When operators, it may be impossible to determine whether to do addition or string concatenation.
The basic subtype of the expression determines the+The operations performed by the operator are shown in the following table:
if | but |
Both expressions are numeric values | Add up |
Both expressions are strings | connect |
One expression is a numeric value and the other is a string | Add up |
The return value of the InputBox function is a subtype of string. If you use the + sign directly, the effect is the same as the & sign, that is, the string is connected. However, if you add 0 in front of it, the effect will be different, that is, the third case in the above table will become the effect that the plus sign should have.
Of course, for the sake of rigorous program, I should also use the IsNumeric function to determine whether the input is a number. For simplicity, I will omit it here.
Original text:/programming/