I'm trying to create a leveling system in QB64 Txt Based Adventure - levels

What I want to happen is whenever expertotal% is equal to levelup%, I want to add 1 to level%. I've tried level% + 1, but I get a syntax error.
Then whenever level% goes up, I want to multiply levelup% and experience% to be multiplied by 1.25.
Here is my code:
do
sub lvl
level% = 1
levelup% = 50
experience% = 5
ExperTotal% = 0
if Expertotal% = levelup% then 'something here to add 1 to level%
while Expertotal% = levelup%
' multiply them both by 1.25
wend
loop
end sub

Rewriting your code somewhat I have come to this:
REM What I want to happen is whenever expertotal% is equal to levelup%,
REM I want to add 1 to level%.
REM I've tried level% + 1, but I get a syntax error.
REM Then whenever level% goes up,
REM I want to multiply levelup% and experience% to be multiplied by 1.25.
level% = 1
levelup% = 50
experience% = 48
ExperTotal% = 48
DO
COLOR 15
PRINT "Increase exp(y/n)";: INPUT x$
IF LCASE$(x$) = "y" THEN
GOSUB lvl
COLOR 14
PRINT "level="; level%
PRINT "experience="; experience%
ELSE
EXIT DO
END IF
LOOP
COLOR 7
END
lvl:
' increase experience total
ExperTotal% = ExperTotal% + 1
' compare experience to levelup
IF ExperTotal% = levelup% THEN
' add 1 to level when experience equal
level% = level% + 1%
' multiply levelup and experience
levelup% = levelup% * 1.25
experience% = experience% * 1.25
COLOR 12
PRINT "level increased.."
END IF
RETURN
END

Related

Loop through a string to find the odd numbers VB

I am looping through a card number finding all the odd numbers and multiplying them by the card digits. Its kind of hard to explain. I am having trouble multiplying the odd number and the card number. Here's an example my teacher gave me. You multiply card number 1 and and odd number 1 and so forth. I am not getting any errors, it just kind of freezes.
Sums
Card #: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
Multiples 1 2 3 4 5 6 7 8
Evens: 2 4 6 8 0 2 4 6 32 =Sum 1
Odds: 1 6 15 28 45 6 21 40 162 =Sum 2
Sum 3: 194
194 =Sum 3
Step 4: =1+9+4 = 14
= 1 + 4 = 5 = check digit
Public Class Payment
Private Sub OK_Click(sender As Object, e As EventArgs) Handles OK.Click
Dim Sum1 = 0
Dim Sum2 = 0
Dim Sum3 = 0
Dim ready As Boolean
Dim ccnumb = CardNumber.Text
Format(CardNumber.Text, "################")
Dim exp = Mid(ExpDate.Text, 1, 3)
Dim checkdigit = 0
If FullName.TextLength = 0 Or cardtype.Text.Length = 0 And ccnumb.Length <= 16 Or exp.Length = 2 Then
MessageBox.Show("Please enter all credit card information before proceeding.")
ready = False
Else ready = True
End If
If ready = True Then
For Each num As Char In ccnumb
If CInt(CStr(num)) Mod 2 <> 0 Then
Sum1 += CInt(CStr(num)) * CInt(CStr(num)) Mod 2 <> 0
Else
Sum2 += CInt(CStr(num))
End If
Next
Sum3 = Sum1 + Sum2
Do While Sum3 > 10
For j = 0 To Sum3.ToString.Length - 1
For k = 1 To Sum3.ToString.Length - 1
Sum3 = j + k
Next
Next
Loop
Do While exp.Length > 1
checkdigit = Mid(ExpDate.Text, 1, 1) + Mid(ExpDate.Text, 1, 2)
Loop
If Sum3 = checkdigit Then
MessageBox.Show("Congratulations! Your payment was successful.")
CustInv.Show()
Else MessageBox.Show("The checkdigit," & Space(1) & Sum3 & Space(1) & "does not match the month code," & Space(1) & checkdigit & "." & Space(1) & "Please reenter your card information.")
End If
End If
End Sub
"it kind of freezes" is lay speak for "my code enters an infinite loop".
This looks suspicious:
Do While Sum3 > 10
For j = 0 To Sum3.ToString.Length - 1
For k = 1 To Sum3.ToString.Length - 1
Sum3 = j + k
Next
Next
Loop
To enter the loop, Sum3 must be greater than 10. For the loop to exit, Sum3 must not be greater than 10, but your code only increments Sum3, so Sum3 can only stay greater than 10.
This means that once entered, this loop is infinite.
If changing outer loop to inside loop doesn't help you
Try this
Do While Sum3 > 10 and j < sum3 ' adding j < sum3 might stop the loop
For j = 0 To Sum3.ToString.Length - 1
For k = 1 To Sum3.ToString.Length - 1
Sum3 = j + k
Next
Next
Loop

