第一次上机作业
1、已知a,b均是整型变量,写出将a,b两个变量中的值互换的程序。(知识点:变量和运算符综合应用)
1 package tset; 2 3 public class demo { 4 5 public static void main(String[] args) { 6 int a = 3; 7 int b = 5; 8 int tmp = a; 9 a = b; 10 b = tmp; 11 System.out.println("a=" + a); 12 System.out.println("b=" + b); 13 // TODO Auto-generated method stub 14 15 } 16 17 }
2、给定一个0~1000的整数,求各位数的和,例
如345的结果是3+4+5=12注:分解数字既可以先
除后模也可以先模后除(知识点:变量和运算符
综合应用)
12 0012
5 0005
1 package tset; 2 3 public class demo { 4 5 public static void main(String[] args) { 6 int a = 2000; 7 int ge = a % 10; 8 int shi = a / 100 % 10; 9 int bai = a / 10 % 100; 10 int qian = a / 1000; 11 int he = ge + shi + bai + qian; 12 System.out.println(ge); 13 System.out.println(shi); 14 System.out.println(bai); 15 System.out.println(qian); 16 System.out.println(he); 17 // TODO Auto-generated method stub 18 19 } 20 21 }
3、华氏度50转成摄氏度是多少???(华氏温度和摄氏温度互相转换,从华氏度变成
摄氏度你只要减去32,乘以5再除以9就行了,将
摄氏度转成华氏度,直接乘以9,除以5,再加上
32即行)
1 package tset; 2 3 public class demo { 4 5 public static void main(String[] args) { 6 int F = 50; 7 int C =(F - 32) * 5 / 9; 8 System.out.println(C); 9 int CC = 10; 10 int FF = CC * 9 / 5 + 32; 11 System.out.println(FF); 12 // TODO Auto-generated method stub 13 14 } 15 16 }
4、给定一个任意的大写字母A~Z,转换为小写字母。 (知识点:变量和运算符)
1 package tset; 2 3 public class demo { 4 5 public static void main(String[] args) { 6 char c = 'A'; 7 System.out.println((char) (c + 32) ); 8 // TODO Auto-generated method stub 9 10 } 11 12 }