JAVA练习题1(初级)
文章目录
1.求一光年是多少千米 , 光在真空中传播速度299792458 m/s
public class demo01 {
public static void main(String[] args) {
long time = 60*60*24*365;
long lon = (299792458*time)/1000;
System.out.println("一光年:"+lon+"千米");
}
}
2.根据天数(46)计算周数和剩余的天数
public class demo02 {
public static void main(String[] args) {
int day = 46;
int week = day/7;
int surplusday = day%7;
System.out.println("周数为:"+week);
System.out.println("剩余天数:"+surplusday);
}
}
3.已知圆的半径radius= 1.5,求其面积 (Java中π用Math.PI表示)
public class demo03 {
public static void main(String[] args) {
double pi = Math.PI;
double radius = 1.5;
double area = pi*Math.pow(radius,2);
System.out.println("该圆形的面积为:"+area);
}
}
public class demo04 {
public static void main(String[] args) {
int a = 10,b= 20,c = 30,d = 40,e = 50;
int max = (a>b)?a:b;
max = (max>c)?max:c;
max = (max>d)?max:d;
max = (max>e)?max:e;
System.out.println(max);
}
}
-
韩梅梅看中两把价格相同的扇子,想挑选一个扇面较大的扇子购买,请你帮她挑选。
A款折叠扇:展开后角度为134.6°,扇骨长26.5cm
B款圆扇:扇柄长12.3cm,扇子总长度36.5cm
注:圆形面积 = 3.14 * 半径平方
扇形面积 = 3.14 * 半径平方 * (度数/360)
public class demo05 {
public static void main(String[] args) {
double a = 3.14*Math.pow(26.5,2)*(134.6/360);
double blong = ((36.5-12.3)/3.14)/2;
double b = 3.14*Math.pow(blong,2);
System.out.println("扇面较大的为:"+((a>b)?"折叠扇":"圆扇"));
}
}
6.变量a和b的值互换 例如:int a = 10,b=20; 结果:a=20,b=10;
public class demo06 {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("互换前a:"+a+"\tb:"+b);
int temp = a;
a = b;
b = temp;
System.out.println("互换前a:"+a+"\tb:"+b);
}
}
7.定义一个变量,是一个三位数,求各个位数的和. 如:123,结果是1+2+3=6
public class demo07 {
public static void main(String[] args) {
int num = 123;
int a = num / 100;
int b = num / 10 % 10;
int c = num % 10;
System.out.println("结果是:" + (a + b + c));
}
}