Why does multiplication does not work with Read-Host value

主宰稳场 提交于 2020-01-03 19:06:54

问题


$table_num = Read-Host -Prompt 'Enter the table number you want to get printed'
for ($i=1; $i-lt 11; $i++){
    $ans = ($table_num*$i)
    write "$table_num*$i=$ans"
}

Above is the script which I was trying to execute and I get the result as below:

Enter the table number you want to get printed: 5
5*1=5
5*2=55
5*3=555
5*4=5555
5*5=55555
5*6=555555
5*7=5555555
5*8=55555555
5*9=555555555
5*10=5555555555

I am new to scripting and help me with my code.


回答1:


As you did not point out what is the expected behavior I am not sure if I solve your issue, but I guess you want something like that

$table_num_str = Read-Host -Prompt 'Enter the table number you want to get printed'
$table_num = [int]::Parse($table_num_str)
for ($i=1; $i-lt 11; $i++){
    $ans = ($table_num*$i)
    write "$table_num*$i=$ans"
} 

which when entered '5' results in

Enter the table number you want to get printed: 5
5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50

So the problem was that Read-Host returns a string which when multiplied with X results in that string repeated X times.




回答2:


The problem lies in type returned by Read-Host cmdlet.

$num = read-host
$num.GetType()

It returns Object[] which is implicitly converted to other types if required. Type is also converted to first argument type when binary operator is used (+, -, *). There are no * operator in object[] type. That's why it's converted to string and THEN multiplied. If first argument is int, PowerShell tries to convert it to int as well. You can tweak your code to use this conversion rules. Take a look at script below:

$table_num = Read-Host -Prompt 'Enter the table number you want to get printed'
1..10 | % { write "$table_num*$_=$($_*$table_num)" }

It also produces:

5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50



回答3:


Thanks for the answers!!

Actually I have added the data type int. And the script is good as below.

$table_num = Read-Host -Prompt 'Enter the table number you want to get printed'

        For ($i=1; $i-lt 11; $i++){
        $ans = ([int]$table_num*$i)
            write "$table_num  *  $i =  $ans"
        }



回答4:


To have a working calculation you need a number, not a string.

Multiplying a string gives this kind of result :

"test" * 5 # returns testtesttesttesttest

You could fix your issue like this:

[int]$input_number = $null

$input_string = Read-Host -Prompt 'Enter the table number you want to get printed'

while(-not [int]::TryParse($input_string, [ref]$input_number)) {
    $input_string = Read-Host -Prompt 'Please enter a number'
}

for ($i = 1; $i -lt 11; $i++) {
    $ans = ($input_number * $i)
    Write-Host "$input_number * $i = $ans"
}

[int]::TryParse($input_string, [ref]$input_number) will try to parse the input string as an integer and store the value in $input_number. As long as this fails (because of a bad input), the script will ask for a new input.

See this other question for additional information.




回答5:


do {
    $table_num = read-host "Enter the table number you want to get printed"
    $a = ""
    if( ![int32]::TryParse( $table_num , [ref]$a ) ) {
        Write-Host "Only integers please"
    }
} until ($a -gt 0)

1..10 | % { Write-Host "$table_num*$_=$($_*$table_num)" }


来源:https://stackoverflow.com/questions/38410213/why-does-multiplication-does-not-work-with-read-host-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!