How to Loop through a If condition untill the user enter a required value, in Visual Basic

Console.WriteLine("Please Enter the number")
Dim number As Integer = Console.ReadLine()
If (number=< 40) Then
number = number* 10
ElseIf (number=< 150) Then
number= number* 15
Else
number= number* 26
End If
Console.WriteLine(number)
Dim total As Integer
Dim vALUE As Integer
Console.WriteLine("Please, type 1 for x . Type 2 for y. Type 3 z")
vALUE = Console.ReadLine()
If vALUE = 1 Then
Console.WriteLine("x")
total = number* (106 / 100)
ElseIf vALUE = 2
Console.WriteLine("y")
total = number* (112 / 100)
ElseIf uSERvALUE = 3
Console.WriteLine("z")
total = number* (116 / 100)
Else
Console.WriteLine("Sorry please re-enter the value")
vALUE = Nothing
End If
End While
Please tell me how to repeat the if condition. In the else line I have displayed to the user to re-enter the value. Therefore I need to repeat the if condition until the value is 1 or 2 or 3.Please explain how to do I'm a newbie.
You can use infinite While-loop ,and put "Exit While" on the end of conditional statement you agreed , so there are few ways out from infinite loop.
For example , on your code:
Console.WriteLine("Please Enter the number")
Dim number As Integer = Console.ReadLine()
If (number=< 40) Then
number = number* 10
ElseIf (number=< 150) Then
number= number* 15
Else
number= number* 26
End If
Console.WriteLine(number)
Dim total As Integer
Dim vALUE As Integer
'infinite loop until user input 1,2 or 3
While True
Console.WriteLine("Please, type 1 for x . Type 2 for y. Type 3 z")
vALUE = Console.ReadLine()
If vALUE = 1 Then
Console.WriteLine("x")
total = number* (106 / 100)
Exit While 'condition matched , break from While
ElseIf vALUE = 2
Console.WriteLine("y")
total = number* (112 / 100)
Exit While 'condition matched , break from While
ElseIf uSERvALUE = 3
Console.WriteLine("z")
total = number* (116 / 100)
Exit While 'condition matched , break from While
Else
Console.WriteLine("Sorry please re-enter the value")
vALUE = Nothing
End If
End While
'do further more you need

Max Value in VB?

Here is the code I'm working on for a project.
What I can't figure out is the max value and how to get it?
Even if I change it, it seems not to affect the outcome of the counter?
Can anyone lead me in the right direction on what to do for this?
Thanks!
Module
Module1
Dim counter As Integer
Const Max_Value As Double = 22
Sub Main()
Console.WriteLine("Are you ready to see which letter is 22? Press Enter")
Console.ReadLine()
For counter As Integer = 0 To Max_Value Step 1
 
