第一次上机练习
1、已知a,b均是整型变量,写出将a,b两个变量中的值互换的程序。(知识点:变量和 运算符综合应用)
package huaerzi; public class huada { public static void main(String[] args) { //TODO AUto-generated method stub int a=100; int b=200; int tmp; //数据交换 tmp=a; a=b; b=tmp; System.out.println("交换后的a:"+a+",b;"+b); }
}![](https://img2020.cnblogs.com/blog/1966300/202003/1966300-20200319110615164-8122341.png)
![](https://img2020.cnblogs.com/blog/1966300/202003/1966300-20200319110615164-8122341.png)
2、给定一个0~1000的整数,求各位数的和,例
如345的结果是3+4+5=12注:分解数字既可以先
除后模也可以先模后除(知识点:变量和运算符
综合应用)
12 0012
5 0005
如345的结果是3+4+5=12注:分解数字既可以先
除后模也可以先模后除(知识点:变量和运算符
综合应用)
12 0012
5 0005
package huaerzi; import java.util.Scanner; public class huada { public static void main(String[] args) { //TODO AUto-generated method stub int a=457,ge,shi,bai,he; ge=a%10; shi=a/10%10; bai=a/100; he=ge+shi+bai; System.out.println("三位数和为"+he); } }
![](https://img2020.cnblogs.com/blog/1966300/202003/1966300-20200319113926103-1534335896.png)
3、华氏度50转成摄氏度是多少???(华氏温度和摄氏温度互相转换,从华氏度变成
摄氏度你只要减去32,乘以5再除以9就行了,将
摄氏度转成华氏度,直接乘以9,除以5,再加上
32即行)
摄氏度你只要减去32,乘以5再除以9就行了,将
摄氏度转成华氏度,直接乘以9,除以5,再加上
32即行)
package huaerzi; import java.util.Scanner; public class huada { public static void main(String[] args) { //TODO AUto-generated method stub Scanner reader=new Scanner(System.in); int h=0; System.out.println("输入华温度:"); h=reader.nextInt(); double s=(double)5*(h-32)/9; System.out.print("华温度"+h+"对应摄氏度为:"); System.out.printf("%4.2f",s); } }
![](https://img2020.cnblogs.com/blog/1966300/202003/1966300-20200319112344385-1303645023.png)
4、给定一个任意的大写字母A~Z,转换为小写字母。 (知识点:变量和运算符)
package huaerzi; import java.util.Scanner; public class huada { public static void main(String[] args) { //TODO AUto-generated method stub char a='A'; System.out.println((char)(a + 32)); } }
![](https://img2020.cnblogs.com/blog/1966300/202003/1966300-20200319113638133-1074277093.png)