I am making a calculator app as my first self assigned project using Visual basic, and I want it to continue accepting numbers to calculate possibly up to a hundred different numbers, and I don't want to have to make a variable for all of them(not to even mention if that would work). The way I see it is that every different number entered into the calculator between operators would be stored as a different variable. Is this possible? maybe a work around? maybe even a better way to do it! any help is appreciated! thanks!
If you use a While loop you can do this fine.
All you need to do is set variables for Multiply, Subtract, Divide, and Add which should be as Booleans and declare FinalAnswer and Input as Dec.
Do something like:
While VariableToShowIfACalculateButtonIsPressedOrNot = False
If Multiply = True Then
FinalAnswer = FinalAnswer * Input
ElseIf Divide = True Then
FinalAnswer = FinalAnswer / Input
ElseIf Add = True Then
FinalAnswer = FinalAnswer + Input
ElseIf Subtract = True Then
FinalAnswer = FinalAnswer - Input
End If
Loop
For whenever you input another number. Have this assigned to a separate button called 'AddNumber' or something.
As soon as you want to output the total of all of these then you can have another button called 'Calculate' or something which could have something like:
FinalAnswer = TextBoxAnswer.txt
You could also have this set at the bottom of the While loop which would give you a running total of all the numbers you have input so far.
Hope this makes sense!
Related
how to see a change in a value in the following looping
Dim one, two, three, four, five as integer;
two = 0;
three = 0;
four = 0;
five = 0;
for one = 0 to 10
if ((one Mod 4) = 0) Then
two = two + (one+three)-five
End if
if ((one Mod 3) = 0) Then
three = three + (one+two)-four
End if
if ((one Mod 2) = 0) Then
four = four + (one+two)-three
End if
if ((one Mod 1) = 0) Then
five = one+ two + three
End if
Next
two = how much has it changed?
How much is the value of one when the two value changes?
This seems like a homework assignment where the purpose is NOT to see if you can produce the right answer. Rather, the purpose of this assignment is to teach you to use the Visual Studio debugger.
Therefore, the correct answer here is to create a new VB.Net Console project in Visual Studio. Then paste this code into the Main method for the new project, set a breakpoint (F9) on the first line and hit F5 to start the debugger.
When you reach the breakpoint, you can use the Locals window at the bottom of Visual Studio to check the value of each local variable, and use the F10 key to continue running just one line of code at a time.
In this way, you will be able to see when a variable changes, as well as the value of other variables at that time, and in this way answer the questions.
I got a knew problem which should be the last one from my bonus exercise. At first let's explain the rules, I have a word which is shuffle. I want my user to find this word and he has a determined number of tries.
Now I want to let the user know which char from his input (txtBox.Text) was right and which one aren't. So I tried to create a method which is able to Color a char in green If the char is correctly positionned else in red. I ask my teacher and it seems to be a hard thing to do, I tried to find the solution he gave me with richBox but it's way too hard right now, i'm struggling way to much.
So ! I think of something way simpler, or at least i thought it was simpler, I get the user input in a String and I do a loop around it, every time the correct word and the input char doesn't match I replaced it by a dot.
It doesen't work either if I try to put an index to my :
If word(i) IsNot proposition(j) Then
I'm facing an out of bound and if I try without it, it consider my string as an array of 1 and add to my listbox a single dot.
Here's my code :
Public Sub charRight&Wrong()
Dim proposition() As String = {txtInput.Text} //get the proposition from the user
Dim dot As Char = "."
Dim word() As String = {theWord} //theWord represent the right answer
For i = 0 To theWord.Length - 1
For j = 0 To proposition.Length - 1
If word(i) IsNot proposition(j) Then
proposition(j) = dot
End If
Next
Next
lboAttempts.Items.AddRange(proposition.ToArray)
End Sub
I don't really know where I'm wrong I hope you can point it out. Thanks again.
basically, I want to reverse the numbers. (in the textbox there will be only 2-digit numbers)
if I have Textbox1.text:
12
2
41
71
70
I want to display in the box (Textbox1.text)
21
2
14
17
70
Function:
Public Shared Function Reverse(num As Integer) As Integer
Dim _reverse As Integer = 0
While num <> 0
_reverse *= 10
_reverse += num Mod 10
num \= 10
End While
Return _reverse
End Function
it should work, it actually works, but I don't know how to arrange it to work in all lines.
For Each lines In TextBox1.Lines
Dim rev = Reverse(lines)
lines.Replace(lines, rev)
Next
This is a perfect example of what happens when people try to write code without knowing what the code is supposed to. What the code is supposed to do is not just the end result but the steps to get there. If you don't know what the steps are then you shouldn't be writing any code because it's unlikely that what you write will do anything useful. Code is simply an implementation of logic so you should be getting the logic down first. It doesn't take any programming experience to work out the logic because we could all do this if it was a manual process and that would be the same logic.
So, what are the steps involved?
Get the lines of the text.
Loop over the lines.
Reverse the current line.
Replace the original line with the result of reversing.
Replace the text with the complete results.
If you actually consider each of those steps, it should be obvious that you cannot use a For Each loop because that will only let you get data out of a list, not put data into it. That would make it obvious that a For loop is the right choice, because will let you get data out and put it in. Now you can write code that actually does something useful.
Dim lines = TextBox1.Lines
For i = 0 To lines.GetUpperBound(0)
Dim line = lines(i)
Dim number = CInt(line)
Dim result = Reverse(number)
lines(i) = result.ToString()
Next
TextBox1.Lines = lines
Simple stuff but, again, if you don't know what the code has to actually do, writing code to do it is a challenge. Always break the problem down into smaller parts first, so you can work on each part individually, and always work out the logic you're trying to implement - and test that logic manually - before trying to write code to implement it.
In my code,I have now realised I have to use the New Random function, my code was working before with the Randomize and then the numbers but now it comes up with loads of errors and wont even let me run the program. I think it is only a small error but I just need some help to get the final bit going
Heres the code and thanks for any help :)
I cannot get the code to work with the randomly generated number and I have to use the New Random function I cannot use randomize() Does anybody know how to help here is the code.
Dim timestook As Int32 = 1
Dim usersguess As Integer
Dim value = New Random(0 - 19)
Console.WriteLine("You have to guess this number. It is between 1 and 20. Good Luck !")
usersguess = Console.ReadLine()
'keep looping until they get the right value
While usersguess <> value
'now check how it compares to the random value
If usersguess < value Then
timestook = timestook + 1
Console.WriteLine("You're too low. Go higher ")
ElseIf usersguess > value Then
Console.WriteLine("You're too high. Go Lower.")
timestook = timestook + 1
End If
'If they are wrong the code will run again,after telling the user if they are too high or too low.
usersguess = Console.ReadLine()
End While
' Console.WriteLine("You're correct. Well Done")
If usersguess = value Then
Console.WriteLine("You took,{0}", timestook)
End If
Console.ReadLine()
End Sub
You'll want to do some googling on how to use random numbers. Your problem is that you aren't creating a Random object to handle the random number generation.
Here's how you can fix your code:
Dim randNumGen As New Random() 'Create Random object
Dim value As Integer = randNumGen.Next(0, 20) 'set value equal to a new random number between 0-19
Please note that this code could be further refactored for readability and simplicity (like changing timestook = timestook + 1 to timestook += 1 and selecting better variable names like numberOfGuesses as opposed to timestook, etc.
The expression New Random(0-19) does not do at all what you think it does, name it does NOT return an integer. Instead, it creates an instance of a Random object, which is a type that knows how to create new random values. The 0-19 part of the expression is the seed for the Random object's constructor, and is the same as just passing the value -19.
This looks like it's either homework or personal practice, so I feel like you will be better served in this case with a separate example using the Random type for reference than you would if I fixed the code sample in the question for you:
Dim rnd As New Random()
For i As Integer = 0 To 10
Console.WriteLine(rnd.Next(0, 20))
Next i
It's also worth mentioning here that you typically only want one Random object for your entire program, or at least only one Random object for each logical part of your program. Creating new Random objects resets the seeds, and for best results you want to follow the same seed on subsequent calls to the same instance for a while.
I have given each checkbox item on my application a value. I have group of checkboxes and I need to add up their total value base on user selection. I am using this code but it's not working for sum. I do not want to use if a.checked then add this elseif this checked add that kind of way.
Dim abe As CheckBox = groupAlumni.Controls.OfType(Of CheckBox)().Where(Function(r) r.Checked = True).Sum()
MsgBox(abe.Tag)
.sum part is giving an error. How can I achieve this?
Option 2
Also I am thinking I could change checkbox names to cb0 to cb15 and create a while loop and check them one by one to see which ones are selected and add up their values. How can I change cb number during while loop?
I wrote this but I really need cb0 to be cb1,cb2 and continue until cb15 during the loop.
If anybody answers either option it's fine for me. Thank you!
Dim counter As Integer = 0
Dim cbScore As Integer = 0
While counter < 15
If cb0.Checked Then
cbScore += cb0.Tag
End If
counter += 1
End While
If you only need the sum of the values, try this:
cbScore = groupAlumni.Controls.OfType(Of CheckBox)().Where(Function(r) r.Checked).Sum(Function(r) CInt(r.Tag))
Removed due to not compiling and incompatible methods.