Java第二次作业

1.编写一个程序,定义圆的半径,求圆的面积。

 1 package week2;
 2 
 3 public class Week1 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         double r = 4.5;
11         double s;
12         s = r * r * 3.14;
13         System.out.println("圆的面积为" + s);
14     }
15 
16 }

2.华氏温度和摄氏温度互相转换,从华氏度变成摄氏度你只要减去32,乘以5再除以9就行了,将摄氏度转成华氏度,直接乘以9,除以5,再加上32即行。(知识点:变量和运算符综合应用)

 1 package week2;
 2 
 3 public class Week2 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         double hua = 50;
11         double she;
12         she = (hua - 32) * 5 / 9;
13         System.out.println("从华氏度变成摄氏度为" + she);
14         double shes = 60;
15         double huas;
16         huas = shes * 9 / 5 + 32;
17         System.out.println("从摄氏度变成华氏度为" + huas);
18     }
19 
20 }

3.已知a,b均是整型变量,写出将a,b两个变量中的值互换的程序。(知识点:变量和运算符综合应用)

 1 package week2;
 2 
 3 public class Week4 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         int a = 1;
11         int b = 2;
12         int c;
13         c = a;
14         a = b;
15         b = c;
16         System.out.println(a);
17         System.out.println(b);
18     }
19 
20 }

4.定义一个任意的5位整数,将它保留到百位,无需四舍五入(知识点:变量和运算符综合应用)

 1 package week2;
 2 
 3 public class Week5 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         int a = 12345;
11         int b;
12         b = a / 100 * 100;
13         System.out.println(b);
14     }
15 
16 }

5.输入一个0~1000的整数,求各位数的和,例如345的结果是3+4+5=12注:分解数字既可以先除后模也可以先模后除(知识点:变量和运算符综合应用)

 1 package second;
 2 
 3 import java.util.*;
 4 
 5 public class Second3 {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         System.out.println("请输入一个0~1000的整数");
13         Scanner sc = new Scanner(System.in);
14         int a = sc.nextInt();
15         int b,c,d,sum;
16         b=a%10;
17         c=a%100/10;
18         d=a/100;
19         sum=b+c+d;
20         System.out.println("结果为"+sum);
21     
22 
23     }
24 
25 }

6.定义一个任意的大写字母A~Z,转换为小写字母(知识点:变量和运算符综合应用)
定义一个任意的小写字母a~z,转换为大写字母

每一个字符都对应一个asc码 A--65 a---97 大写和它的小写差32

 1 package week2;
 2 
 3 public class Week6 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         char c = 'a';
11         char e = 'A';
12         System.out.println((char) (c - 32));
13         System.out.println((char) (e + 32));
14     }
15 }

 

posted @ 2021-03-12 10:26  MXT16  阅读(56)  评论(0编辑  收藏  举报