'Powershell Basic

I have to do a simple calculator +, -, * and / on PowerShell for my coding introduction course. Why my Variable Valeur1 & Valeur2 don't remain intact during the integer validation ? My 4 options give a random answer and don't remember the initial value of my 2 Read-Host Variables.



Write-Host "Microsoft Powershell Calculator "

$R = $True
$OK = $True

while ($R -eq $True) {
    do {
        $Valeur1 = [int]::TryParse((Read-Host "Entrez une premiere valeur numerique"), [ref]$OK)
        if (-not $OK) {
            Write-Host ("ERREUR ! Vous devez entrez une valeur numerique")
            $OK = $false
        }
        else{
            $OK = $true    
        }
    } while (-not $OK)

    do {
        $Valeur2 = [int]::TryParse((Read-Host "Entrez une seconde valeur numerique"), [ref]$OK)
        if (-not $OK) {
            Write-Host ("ERREUR ! Vous devez entrez une valeur numerique")
            $OK = $false
        }
        else{
            $OK = $true    
        }
    } while (-not $OK)

    $operation = Read-Host -prompt "Quelle opération voulez-vous réaliser? Choisissez entre [+, -, *, or /] "
    Switch($operation)
    {
        -
        {
            Write-Host("Le resultat de votre soustraction est :")
            $Valeur1 - $Valeur2
        }
        +
        {
            Write-Host("Le resultat de votre addition est :")
            $Valeur1 + $Valeur2
        }
        *
        {
            Write-Host("Le resultat de votre multiplication est :")
            $Valeur1 * $Valeur2
        }
        /
        {
            Write-Host("Le resultat de votre division est :")
            $Valeur1 / $Valeur2
        }
        default
        {
            Write-Host("Option Invalide")
        }
    }

    $choix = Read-Host "Desirez-vous continuer? Y/N" 
    if($choix -eq "Yes"){
        $R = $true
    }
    if($choix -eq "Y"){
        $R = $true
    }
    else{
        $R = $false
        exit
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source