自己写了一个java程序,但是很无语,求帮助!
package P20_2;
import java.util.*;
class F
{
int fun(int n)
{
Scanner in = new Scanner(System.in);
n = in.nextInt();
while((char)n != '#')
{
break;
}
return (n * fun(n));
}
}
public class p20_2
{
public static void main(String[] args)
{
int n;
F f = new F();
Scanner in = new Scanner(System.in);
System.out.println("这个程序可以多次把数相乘");
System.out.println("输入一个整数后按回车键,可继续输入要乘的数,以#结束计算");
n = in.nextInt();
System.out.println("结果为:" +f.fun(n));
}
}
请帮忙一下,谢谢了
------解决方案--------------------
这样怎么样?
Java code
import java.util.*;
class F {
int fun(int n) {
Scanner in = new Scanner(System.in);
if(in.hasNextInt()) {
int m = in.nextInt();
return fun(n * m);
} else {
return n;
}
}
public static void main(String[] args) {
int n;
F f = new F();
Scanner in = new Scanner(System.in);
System.out.println("这个程序可以多次把数相乘");
System.out.println("输入一个整数后按回车键,可继续输入要乘的数,以#结束计算");
n = in.nextInt();
System.out.println("结果为:" + f.fun(n));
}
}