How to write a VB net console app using loops for this kind of output:
$$$$$
$$$$
$$$
$$
$
'Declaring the variables
Dim DollarSign As String
DollarSign = "$"
For counter = 5 To 1 Step -1
Console.WriteLine(DollarSign)
Next
the output shows like this:
$
$
$
$
$
as well this following output:
1$$$$
12$$$
123$$
1234$
I can't figure this out. What's going on here?
Dim DollarSign as String = "$"
For counter = 5 To 1 Step -1
Console.WriteLine(New String(DollarSign, counter))
Next
Right now your code is looping from 5 to 1 and printing the dollar sign once during each iteration.
It sounds like you want the dollar sign to be repeated based on the current index of the loop.
If that's the case then you can create a new string and pass in the dollar sign and the current index:
Dim dollarSign As Char = "$"c
For counter = 5 To 1 Step -1
Console.WriteLine(New String(dollarSign, counter))
Next
Fiddle: https://dotnetfiddle.net/Ok8NSD
Another approach using NESTED for loops:
Dim numRows As Integer = 5
For row As Integer = numRows To 1 Step -1
For col As Integer = 1 To row
Console.Write("$")
Next
Console.WriteLine()
Next
Console.ReadKey()
Output:
$$$$$
$$$$
$$$
$$
$
Second version:
Dim numRows As Integer = 5
For row As Integer = numRows To 1 Step -1
For col As Integer = 1 To ((numRows + 1) - row)
Console.Write(col)
Next
For col As Integer = 1 To (row - 1)
Console.Write("$")
Next
Console.WriteLine()
Next
Console.ReadKey()
Output:
1$$$$
12$$$
123$$
1234$
12345
There are just so many ways to do this:
Dim output = "$$$$$"
Do While output.Length > 0
Console.WriteLine(output)
output = output.Substring(1)
Loop
That gives:
$$$$$
$$$$
$$$
$$
$
Related
Im trying to fill multiple matrices according to inputs from a userform.
Right now i have to create 5 matrices but i want this to be able to change in the future, so i want to create the variables "on the fly" in my loop. I basically want to Create Matrix1, Matrix2, Matrix3 ... Matrixn, but i can't figure how to do it. Any Ideas ?
Following you can see the script --> The two last lines are totally wrong (i know it) but i just added them in hope that you understand what i want to do :)
PS: And most important i should be able to then look to Matrix1(i,j),..., Matrixn(i,j) values easily
Looking Forward your help <3
'FicheCalcul ----------------------------------------------------------------------------------------------------
Dim Temp4 As String
Dim Temp5 As String
Dim MatrixTemp(3 - 1, 5 - 1) As Double 'm-1,x-1
For i = 1 To 5 'n
For j = 0 To 3 - 1 'm-1
For k = 0 To 5 - 1 'X-1
Temp4 = j & "C" & i
Temp5 = Temp4 & k
If UserForm1.Controls(Temp5) = False Then
MatrixTemp(j, k) = ""
Else
If UserForm1.Controls(Temp5) = True Then
MatrixTemp(j, k) = k - 2 ' -2 -1 0 1 2
End If
End If
Next k
Next j
Dim ("Matrix"&i)(3-1,5-1) As Double
("Matrix"&i) = MatrixTemp
Next i
You cannot have variable variable names. Therefore you need to use arrays.
Option Explicit
Sub example()
Dim MatrixTemp(4, 5) As Double
'do your matrix stuff here
MatrixTemp(0, 0) = 1
MatrixTemp(0, 1) = 2
MatrixTemp(0, 2) = 3
Dim Matrix(4) As Variant 'create an array of 5 matices
Matrix(0) = MatrixTemp 'fill your temp matrix into the first matrix
Debug.Print Matrix(0)(0, 2) 'outputs 3
End Sub
I'm working on extracting twitter/facebook style mentions from a textbox.
So far, here's my code:
Dim a As String = TextBox1.Text + " "
Dim b As Char() = a.ToCharArray
Dim c As String
Dim l As Integer = TextBox1.Text.Length
Dim temp As Integer = 0
Dim nex As Integer = a.IndexOf(" ")
For i = 0 To l - 1
If b(i) = "#" Then
temp = 1
ElseIf temp = 1 Then
temp = 2
End If
If temp = 2 Then
c = a.Substring(i, nex).Trim() 'nex needs be replaced with next space on 2nd or nth loop
MsgBox(c)
temp = 0
nex = a.IndexOf(" ") + nex
End If
Next
Now this works great if the entered text is- #one #twwo #three. (If
the next strings are greater in length.) But doesn't work elsewhere.
Also, there is probably going to be content between two #mentions so I'm not willing to change the b(i).
I'm sure there's a much more efficient way to do this.
Thanks!!
This is a job for regex. The pattern #\w+ should do nicely.
For Each m As Match In Regex.Matches("#testing this is a #test", "#\w+")
Console.WriteLine(m.Value)
Next
Will print out #testing and #test.
This pattern basically means "Find everything that starts with an '#' followed by one or more 'word characters'."
Regex is a very powerful tool for searching strings, you can read more about it on MSDN.
I am trying to print reverse of number using VB.NET console application and I have variable of type Integer. when I give number 651 as input it prints 1561. I have write code as
Sub Main()
Dim no, rev, temp As Integer
Console.WriteLine("enter the no")
no = CInt(Console.ReadLine())
rev = 0
temp = no
While temp > 0
Dim t As Integer
t = temp Mod 10
rev = rev * 10 + t
temp = temp / 10
End While
Console.WriteLine("Reverse number=>"+rev.ToString())
Console.ReadKey()
End Sub
when I enter number 123 then it gives proper output as 321, but when i give 678,it give output 8761 or other garbage value, please suggest me suggetion
You are getting such result because,
when you assign a division result to an integer value it will
automatically get rounded to the next higher integer.
For example Dim no As Integer = 68 / 10 will give you result as 7,
if you are coding with Option Strict On then this casting is not allowed.
The best way is suggested by Bjørn-Roger Kringsjå you can simply reverse a number and print it.
Console.Write("Reverse of {0} is : ", no.ToString().Reverse())
or else you can follow the following steps:
Console.WriteLine("enter the no")
Dim no As Integer = CInt(Console.ReadLine())
Console.Write("Reverse of {0} is : ", no)
While no > 0
Console.Write(no Mod 10)
no = Math.Floor(no / 10)
End While
Console.ReadKey()
Just use \ instead of / and your work is done.
I Have about 10 (DatagridView Count may varies As per User Selected Files From 2 to 10) Datagridview ,So How can i find common value from all Datagridviews ??
Comment If you need more brief details
Below is mine but It find common from 2 -2 datagridviews
For i As Integer = 1 To dgvCont
For j As Integer = 0 To Main.DGVM(i).Rows.Count - 1
For Each Val As DataGridViewRow In Main.DGVM(i + 1).Rows
If Val.Cells(0).Value = Main.DGVM(i).Rows.Item(j).Cells(0).Value Then
Dim cm As String = Val.Cells(0).Value
If cm = "" Then
Else
Analysis.lvCmn.Items.Add(Val.Cells(0).Value)
End If
End If
Next
Next
Next
I understand that you want to set two nested loops accounting for an undetermined number of elements (items in an array of DataGridView, I presume), performing the checks you want:
For count1 As Integer = 1 To dgvCont 'Assuming indices from 1 to dgvCont
For row1 As Integer = 0 To Main.DGVM(count1).Rows.Count - 1
If (Main.DGVM(count1).Rows(row1).Cells(0).Value Is Nothing) Then Continue For
Dim val1 As String = Main.DGVM(count1).Rows(row1).Cells(0).Value
Dim found As Boolean = False
For count2 As Integer = 1 To dgvCont 'Assuming indices from 1 to dgvCont
If (count2 = count1) Then Continue For
For row2 As Integer = 0 To Main.DGVM(count2).Rows.Count - 1
If (Main.DGVM(count2).Rows(row2).Cells(0).Value Is Nothing) Then Continue For
Dim val2 As String = Main.DGVM(count2).Rows(row2).Cells(0).Value.ToString()
If val1 = val2 Then
Dim cm As String = val1
If cm = "" Then
Else
Analysis.lvCmn.Items.Add(val1)
End If
found = True
Exit For 'By assuming that you want to stop searching after finding a match
End If
Next
If (found) Then Exit For 'By assuming that you want to stop searching after finding a match
Next
Next
Next
Your code is not too clear (neither what you want); but this should give you a good enough start to carry out the implementation you are looking for. Bear in mind that this code (like yours) only considers one column (first one); in case of wanting to iterate through all the columns, you would have to add further nested loops accounting for that.
it doesnt like my code:
Dim n As Integer
Dim flag As Boolean
Dim i
Dim x() As Integer
n = InputBox("How many numbers do you want to be sorted?")
For i = 1 To n - 1 Step 1
x(i) = InputBox("Please enter a record")
Next i
I want to put values into x()
You need to use a ReDim to specify the size of x as soon as you know it (ie after your first InputBox):
n = InputBox("How many numbers do you want to be sorted?")
ReDim x(n - 1)
Also, your For loop will be simpler to handle if it's zero based as in:
For i = 0 To n - 1 Step 1