Console.Write("a")
Console.WriteLine("0")
counter = counter + 1
Console.Write("b")
Console.WriteLine("1")
counter = counter + 1
Console.Write("c")
Console.WriteLine("2")
counter = counter + 1
Console.Write("d")
Console.WriteLine("3")
counter = counter + 1
Console.Write("e")
Console.WriteLine("4")
counter = counter + 1
Console.Write("f")
Console.WriteLine("5")
counter = counter + 1
Console.Write("g")
Console.WriteLine("6")
counter = counter + 1
Console.Write("h")
Console.WriteLine("7")
counter = counter + 1
Console.Write("i")
Console.WriteLine("8")
counter = counter + 1
Console.Write("j")
Console.WriteLine("9")
counter = counter + 1
Console.Write("k")
Console.WriteLine("10")
counter = counter + 1
Console.Write("l")
Console.WriteLine("11")
counter = counter + 1
Console.Write("m")
Console.WriteLine("12")
counter = counter + 1
Console.Write("n")
Console.WriteLine("13")
counter = counter + 1
Console.Write("o")
Console.WriteLine("14")
counter = counter + 1
Console.Write("p")
Console.WriteLine("15")
counter = counter + 1
Console.Write("q")
Console.WriteLine("16")
counter = counter + 1
Console.Write("r")
Console.WriteLine("17")
counter = counter + 1
Console.Write("s")
Console.WriteLine("18")
counter = counter + 1
Console.Write("t")
Console.WriteLine("19")
counter = counter + 1
Console.Write("u")
Console.WriteLine("20")
counter = counter + 1
Console.Write("v")
Console.WriteLine("21")
counter = counter + 1
Console.Write("w")
Console.WriteLine("22")
Console.ReadLine()
Next
Console.WriteLine()
Console.WriteLine("Summary of the Count: {0} So we've counted to 22{0} The Winning letter is W{0} Which is great because that's the letter of my first name{0} W also stands for War Eagle!", _
Environment.NewLine)
  
Console.WriteLine("Press Enter to Exit")
Console.ReadLine()
End Sub
End
Module
I think what you are trying to accomplish is something like this:
Module Module1
Dim counter As Integer
Const Max_Value As Integer = 22
Sub Main()
Console.WriteLine("Are you ready to see which letter is 22? Press Enter")
Console.ReadLine()
For counter As Integer = 0 To Max_Value Step 1
Select Case counter
Case 0
Console.Write("a")
Case 1
Console.Write("b")
Case 2
Console.Write("c")
Case 3
Console.Write("d")
Case 4
Console.Write("e")
Case 5
Console.Write("f")
Case 6
Console.Write("g")
Case 7
Console.Write("h")
Case 8
Console.Write("i")
Case 9
Console.Write("j")
Case 10
Console.Write("k")
Case 11
Console.Write("l")
Case 12
Console.Write("m")
Case 13
Console.Write("n")
Case 14
Console.Write("o")
Case 15
Console.Write("p")
Case 16
Console.Write("q")
Case 17
Console.Write("r")
Case 18
Console.Write("s")
Case 19
Console.Write("t")
Case 20
Console.Write("u")
Case 21
Console.Write("v")
Case 22
Console.Write("w")
End Select
Console.WriteLine(counter)
Console.ReadLine()
Next
Console.WriteLine()
Console.WriteLine("Summary of the Count: {0} So we've counted to 22{0} The Winning letter is W{0} Which is great because that's the letter of my first name{0} W also stands for War Eagle!", Environment.NewLine)
Console.WriteLine("Press Enter to Exit")
Console.ReadLine()
End Sub
End Module
In your version of the code, it outputs all of the letters each time it loops. In my version of the code, it only outputs one letter each time it loops. The Select Case statement is basically a simpler way of writing a bunch of separate If statements.
However, it's silly to write a big Select Case like that. The best way to implement a loop is to have it iterate over some sort of indexed data-structure. In this case, all you need is a list of letters. The simplest way to do that is to just store all of the letters in a single string, like this:
Dim letters As String = "abcdefghijklmnopqrstuvw"
For counter As Integer = 0 To Max_Value Step 1
Console.Write(letters(counter))
Console.WriteLine(counter)
Console.ReadLine()
Next
Create seperate variable to be used in for loop. What is happening is that you are looping with variable counter and then incrementing it inside loop. This will always loop 22 times.
So declare seperate variable and loop on it.
Dim intC as integer
For intC = 0 to Max_Value step 1
counter = counter + 1
Next
You are declaring counter on Module level and as a private variable for your iteration.
Every time your for loop goes to the next step, the private counter is overwritten with a new value. You should rename it something else
For c As Integer = 0 To Max_Value Step 1
counter = counter + 1
Next
or
For c As Integer = 0 To Max_Value Step 1
counter = c
Next
you probably want this:
For c as Integer = 0 To Max_Value ' Step 1 is default so you can skip that
Console.Write(Convert.ToChar(c + 97)) ' 97 = a
Console.WriteLine(c)
Next
Update: This has not much to do with your original code but is an alternative approach
Dim maxvalue As Integer = 22
Dim counter As Integer = 0
For Each c As Char In "abcdefghijklmnopqrstuvw".ToCharArray()
counter += 1
If counter = maxvalue Then
Dim ordinal As String = "th"
Select Case counter
Case 1 : ordinal = "st"
Case 2 : ordinal = "nd"
Case 3 : ordinal = "rd"
End Select
Console.WriteLine("{0} is the {1}{2} letter in the alphabet", c, maxvalue, ordinal)
Exit For
End If
Next

