1.用变量简化计算
public class test01 {
/*
* (sqrt(20)+sqrt(10))/(sqrt(20-sqrt(10)))
*/
public static void main(String[] args) {
// System.out.println("(sqrt(20)+sqrt(10))/(sqrt(20-sqrt(10)))="+
// (Math.sqrt(20)+Math.sqrt(10))/(Math.sqrt(20)-Math.sqrt(10));
double sqrt20=Math.sqrt(20);
double sqrt10=Math.sqrt(10);
double result=(sqrt10+sqrt20)/(sqrt20-sqrt10);
result=Math.round(10*result)/10.0;
System.out.println("(sqrt(20)+sqrt(10))/(sqrt(20-sqrt(10)))="+result);
}
}
- 用变量保存多种类型的数据
public class test02 {
public static void main(String[] args) {
String name="李靖";
char sex='女';//char类型需要单引号
short age=24;
float height=1.55f;
String type="漂亮的";
System.out.println("嗨,大家好,我叫"+name+",今年"+age+"岁,我是一个"+type+sex+"士");
name="张昭";
sex='男';
age=24;
height=1.73f;
type="帅气的";
System.out.println("嗨,大家好,我叫"+name+",今年"+age+"岁,我是一个"+type+sex+"士");
}
}