编写步骤:
参考答案:
public class Homework1 {
public static void main(String[] args) {
double x = 100.8;
double y = 20.6;
double add = x + y;
System.out.println("x,y的和为:" + add);
double sub = x - y;
System.out.println("x,y的差为:" + sub);
double mul = x * y;
System.out.println("x,y的积为:" + mul);
double div = x / y;
System.out.println("x,y的商为:" + div);
}
}
开发提示:四则运算的符号
加: +
减: -
乘: *
除: /
编写步骤:
参考答案:
public class Homework2 {
public static void main(String[] args) {
int x = 100;
int y = 200;
int add = x + y;
System.out.println("x,y的和为:" + add);
int sub = x - y;
System.out.println("x,y的差为:" + sub);
int mul = x * y;
System.out.println("x,y的积为:" + mul);
int div = x / y;
System.out.println("x,y的商为:" + div);
}
}
(1)先声明两个byte类型的变量b1,b2,并分别赋值为10和20,求b1和b2变量的和,并将结果保存在byte类型的变量b3中,最后输出b3变量的值
(2)先声明两个short类型的变量s1,s2,并分别赋值为1000和2000,求s1和s2变量的和,并将结果保存在short类型的变量s3中,最后输出s3变量的值
(3)先声明1个char类型的变量c1赋值为’a’,再声明一个int类型的变量num赋值为5,求c1和num变量的和,并将结果将结果保存在char类型的变量letter中,最后输出letter变量的值。
(4)先声明两个int类型的变量i1,i2,并分别赋值5和2,求i1和i2的商,并将结果保存在double类型的变量result中,最后输出result变量的值。如何得到结果2.5呢?
参考答案:
public class Homework3 {
public static void main(String[] args) {
byte b1 = 10;
byte b2 = 20;
byte b3 = (byte)(b1 + b2);
System.out.println("byte类型的b1和b2的和为:" + b3);
short s1 = 1000;
short s2 = 2000;
short s3 = (short)(s1 + s2);
System.out.println("short类型的s1和s2的和为:"+ s3);
char c1 = 'a';
int num = 5;
char letter = (char) (c1 + num);
System.out.println("char类型的c1和int类型的num的和:" + letter);
int i1 = 5;
int i2 = 2;
double result = (double)i1 / i2;
System.out.println("int类型的i1和i2的商是:" + result);
}
}
参考答案:
public class Homework4 {
public static void main(String[] args) {
int a1 = 10;
int a2 = 11;
int a3 = 12;
int a4 = 13;
System.out.println(a1 + "是偶数?" + (a1 % 2 == 0));
System.out.println(a2 + "是偶数?" + (a2 % 2 == 0));
System.out.println(a3 + "是奇数?" + (a3 % 2 != 0));
System.out.println(a4 + "是奇数?" + (a4 % 2 != 0));
}
}
案例:为抵抗洪水,战士连续作战89小时,编程计算共多少天零多少小时?
步骤:
参考答案:
public class Homework5 {
public static void main(String[] args){
int hours = 89;
int day = hours / 24;
int hour = hours % 24;
System.out.println("为抵抗洪水,战士连续作战89小时:");
System.out.println(hours + "是" + day + "天" + hour +"小时");
}
}
案例:小明要到美国旅游,可是那里的温度是以华氏度为单位记录的。它需要一个程序将华氏温度(80度)转换为摄氏度,并以华氏度和摄氏度为单位分别显示该温度。转换公式:
参考答案:
public class Homework9 {
public static void main(String[] args) {
double hua = 80;
double she = (hua-32)/1.8;
System.out.println("华氏度" + hua+"℉转为摄氏度是" +she+"℃");
}
}
float f=3.4;是否正确,表达式15/2*2的值是多少?
参考答案:
public class Homework10 {
public static void main(String[] args) {
float f = 3.4; //编译报错,因为3.4是double类型,大于float类型
System.out.println(15/2*2);//15/2结果是7,7*2=14
}
}
?
参考答案:
public class Homework10 {
public static void main(String[] args) {
float f = 3.4; //编译报错,因为3.4是double类型,大于float类型
System.out.println(15/2*2);//15/2结果是7,7*2=14
}
}