如何水平显示for循环中的输出?
How to display output in the for loop horizontally?
我正在创建一个显示 n 个质数的程序(例如,如果用户输入 6,该程序将显示前 6 个质数)。我希望显示 1 行包含 5 个数字。我确实用谷歌搜索来寻找解决方案,但没有找到。
我试过使用 if else 语句来做,但没有用
for ( $i = 3; $i -gt 0; ++$i ) #starting from 3
{
$sqrt = [math]::Sqrt($i) #Optimize memory consumption
for ( $j = 2; $j -le $sqrt; ++$j ) #Checked if it is divisible by any natural number
{
if ( $i % $j -eq 0 ) #if is prime number
{
$prime = 1 #set to false
break #stop taking into account
}
}
if ( $prime -eq 0 ) #if not prime number
{
Write-Host ("$i") #display prime number
$count++ #set count to 1
$turn++ #set turn to 1
}
$prime = 0 #is prime number
if ( $count -eq $value ) #if until nth prime number
{
break #stop taking into account
}
if ( $turn -eq 5 )
{
Write-Output(" ")
$turn = 0
}
}
我期待的结果是这样的:
3个
5个
7
11
13
17
19
23
29
31
但它给了我这样的输出:
3
5
7
11
13
17
19
23
29
31
欢迎任何建议/意见。提前谢谢你。
问题已解决。
通过使用 -NoNewLine 命令并在 "decorating" 输出上付出一些努力。
达到预期效果。
非常感谢。
一些通用的指针:
避免混合调用 Write-Host
和 Write-Output
构建输出 - 这些 cmdlet 的用途非常不同。
Write-Output
- 很少需要显式使用 - 输出 data,供以后 程序化处理 .
- 任何未在变量中捕获、通过管道发送到另一个命令或重定向到文件的命令或表达式的输出都是隐式输出,因此
Write-Output $i
可以简单地写成 $i
.
Write-Host
writes to the host (typically, the console) and provides feedback to the user in the form of status information or to aid in soliciting input - it writes to an output stream 即 separate 来自 data (成功输出)流,因此你不应该用它来输出 data,因为这样的输出(无需额外的努力)既不会被捕获在变量中,也不会被发送到另一个命令。
调用 PowerShell 命令(cmdlet/函数/脚本)时避免伪方法语法,例如 Write-Output(" ")
:
- 在 PowerShell 中,命令的调用方式类似于 shell 命令 -
Write-Output arg1 arg2
- 而非 类似于 C# 方法 - Write-Output(arg1, arg2)
;参见 Get-Help about_Parsing
。
为防止意外使用方法语法,请使用 Set-StrictMode -Version 2
或更高版本,但请注意其其他影响。
如果你想在同一行输出一个array对象,简单地stringify它通过expandable string(内插字符串),"..."
,它创建一个 space 分隔的 [1] 其(字符串化)元素列表:
function get-Primes {
param($n)
1..$n # simulate output: simply return array 1, 2, 3, ...
}
# Get an array of numbers
$primes = get-Primes 5
# Output the array as a space-separated list, via an expandable string:
"$primes"
以上结果:
1 2 3 4 5
[1] space 字符。是默认分隔符,但 $OFS
首选项变量可用于指定不同的字符。
我正在创建一个显示 n 个质数的程序(例如,如果用户输入 6,该程序将显示前 6 个质数)。我希望显示 1 行包含 5 个数字。我确实用谷歌搜索来寻找解决方案,但没有找到。
我试过使用 if else 语句来做,但没有用
for ( $i = 3; $i -gt 0; ++$i ) #starting from 3
{
$sqrt = [math]::Sqrt($i) #Optimize memory consumption
for ( $j = 2; $j -le $sqrt; ++$j ) #Checked if it is divisible by any natural number
{
if ( $i % $j -eq 0 ) #if is prime number
{
$prime = 1 #set to false
break #stop taking into account
}
}
if ( $prime -eq 0 ) #if not prime number
{
Write-Host ("$i") #display prime number
$count++ #set count to 1
$turn++ #set turn to 1
}
$prime = 0 #is prime number
if ( $count -eq $value ) #if until nth prime number
{
break #stop taking into account
}
if ( $turn -eq 5 )
{
Write-Output(" ")
$turn = 0
}
}
我期待的结果是这样的:
3个
5个
7
11
13
17 19 23 29 31
但它给了我这样的输出:
3
5
7
11
13
17
19
23
29
31
欢迎任何建议/意见。提前谢谢你。
问题已解决。 通过使用 -NoNewLine 命令并在 "decorating" 输出上付出一些努力。
达到预期效果。
非常感谢。
一些通用的指针:
避免混合调用
Write-Host
和Write-Output
构建输出 - 这些 cmdlet 的用途非常不同。Write-Output
- 很少需要显式使用 - 输出 data,供以后 程序化处理 .- 任何未在变量中捕获、通过管道发送到另一个命令或重定向到文件的命令或表达式的输出都是隐式输出,因此
Write-Output $i
可以简单地写成$i
.
- 任何未在变量中捕获、通过管道发送到另一个命令或重定向到文件的命令或表达式的输出都是隐式输出,因此
Write-Host
writes to the host (typically, the console) and provides feedback to the user in the form of status information or to aid in soliciting input - it writes to an output stream 即 separate 来自 data (成功输出)流,因此你不应该用它来输出 data,因为这样的输出(无需额外的努力)既不会被捕获在变量中,也不会被发送到另一个命令。
调用 PowerShell 命令(cmdlet/函数/脚本)时避免伪方法语法,例如
Write-Output(" ")
:- 在 PowerShell 中,命令的调用方式类似于 shell 命令 -
Write-Output arg1 arg2
- 而非 类似于 C# 方法 -Write-Output(arg1, arg2)
;参见Get-Help about_Parsing
。
为防止意外使用方法语法,请使用Set-StrictMode -Version 2
或更高版本,但请注意其其他影响。
- 在 PowerShell 中,命令的调用方式类似于 shell 命令 -
如果你想在同一行输出一个array对象,简单地stringify它通过expandable string(内插字符串),"..."
,它创建一个 space 分隔的 [1] 其(字符串化)元素列表:
function get-Primes {
param($n)
1..$n # simulate output: simply return array 1, 2, 3, ...
}
# Get an array of numbers
$primes = get-Primes 5
# Output the array as a space-separated list, via an expandable string:
"$primes"
以上结果:
1 2 3 4 5
[1] space 字符。是默认分隔符,但 $OFS
首选项变量可用于指定不同的字符。