Java第二次作业
1.编写一个程序,定义圆的半径,求圆的面积.
package test; public class test1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int r=2; double Π=3.14; System.out.println(r*r*Π); } }
2.华氏温度和摄氏温度互相转换,从华氏度变成摄氏度你只要减去32,乘以5再除以9就行了,将摄氏度转成华氏度,直接乘以9,除以5,再加上32即行。(知识点:变量和运算符综合应用)
package test; public class test1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub float c = 30; float h = 50; float C = (h-32)*5/9; float H = c*9/5+32; System. out. println("摄氏转华氏="+C ); System. out. println("华氏转摄氏="+H ); } }
3.已知a,b均是整型变量,写出将a,b两个变量中的值互换的程序。(知识点:变量和运算符综合应用)
package test; public class test1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a = 5,b = 8; int c = a; a = b; b = c; System.out.println(a); System.out.println(b); } }
4.定义一个任意的5位整数,将它保留到百位,无需四舍五入(知识点:变量和运算符综合应用)
package test; public class test1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a; a=12345; System.out.println(a/100*100); } }
5.输入一个0~1000的整数,求各位数的和,例如345的结果是3+4+5=12注:分解数字既可以先除后模也可以先模后除(知识点:变量和运算符综合应用)
package test; public class test1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a= 123; int b,c,d,e,sum; b = a/1000; c = a/100%10; d = a/10%10; e = a%10; sum =b+c+d+e; System.out.println(sum); } }
6.定义一个任意的大写字母A~Z,转换为小写字母(知识点:变量和运算符综合应用)
定义一个任意的小写字母a~z,转换为大写字母
package test; public class test1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub char m='A'; System.out.println((char)(m+32)); char n='a'; System.out.println((char)(n-32)); } }