Визуальные основы help-Variables для цикла for - PullRequest
0 голосов
/ 10 октября 2018

Я пытаюсь получить целые числа "num3" (входные данные от пользователя) и возводить в квадрат каждого символа и в конечном итоге посмотреть, включает ли его "счастливое число" = 1 или "несчастное число" 4 Например,19 - счастливое число 1 ^ 2 + 9 ^ 2 = 82, 8 ^ 2 + 2 ^ 2 = 68,6 ^ 2 + 8 ^ 2 = 100, 1 ^ 2 + 0 + 0 = 1

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

Конечный модуль

Однако я застрял, если "num3" заменить себя из цикла for

1 Ответ

0 голосов
/ 10 октября 2018

Здесь я считаю, что это должно работать:

    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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...