PowerShell 程序练习
- 从键盘输入一个字符,判断字符类型
[char]$char = Read-Host \"Input a char!\"if($char -ge 48 -and $char -le 57){Write-Output(\"这是一个数字\")}elseif($char -ge 65 -and $char -le 90){Write-Output(\"这是一个大写字母\")}elseif($char -ge 97 -and $char -le 122){Write-Output(\"这是一个小写字母\")}else{Write-Output(\"这是一个符号\")}
- 输入字符串,统计字符数
[string]$string = Read-Host \"Input a string!\"$length = $string.Length[int]$num0 = 0[int]$num1 = 0[int]$num2 = 0[int]$num3 = 0for($i = 0; $i -lt $length; $i++){if($string[$i] -ge 48 -and $string[$i] -le 57){$num0++}elseif($string[$i] -ge 65 -and $string[$i] -le 90){$num1++}elseif($string[$i] -ge 97 -and $string[$i] -le 122){$num2++}else{$num3++}}Write-Host(\"数字:\" + $num0)Write-Host(\"大写字母:\" + $num1)Write-Host(\"小写字母:\" + $num2)Write-Host(\"符号:\" + $num3)
- 水仙花数
[int]$count = 0for([int]$i = 100; $i -lt 1000; $i++){$num0 = $i % 10$num1 = ($i - $num0) / 10 % 10$num2 = ($i - $num0 - $num1 * 10) / 100if($i -eq ($num0 * $num0 * $num0 + $num1 * $num1 * $num1 + $num2 * $num2 * $num2)){Write-Output($i)$count++}}Write-Output(\"水仙花数个数为:\" + $count)
- 判断字符串能否转为有效数字
[string]$string = Read-Host \"输入字符串:\"for($i = 0; $i -lt $string.Length; $i++){if(!($string[$i] -ge 48 -and $string[$i] -le 57)){Write-Output(\"不能转为数字\")return}}Write-Output(\"可以转为数字\")
- 打印乘法表
for($raw = 1; $raw -lt 10; $raw++){for($column = 1; $column -le $raw; $column++){Write-Host($column)-NoNewlineWrite-Host(\"*\"+$raw)-NoNewlineWrite-Host(\"=\")-NoNewlineWrite-Host($raw * $column)-NoNewlineWrite-Host(\" \")-NoNewline}Write-Host(\"\")}
- 完全平方数
一个10000以内的整数,加上100和268后都是完全平方数,求这个数
for($x = 0; $x -lt 10000; $x++){for($y = 1; $y -lt 200; $y++){if($y -eq ($x + 100) / $y){for($z = 1; $z -lt 200; $z++){if($z -eq ($x + 268) / $z){Write-Output($x)}}}}}
- 拆字
\”网络空间安全\”拆成“网络、络空、空间、间安、安全”
[string]$s = \"网络空间安全\"for($i = 0; $i -lt $s.Length - 1; $i++){Write-Output($s.Substring($i,2))}
- 排序
$num = 3, 65, 22, 102, 4for($i = 0; $i -lt $num.Length; $i++){for($j = $i; $j -lt $num.Length; $j++){if($num[$i] -lt $num[$j]){$n = $num[$i]$num[$i] = $num[$j]$num[$j] = $n}}}Write-Output($num)
- 1000内的质数
for($num = 0; $num -lt 1000; $num++){$count = 0for($n = 1; $n -le $num; $n++){if($num % $n -eq 0){$count++}}if($count -eq 2){Write-Output($num)}}
- 打印随机行数的三角形
$num_array = 1..100$num_array = Get-Random -InputObject $num_array -Count ([int]::MaxValue)for($i = 0; $i -lt $num_array[0]; $i++){for($j = 0; $j -le $i; $j++){Write-Host(\"*\")-NoNewline}Write-Host(\"\")}