Trying to ask for an integer input based on user - vb.net

Vb.net how to ask a user for an integer? That’s how many times they have to input a number and then find the largest number
Can’t think of the logic

Print("Please enter count")
Dim input As String = ReadFromUser()
Dim count As Integer = ConvertToInteger(input)
Dim numbers(count - 1) As Integer
For i = 0 To count - 1
input = ReadFromUser()
Dim n As Integer = ConvertToInteger(input)
numbers(i) = n
Next
Sort(numbers)

Related

Is there a way to add up items(numbers) in the listbox and display them in the same box in visual basic

So I'm a beginner in vb class and am wondering how I can add up all products of Modnumber(variable name) Mod 2 so that i can attach them into one single line at the bottom of my listbox. What line should i add so the program runs like the pic below?
Dim number As Integer
Dim Modnumber As String
number = InputBox("I'm kindly asking you to tell me a number", "Number")
LstNumber.Items.Clear()
Do Until number = 1
Modnumber = number
LstNumber.Items.Add(number & vbTab & Modnumber Mod 2)
'each time it devides the number by two until number = 1
number \= 2
Loop
This is what I get
This is what I'm supposed to get in my lstbox
'capture your original input to use in the final result
Dim input = InputBox("I'm kindly asking you to tell me a number", "Number")
Dim number As Integer = input
Dim modNumber As Integer 'Changed this to a number
Dim result As New StringBuilder 'Used to store the product of doing Mod 2
LstNumber.Items.Clear()
Do Until number = 0 'loop until 0 instead of 1
modNumber = number Mod 2 'Do mod outside of LstNumber add so u can retain the value
LstNumber.Items.Add(number & vbTab & modNumber)
result.Insert(0, modNumber) 'Build your result string
number \= 2
Loop
'Add your additional stuff
LstNumber.Items.Add("Therefore")
LstNumber.Items.Add($"{input} = {result.ToString}") 'Original input plus built string

Trying to shorten code in VB.NET

One of my assignments in vb.net is to make a program that takes in 15 inputted "test scores", returns the average, and then returns the corresponding letter grade. I am trying to make this as short as possible, and I remembered a code format that I can use when I am coding in Python (simple example with only three scores):
counter = 0
number = 0
test1,test2,test3 = 0,0,0
for i in [test1,test2,test3]:
print("Enter a test score")
i = int(input())
counter += i
number += 1
average = counter/number
Is there such a code format in VB.NET?
Yes.
Here is a simple sample code which asks user to input number of items and then items one by one. At the end it calculates average of numbers.
Console.WriteLine("Enter number of Items")
Dim numberOfItems As Integer
If Integer.TryParse(Console.ReadLine(), numberOfItems) Then
Dim items As New List(Of Decimal)()
For i As Integer = 1 To numberOfItems
Console.WriteLine("Enter number {0}", i)
Dim num As Decimal
If Decimal.TryParse(Console.ReadLine(), num) Then
items.Add(num)
End If
Next
Console.WriteLine("Average is " + items.Average())
Console.ReadLine()
End If
Please note that I've not included any type of error handling.

Get last number from string and increase it by one

Have some problems with my function. In database i could have diffrent numbers. For instance below: ( i know it looks strange )
12 312323.3
013.43.9
3.23.14353.55 WHATEVER 345.193
728937.3
87.3 ojojo 23.434blabla 24.424.7
What i need to do is increase number after LAST DOT so just make + 1.
The problem is its not working when it comes after dot more than one digit then.
here is my current code:
Dim inputValue as String = "34.234234.6.12"
'--Get Last char from string and add 1 to it
Dim lastChar As String = CInt(CStr(inputValue.Last)) + 1
'--Remove last char and add lastChar
Dim nextCombinNummer As String = lastValue.Nummer.Substring(0, lastValue.Nummer.Length - 1) & lastChar
Return nextCombinNummer
I think the problem is lastValue.Last + 1 as it will take only one digit, and also when i remove by substring last digit but only 2 will be removed.
Can you help me out with this? How to always take number after last dot from string and then increase that number by 1 and return new entire number?
EDIT:
I think i am able to get and increase the number but still dont know how to remove and put it at the end:
Think that's ok:
Dim inputValue as String = "34.234234.6.12"
Dim number As String = inputValue .Substring(inputValue .LastIndexOf("."c) + 1)
Dim numberIncreased as integer = CInt(number) + 1
'How to do this correctly? :
Dim nextCombinNummer As String = lastValue.Nummer.Substring(0, lastValue.Nummer.Length - 1) & numberIncreased
An easy solution is to cast as Integer the last part of the string, add one, then recompose your string :
'Original Value
Dim val As String = "123.456.789"
'We take only the last part and add one
Dim nb = Integer.Parse(val.Substring(val.LastIndexOf(".") + 1)) + 1
'We recompose the string
Dim FinalVal As String = val.Substring(0, val.LastIndexOf(".") + 1) & nb.ToString()
I'd use following which uses String.Split, Int32.TryParse and String.Join:
Dim numbers As New List(Of String) From {"12.312323.3", "013.43.9", "3.231435355345.193", "728937.3", "87.323.43424.424.7"}
for i As Int32 = 0 To numbers.Count -1
Dim num = numbers(i)
Dim token = num.Split("."c)
dim lastNum = token.Last() ' or token(token.Length-1)
Dim n As Int32
If int32.TryParse(lastNum, n)
n += 1
token(token.Length-1) = n.ToString()
End If
numbers(i) = string.Join(".", token)
Next

Populating 2 dimensional array using a For loop

Currently I'm trying to fill a 3x3 square with random x's and o's to make a tic tac toe
game. Unfortunately the game doesn't seem to output all the x's and o's. Logically, from what I can see, it should be able to but it's not. Any help would be appreciated.
Shared Sub twodimension()
Dim tic(2, 2) As String
Dim min As Integer
Dim x As String
Dim random As New Random()
Dim i As Integer
Dim x1 As Integer
Dim bound0 As Integer = tic.GetUpperBound(0)
Dim bound1 As Integer = tic.GetLowerBound(1)
For i = 0 To bound0
For x1 = 0 To bound1
min = random.Next(2)
If min = 0 Then
x = "x"
Console.WriteLine("{0}", x)
Else
x = "o"
Console.WriteLine("{0}", x)
End If
Console.Write(" "c)
Next
Console.WriteLine()
Next
End Sub
So presumably you've got this declaration somewhere, right?
Public Shared Tic(2, 2) As String
In your code you've got GetLowerBound which will (almost) always returns zero and instead you should have GetUpperBound().
Dim bound0 As Integer = tic.GetUpperBound(0)
Dim bound1 As Integer = Tic.GetUpperBound(1)
EDIT (in response to comment)
GetUpperBound(int) returns the highest number that you can use for the dimension that you specify.
So for the following array:
Dim MyArray(4, 6, 8) As Integer
Trace.WriteLine(MyArray.GetUpperBound(0)) ''//Returns 4
Trace.WriteLine(MyArray.GetUpperBound(1)) ''//Returns 6
Trace.WriteLine(MyArray.GetUpperBound(2)) ''//Returns 8
GetLowerBound(int) returns the lowest number that you can use for the dimension that you specify. In almost every case this is zero but in older versions of VB (and using some COM interop) you can create arrays that don't "start" at zero and instead start at whatever you wanted. So in old VB you could actually say Dim Bob(1 To 4) As Integer and GetLowerBound(0) would return 1 instead of 0. For the most part there is no reason to even be aware that GetLowerBound exists even.

vb express 2010 populating array

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