第二周上机练习

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

public class Test3 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int r=5; 
		double pi=3.14;
        System.out.println(4*pi*r*r);

	}

}

  


2.华氏温度和摄氏温度互相转换,从华氏度变成摄氏度你只要减去32,乘以5再除以9就行了,将摄氏度转成华氏度,直接乘以9,除以5,再加上32即行。(知识点:变量和运算符综合应用)
double hua=50;
//计算它对应的摄氏度
//输出摄氏度是xxxxx
double she=30;
//计算它对应的华氏度
//输出

public class Test3 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		double hua=50;
		double she=30;
		double cTof = (hua-32) * 5/9;
		double fToc = she*9/5+32;
		System. out. println("摄氏转华氏= "+cTof );
		System. out. println("华氏转摄氏= "+fToc );
	}

}

  


3.已知a,b均是整型变量,写出将a,b两个变量中的值互换的程序。(知识点:变量和运算符综合应用)
int a=5,b=8;
//借助一个c交换a b 中的变量

public class Test3 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int a=5;
		int b=8;
		int c=a;
		a=b;
		b=c;
		System.out.println(a);
		System.out.println(b);
	}

}

  


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

12345 12300

public class Test3 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		 int a=12345;
	        int w=12345/10000;
	        int q=12345%10000/1000;
	        int b=12345%10000%1000/100;
	        System.out.println(w*10000+q*1000+b*100);
	}

}

  

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

public class Test3 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		 int n = 2345;
	        int q = n / 1000;
	        int b = n % 1000 / 100;
	        int s = n % 1000 % 100 / 10;
	        int g = n % 1000 % 100 % 10;
	        int sum =q+b+s+g;
	        System.out.println(sum);
	}

}

  


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

public class Test2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		char a='A';
        char b='A'+32;
        System.out.println(b);

	}

}

  

定义一个任意的小写字母a~z,转换为大写字母

public class Test3 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		  char a='a';
	        char b='a'-32;
	        System.out.println(b);
	        
	}

}

  

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

 

posted @ 2021-03-12 09:55  空?  阅读(73)  评论(0编辑  收藏  举报