Issues with For Loop in VB

For i = 1 To 5
If i = 0 Then
i = i + 1
ElseIf i Mod 2 = 0 Then
LabelEvens.Text = i
i = i + 1
Else
LabelOdds.Text = i
i = i + 1
End If
Next i
I'm making a program in VB where I have to use a for loop to sort between 2 numbers(loop limit 1 and 2) and find if they are even or odd, Then output the results to 2 labels. This loop makes sense to me, but for example when I put in 1 and 4 all it outputs is a 5 in the odd label. I guess my question is can anyone see the issue with my loop?
You don't need to add 1 to your loop variable i manually, the for loop itself does that for you behind the scenes:
For i = 1 To 5
If i Mod 2 = 0 Then
LabelEvens.Text = i
Else
LabelOdds.Text = i
End If
Next i
You'll noticed I've also removed the If i = 0 bit since i can never be zero within that loop. It ranges from one to five inclusive.
One other thing you'll need to do is to append the value to your text box. What you have at the moment is a replacement so that it'll only be set to the last value processed. Something like this should suffice:
' Initialise to empty strings '
LabelEvens.Text = ""
LabelOdds.Text = ""
' Append the values '
For i = 1 To 5
If i Mod 2 = 0 Then
LabelEvens.Text = LabelEvens.Text & "," & CStr(i)
Else
LabelOdds.Text = LabelOdds.Text & "," & CStr(i)
End If
Next i
' Remove initial comma from both '
LabelEvens.Text = Mid(LabelEvens.Text,2)
LabelOdds.Text = Mid(LabelOdds.Text,2)
Some issues in your code:
For i = 1 To 5
If i = 0 Then <-- 'I' will never be 0 since you start from 1
i = i + 1 <-- Don't manually increment since you are using a for
ElseIf i Mod 2 = 0 Then
LabelEvens.Text = i
i = i + 1 <-- Don't manually increment since you are using a for
Else
LabelOdds.Text = i
i = i + 1 <-- Don't manually increment since you are using a for
End If
Next i
Another issue you have is that if you have more than one odd number in the for range (say in a range of 1 to 10) you will only get the last number. What do you want to do in this case? Concatenate all odd numbers in a string or stop after the first one is found? Do you really need a FOR loop at all?
you can Also state
LabelEvens.Text="" 'Clear contents of the label before assigning new values
LabelOdds.Text=""
For i As Integer = 1 To 5
If i Mod 2 = 0 Then
LabelEvens.Text = LabelEvens.Text & i
Else
LabelOdds.Text = LabelOdds.Text & i
End If
Next
From Above you can replace '&' with '+' if you want the Total.

number variable won't addition by 1 and display in a label everytime the timer counts 1000ms. - vb.net

Here is my code:
number = 1
If chkFN.Enabled = True Then
If ProgressBar1.Value < 100 Then
number += 1
lblFN2.Text = number
Else
lblFN2.Text = "0"
End If
End If
i have a checkbox, progressbar and a label.
when the progress bar is lower than 100 i want the number variable to + 1 every time the timer counts 1. I've tried "X = X + 1" - it worked to a point but i need to reset the variable to "0" when the progressbar hits 100% and when i click the stop button.
while I've been typing this i've also tried:
X = X + 1
If chkFN.Enabled = True Then
If ProgressBar1.Value < 100 Then
lblFN2.Text = X + 1
ElseIf AxWindowsMediaPlayer1.playState = WMPLib.WMPPlayState.wmppsMediaEnded
Then
X = 0
lblFN2.Text = "0"
Else
X = 0
lblFN2.Text = "0"
End If
End If
But when you start the timer again it just resumes from it's last number e.g. "13" and doesn't reset to "0".
Thanks for the help guys :)
Use your first code, but add in
number += 1
to the Else condition. You need to reset the variable or it just increments it again.