我怎样才能将输入吐到2?
How can i spit the input into 2?
我有一个输入,由一行中的任意 2 个数字组成,并且可以有无限数量的行,例如。
30 60
81 22
38 18
我想将每一行分成 2 个标记,第一个标记是左边的数字,第二个标记是右边的数字。我应该怎么办?感谢所有帮助。
如果输入总是这样设置,请查看 String.split()
// For just splitting into two strings separated by whitespace
String numString = "30 60";
String[] split = numString.split("\s+");
// For converting the strings to integers
int[] splitInt = new int[split.length];
for(int i = 0; i < split.length; i++)
splitInt[i] = Integer.parseInt(split[i]);
使用扫描仪和System.in:
public class SplitTest
{
public static void main (final String[] args)
{
try (Scanner in = new Scanner (System.in))
{
while (in.hasNext ())
{
System.out.println ("Part 1: " + in.nextDouble ());
if (in.hasNext ())
System.out.println ("Part 2: " + in.nextDouble ());
}
}
catch (final Throwable t)
{
t.printStackTrace ();
}
}
}
我有一个输入,由一行中的任意 2 个数字组成,并且可以有无限数量的行,例如。
30 60
81 22
38 18
我想将每一行分成 2 个标记,第一个标记是左边的数字,第二个标记是右边的数字。我应该怎么办?感谢所有帮助。
如果输入总是这样设置,请查看 String.split()
// For just splitting into two strings separated by whitespace
String numString = "30 60";
String[] split = numString.split("\s+");
// For converting the strings to integers
int[] splitInt = new int[split.length];
for(int i = 0; i < split.length; i++)
splitInt[i] = Integer.parseInt(split[i]);
使用扫描仪和System.in:
public class SplitTest
{
public static void main (final String[] args)
{
try (Scanner in = new Scanner (System.in))
{
while (in.hasNext ())
{
System.out.println ("Part 1: " + in.nextDouble ());
if (in.hasNext ())
System.out.println ("Part 2: " + in.nextDouble ());
}
}
catch (final Throwable t)
{
t.printStackTrace ();
}
}
}