Java第二次作业
1.给定一个0~1000的整数,求各位数的和,例如345的结果是3+4+5=12
1 public class Zuoye2 2 { 3 public static void main(String[] args) { 4 int a=345; 5 int ind=a/1000; 6 int ten=a%1000/100; 7 int b=a%100/10; 8 int k=a%10; 9 int sum=ind+ten+b+k; 10 System.out.println(sum); 11 } 12 }
2.华氏温度与摄氏温度互相转换
1 public class Zuoye2 2 { 3 public static void main(String[] args) { 4 float f; 5 float c; 6 f=72; 7 c=f/9*5-32; 8 System.out.println(c); 9 c=56; 10 f=c*9/5+32; 11 System.out.println(f); 12 13 } 14 }
3.编写一个程序,定义圆的半径,求圆的面积.
1 public class Zuoye2 2 { 3 public static void main(String[] args) { 4 double r=4; 5 double l; 6 l=3.14*r*r; 7 System.out.println(l); 8 } 9 }
4.定义一个任意的5位整数,将它保留到百位,无需四舍五入(知识点:变量和运算符综合应用)
1 public class Zuoye2 2 { 3 public static void main(String[] args) { 4 int s=12234 , s1; 5 s1=s/100*100; 6 System.out.println(s1); 7 } 8 }
已知a,b均是整型变量,写出将a,b两个变量中的值互换的程序。(知识点:变量和运算符综合应用)
1 public class Zuoye2 2 { 3 public static void main(String[] args) { 4 int a=2 , b=3; 5 a = a + b; 6 b = a - b; 7 a = a - b; 8 System.out.println(a); 9 System.out.println(b); 10 } 11 }
6.定义一个任意的大写字母A~Z,转换为小写字母(知识点:变量和运算符综合应用)
1 public class Zuoye2 2 { 3 public static void main(String[] args) { 4 char c='A'; 5 System.out.println((char)(c+32)); 6 } 7 }