【发布时间】:2014-06-21 02:21:02
【问题描述】:
我是java新手,所以如果这是一个“愚蠢”的问题,请耐心等待。有没有办法格式化类似于 printf("%d", a) 的返回语句?到目前为止,这是我的代码的 sn-p。
public static int numUnique(int a, int b, int c) {
if (a==b && a==c) {
System.out.println("No unique numbers.");
} else if (a==b && a!=c) {
System.out.printf("%d%d", a, c);
} else if (a==c && a!=b) {
System.out.printf("%d%d", c, b);
} else if (b==c && b!=a) {
System.out.printf("%d%d", b, a);
} else {
System.out.printf("%d%d%d", a, b, c);
}
}
}
我知道那里需要一个 return 语句才能获得正确的语法,并且我想使用 return 类似于我的代码中“理论上”使用的 printf。谢谢!
杰森
【问题讨论】:
-
但是这个方法不应该返回一个int ...吗?
-
一个'int'没有格式,甚至没有表示;这只是一个数字。正如@DavidWallace 建议的那样,您可以改为返回一个字符串。