?: 在Java中:如何更合理地组合res和resString?
我希望输出(double res和String resString的混合)被分配一个变量
import java.util.Scanner;
public class Lab3Task02_08 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("х = ");
int x = in.nextInt();
System.out.print("y = ");
int y = in.nextInt();
double arithm = (x+y)/2;
double geom = Math.sqrt(Math.abs(x*y));
//////////////////////////////////////////////////////////////
double res = x>y? arithm:geom;
String resString = x>y? "Arithm.average = ":"Geom.average = ";
//////////////////////////////////////////////////////////////
System.out.print(resString + res);
in.close();
}
}
# 1 楼答案
如果我理解这个问题,这就是你想要的吗
# 2 楼答案
据我所知,您希望根据相同的条件输出两个不同的值。其中一个选项是,它可以通过
String.format()
、Double.compare()
和x ? y : x
完成输出: