日期:2014-05-20  浏览次数:20686 次

想从1/2格式的输入中读入数字
使用scanner,发现手册中有useDelimeter的用法
想借此实现用户输入1/2后,分别读出分子、分母作为整型变量存储
可是程序出错,大家帮忙改改

public   void   fGetFraction(Fraction   f1){
Scanner   sc   =   new   Scanner(System.in).useDelimiter( "//s*///s* ");
System.out.println( "Please   input   a   fraction(as   the   style   of   1/2): ");
f1.fz   =   (int)sc.nextInt();
//System.out.print( "/ ");
f1.fm   =   (int)sc.nextInt();
}

谢谢

------解决方案--------------------
"//s*///s* "这是什么东东,如果是 "\\s*/\\s* "应该也可以,其实只需要 "/ "就行了
\\s*是表示0个或0个以上的空格。
------解决方案--------------------
"1/2 ".split( "/ ")会得到数组:{ "1 ", "2 "}