import java.io.*;
import java.util.*;
class usingDelimiters
{
public static void main(String args[])
{
Scanner dis=new Scanner(System.in);
int a,b,c;
a=dis.nextInt();
b=dis.nextInt();
c=dis.nextInt();
System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("c="+c);
}
}
当我的输入是 1 2 3 (用空格分隔)时,该程序运行正常
但是,当我的输入是1,2,3(用逗号分隔)时如何修改我的程序
您可以对非数字项使用分隔符,这会将任何非数字标记为分隔符。
例如:
dis.useDelimiter("\\D");
The useDelimiter
方法需要一个Pattern
or the String
的表示Pattern
.
完整示例:
Scanner dis=new Scanner(System.in);
dis.useDelimiter("\\D");
int a,b,c;
a=dis.nextInt();
b=dis.nextInt();
c=dis.nextInt();
System.out.println(a + " " + b + " " + c);
dis.close();
输入(或者)
Output
1 2 3
Note
- 别忘了
close
your Scanner
!
- See the API for
Pattern
为您的输入划定界限带来更多乐趣。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)