Visual Basic帮助-for循环中的变量

时间:2018-10-09 23:10:48

标签: vb.net visual-studio-2017

我正在尝试获取“ cat $line.part-* > $line”(用户输入)的整数,并对每个字符进行平方运算,最后查看其“高兴数” = 1或“不幸数”是否包括4 例如19是快乐数字1 ^ 2 + 9 ^ 2 = 82,8 ^ 2 + 2 ^ 2 = 68,6 ^ 2 + 8 ^ 2 = 100,1 ^ 2 + 0 + 0 = 1

num3

最终模块

无论如何,我都无法从for循环中替换“ Sub Main() Dim number, num2, total As Integer Dim num3 As String Console.Write("pick a number:") number = Console.ReadLine() num2 = 0 num3 = Convert.ToString(number) Do Until total = 1 Or num2 = 4 For Each num3 In num3.Substring(0, num3.Length) For counter = 1 To num3.Length And num3 = num3.Substring(0, num3.Length) num2 = num3 ^ 2 total = total + num2 Console.WriteLine(total) Next num3 = total Next Loop Console.ReadLine() If total = 1 Then Console.WriteLine("happy number") ElseIf num2 = 4 Then Console.WriteLine(number & "is a unhappy number") Console.ReadLine() End If End Sub

1 个答案:

答案 0 :(得分:0)

在这里,我相信这应该可行:

    Dim InputNum As Integer = 45 'Your input number

    Dim TempNum As Integer = InputNum 'Here we hold number temporarily during steps 19>82....
    Do
        Dim SumTotal As Double = 0 'sum of numbers squares
        For Each number As Char In TempNum.ToString 
            SumTotal += Integer.Parse(number) ^ 2
        Next
        TempNum = CInt(SumTotal)
        Debug.Print(TempNum.ToString)
        'If result is 4 it will loop forever
        If SumTotal = 4 Then
            Console.WriteLine(InputNum & " is an unhappy number")
            Exit Do
        ElseIf SumTotal = 1 Then
            Console.WriteLine(InputNum & " is a happy number")
            Exit Do
        End If
    Loop