这是我正在为项目工作的代码 我无法弄清楚的是最大值以及如何获得它? 即使我改变它,似乎也不会影响柜台的结果? 任何人都可以带领我朝着正确的方向前进吗? 谢谢!
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
答案 0 :(得分:1)
我认为你想要完成的事情是这样的:
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
在您的代码版本中,它会在每次循环时输出所有字母。在我的代码版本中,每次循环时它只输出一个字母。 Select Case
语句基本上是编写一堆单独的If
语句的简单方法。
然而,写一个像这样的大Select Case
是愚蠢的。实现循环的最佳方法是让它迭代某种索引数据结构。在这种情况下,您只需要一个字母列表。最简单的方法是将所有字母存储在一个字符串中,如下所示:
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
答案 1 :(得分:0)
您在模块级别声明counter
并在迭代中声明为私有变量。
每次for循环进入下一步时,私有counter
都会被新值覆盖。您应该将其重命名为
For c As Integer = 0 To Max_Value Step 1
counter = counter + 1
Next
或
For c As Integer = 0 To Max_Value Step 1
counter = c
Next
你可能想要这个:
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
更新:这与您的原始代码没什么关系,但是另一种方法
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
答案 2 :(得分:0)
创建要在for循环中使用的单独变量。发生的事情是你使用变量计数器循环然后在循环内递增它。这总是循环22次。 所以声明单独的变量并循环它。
Dim intC as integer
For intC = 0 to Max_Value step 1
counter = counter + 1
Next