我是java新手,如果这是一个“愚蠢”的问题,请耐心等待我。有没有一种方法来格式化类似于printf(“%d”,a)的返回语句?这是我的代码到目前为止的一个片段。返回语句是否可以像printf一样格式化?
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语句,我想同样使用返回的printf在我的代码中使用了“理论上”。谢谢!
杰森
但不应该这个方法返回一个int ...? – sfletche
'int'没有格式,甚至没有表示;这只是一个数字。正如@DavidWallace所示,您可以返回一个字符串。 – davmac