Will you please give me a gide line on how to solve my current problem. I'm not sure how to put this in practice.
I have a counter which increase by one in a for statement
I want to add a if statment that needs to do the following:
Dim count as decimal = 1
For i As Integer = 1 To 400 - 1
If count = 3 or count = 6 or count = 9 or count = 12 ..and on and on
'All the numbers that mathes the count
Else
'All the numbers that does not match
End if
count += 1
Next
I want a simpaler method on how to write the If count = 3 or count = 6 and so on
If the count should be dividable by 3 without a rest (as it seems to be the case), you can use the Mod operator: Documentation
The Mod operator will divide 2 numbers and will return the remaining, so e.g 14 Mod 3 will be 2. So the only check, you need to do, is if count Mod 3 = 0 like:
Dim count as decimal = 1
For i As Integer = 1 To 400 - 1
If count Mod 3 = 0 then
'All the numbers that mathes the count
Else
'All the numbers that does not match
End if
count += 1
Next
1) Why do you have i and count which appear to always be the same value?
2) Two possible solutions: either the Mod operator as others have noted, assuming you actually want every third number, or:
For i As Integer = 1 To 400 - 1
Select Case i
Case 3,6,9,12,15....
'Do stuff here for matching
Case Else
'All the numbers that does not match
End Select
Next
Modulus is your friend.
number1 Mod number2
if count MOD 3 = 0
http://msdn.microsoft.com/en-us/library/se0w9esz(v=vs.90).aspx
I'm not sure about syntax, but you need to use Mod operator:
Dim count as decimal = 1
For i As Integer = 1 To 400 - 1
If (count Mod 3) = 0
'All the numbers that mathes the count
Else
'All the numbers that does not match
End if
count += 1
Next
Related
I need some help getting this code to work. I need the code to calculate the first 100 even and odd numbers. The code below shows the even portion, but I assume they both will work the same way, just with different numbers. I know that the first 100 even numbers are 2 to 200 or 0 to 200?, and the first 100 odd numbers are 1 to 199. (I also need to use while loops, would I use a separate while loop to determine how the code calculate the sum?)
here is the code.
Dim sum, count As Integer 'the sum and count are changing and they must be a whole number.
Const num As Integer = 2
'now we must deveolp a while loop/equation that adds every number lower or equal to 100 together.
While count Mod 2 <= 200 'while the count is smaller or equal to 100,
count =
sum += count 'The sum will be add or equaled to the count
count += 1 'The count will be added to equaled to 1
End While
Console.WriteLine("The Sum of the first 100 even numbers is: {0}!", sum) 'This is the text the user sees, which tells them that the sum of the first 100 numbers will be 5050.
Console.ReadLine()
End Sub
As usual the math portion is giving me trouble. I feel like I have the right idea, but I cant execute it properly.
Dim sumEven, sumOdd As Integer
Dim even = 2
Dim odd = 1
While even <= 200 AndAlso odd <= 200
sumEven += even
sumOdd += odd
even += 2
odd += 2
End While
Console.WriteLine("The Sum of the first 100 even numbers is: {0}!", sumEven)
Console.WriteLine("The Sum of the first 100 odd numbers is: {0}!", sumOdd)
I'm having a certain layout like this:
0
0
0 5
1 6
1
0 7
1 8
1
0
0 9
1 10
1
0 11
1 12
Above is a list with combinations from 000, 001, 010, 011 to 111. The combinations all have a certain value, from 5 to 12.
(The enters are new rows, the spaces are indentlevels of cells, the 5, 6, 7, etc. are in a new column. The 0's and 1's are all in the same column.)
Now I need to have the sum of all the values in which the first 0/1 is 1, the sum of all the values in which the second 0/1 is 1, and the same for the last value. The results in this case must be: 42, 38 and 36
I can't find out how to programm this properly. I was hoping for something like this:
While not Sheets("Sheet 1").Cells(j, 1).indentlevel(2).Value = 0
sum = sum + cells(j,2)
j = j + 1
Wend
But obviously this doesn't work. I can't program this all out without loops, because the codes can be up to 5 didgets (ex. 01101)
Have you thought of using a for loop? Also I think you are using the IndentLevel syntax incorrectly.
With ThisWorkbook.Sheets("Sheet 1")
set rng = Range(.cells(1,1), .cells(3,10)) ' Change this to match your range
End With
For each c in rng
If c.IndentLevel = 2 Then
sum = sum + c
End If
Next c
This doesn't need VBA. The real problem is an inappropriate data format. Update your convention such that each indentation level is moved to a separate column. You'll then be able to proceed with simple Excel formulas.
So, we are creating a game in VB.net based off the game, Farkle. Basically, you roll an amount of dice and depending on what you keep is your score. We have eight dice. Here's an example, say you roll 3 "3"s and you want to score them, a three of a kind. We want to check all the dice together to see if we do have three 3's. We have figured out the two of a kind, but the three of a kind we cannot.
For l = 0 To 5
For o = 1 To 6
For q = 2 To 7
If (DieScore(l) = DieScore(o) And DieScore(l) = DieScore(q)) Then
PlayerScore = 200 + PlayerScore
End If
Next
Next
Next
This is our checking for three of a kind on the dice. If it is true we add 200 to the score. The DieScore(x) refer to the dice. Where are we going wrong?
You really just need to loop and count how many times that number of pips (spots) appears in the array.
Dim count As Integer = 0
For pips As Integer = 1 To 6
count = 0 ' reset count for each Pip iteration
For n As Integer = 0 To DieScore.Length - 1
' If operator version:
count += If(DieScore(n) = pips, 1, 0)
' If Statement version:
'If DieScore(n) = pips Then
' count += 1
'End If
Next
' scoring
Select Case count
Case 2 ' pair
playerscore += 50
Case 3 ' trips
playerscore += 200
If pips = 6 Then
playerscore += 25 ' extra bonus for 666 (example)
End If
Case 4 ' quads
playerscore += 350
' etc
End Select
Next
Not for nothing, but these kinds of logic issues are easy to find using the Debugger. Plus, you will learn a lot about how code executes.
For starters, please learn to use different, more descriptive variable names than l and o, that are easily confused with 1 and 0. Some famous bugs have been caused by doing things like that.
One thing you can do is simply count how many dots or pips there are in a roll of the dice and store that in an array.
' index 0 = 1 spot, 5 = 6 spots.
Dim pipsCount(6) as Integer
' This counts the number of dice for each possible "pips"
For dieIndex as Integer = 0 To DieScore.Length - 1
' Increment (the -1 is because index starts at 0)
pipsCount(DieScore(dieIndex)-1) += 1
Next
Now that you have the number of dice that landed headsup with a given number of pips, you can do a number of different things with that.
' You can easily find out now how many sixes were thrown:
Dim numberOfSixes As Integer = pipsCount(6-1)
' Or score pairs, trips, quads, ...
For pipsIndex As Integer = 0 To 5
Select Case pipsCount(pipsIndex)
Case 2
PlayerScore += 50
Case 3
PlayerScore += 200
' ... etc
End Select
Next
' Or count the length of a straight
Dim straightLength As Integer = If(pipsCount(0) > 0, 1, 0)
Dim longestStraight As Integer = straightLength
For pipsIndex As Integer = 1 To 5
If pipsCount(pipsIndex) > 0 Then
straightLength += 1
Else ' straight ended
If straightLength > longestStraight Then
longestStraight = straightLength
End If
straightLength = 0
End If
Next
I was wondering how to colour the first 8 rows of a datagridview. I have managed to sort the values in descending order and I wish to have the first 8 rows coloured to highlight the top 8 to the user, and I'm not sure how to go about doing this.
Dim count As Integer
For count = 0 To datagridsort.RowCount - 1
Do
datagridsort.Rows(0).Cells(0).Style.BackColor = Color.Coral
datagridsort.Rows(0).Cells(1).Style.BackColor = Color.Coral
Loop Until count = 8
Next
In the code you posted in your comment, you were never using the count variable. You were only updated the first row every time. Try it like this:
For i As Integer = 0 To Math.Min(datagridsort.RowCount - 1, 7)
For j As Integer = 0 To datagridsort.ColumnCount - 1
datagridsort.Rows(i).Cells(j).Style.BackColor = Color.Coral
Next
Next
What I'm trying to do is loop through the numbers 0 to 100 and check which ones are divisible by 5 and have that print to console, this is what I've done so far:
Module Module1
Sub Main()
For i = 0 To 100
If i / 5 = Then
Console.WriteLine(i)
End If
Next
Console.ReadLine()
End Sub
End Module
I'm wondering if I'm able to have an If statement check with a range of numbers for example like this:
If i / 5 = 0 to 19 Then
Is that possible?
Thank you in advance!
like this: If i / 5 = 0 to 19 Then ...
You can use the Step argument in the For-loop:
For i As Int32 = 0 To 100 Step 5
Console.WriteLine(i)
Next
If you want to check if a number is divisible by another number use the Mod operator:
If i Mod 5 = 0 Then
Console.WriteLine(i)
End If
Use the MOD operator. It divides the numbers but returns only the reminder.
You can check if a number is divisible by 5 if number MOD 5 = 0
So your code can be like this:
For i = 0 To 100
If i Mod 5 = 0 Then
Console.WriteLine(i)
End If
Next