第一次练习
1.已知a,b均是整型变量,写出将a,b两个变量中的值互换的程序。
package homework1; public class Name1 { public static void main(String[] args) { int a = 1; int b = 2; int c; //进行数据交换 c = b; b = a; a = c ; System.out.println("交换后的a:"+ a + ",b:" + b); }
2.给定一个0~1000的整数,求各位数的和。
package homework1; public class Name2 { public static void main(String[] args) { int a=345; int b=a/100; int c=a%100/10; int d=a%100%10; int sum=b+c+d; System.out.println("结果为"+sum); } }
3.华氏度50转成摄氏度是多少?
package homework1; public class Name2{ public static void main(String[] args) { float c=50f; float f=100f; float cTof=f/9*5+32; float fToc=c/32*5/9; System.out.println("摄氏转华氏="+cTof); System.out.println("华氏转摄氏="+fToc); } }
4.给定一个任意的大写字母A~Z,转换为小写字母。
package homework1; public class Name2 { public static void main(String[] args) { // TODO Auto-generated method stub char g = 'A'; System.out.println("转换为小写字母为:" + (char)(g + 32)); } }