我一直在尝试从给定输入作为STDIN的在线课程中读取数字,我的文件是这样的:
2
3 15
我读了2和3,怎么读15?
我的代码:
<?php
/*
// Sample code to perform I/O:
fscanf(STDIN, "%s\n", $name); // Reading input from STDIN
echo "Hi, ".$name.".\n"; // Writing output to STDOUT
// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
*/
// Write your code here
fscanf(STDIN, "%s\n", $ammount);
echo $ammount . "\n";
for ($i = 0; $i <= $ammount; $i++)
{
fscanf(STDIN, "%s", $x);
echo $x . "\n";
}
?>
我现在的输出是:2 3 3我想要2 3 15
fscanf
在线上工作,但%s
默认停止阅读太空。使用:
fscanf(STDIN, "%[^\n]", $x);
并用空格爆炸$x
我试图推动你的是在你的格式字符串中使用一个空格并将你的输入读入单独的变量,就像这样。
fscanf(STDIN, "%s %s", $a, $b);
echo $a . ' ' . $b . "\n";