java第二周上机练习

今天的截图同样是cmd窗口

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

public class Homework0312_01{
	public static void main(String[] args){
		mianJi();
	}
	public static double banJing(){
		System.out.println("请输入半径的长");
		java.util.Scanner s = new java.util.Scanner(System.in);
		double banJing = s.nextDouble();
		while(banJing < 0){
			System.out.println("半径长度不能小于零,请重新输入");
			banJing = s.nextDouble();
		}
		return banJing;
	}
	public static double mianJi(){
		double banJing = banJing();
		double mianJi = banJing * banJing * 3.14;
		System.out.println("面积是" + mianJi);
		return mianJi;
	}
}

 

2.华氏温度和摄氏温度互相转换,从华氏度变成摄氏度你只要减去32,乘以5再除以9就行了,将摄氏度转成华氏度,直接乘以9,除以5,再加上32即行。

public class Homework0312_02{
	public static void main(String[] args){
		huaZhuanShe();

		sheZhuanHua();
	}
	public static double huaShiDu(){
		System.out.println("请输入华氏度");
		java.util.Scanner s = new java.util.Scanner(System.in);
		double hua = s.nextDouble();
		while(hua < -459.67){
			System.out.println("不能低于绝对零度,请重新输入");
			hua = s.nextDouble();
		}
		return hua;
	}
	public static double sheShiDu(){
		System.out.println("请输入摄氏度");
		java.util.Scanner s = new java.util.Scanner(System.in);
		double she = s.nextDouble();
		while(she < -273.15){
			System.out.println("不能低于绝对零度,请重新输入");
			she = s.nextDouble();
		}
		return she;
	}
	public static void huaZhuanShe(){
		double hua = huaShiDu();
		double she = (hua - 32) * 5 / 9;
		System.out.println(hua + "华氏度可转化为" + she + "摄氏度");
		return;
	}
	public static void sheZhuanHua(){
		double she = sheShiDu();
		double hua = (she * 9 / 5) + 32;
		System.out.println(she + "摄氏度可转化为" + hua + "华氏度");
		return;
	}
}

  

3.已知a,b均是整型变量,写出将a,b两个变量中的值互换的程序。

public class Homework0312_03{
	public static void main(String[] args){
		int a = 5;
		int b = 8;
		int c = 0;
		c = a;
		a = b;
		b = c;
		System.out.println("a由5变为" + a + "\tb由8变为" + b);
	}
}

  

4.定义一个任意的5位整数,将它保留到百位,无需四舍五入

public class Homework0312_04{
	public static void main(String[] args){
		int num = num();
		int num2 = (num / 100) * 100;
		System.out.println("保留到百位后等于" + num2);
	}
	public static int num(){
		System.out.println("请输入五位整数");
		java.util.Scanner s = new java.util.Scanner(System.in);
		int num = s.nextInt();
		while(num < 10000 || num > 99999){
			System.out.println("您输入的不是五位数,请重新输入");
			num = s.nextInt();
		}
		return num;
	}
}

  

5.输入一个0~1000的整数,求各位数的和,例如345的结果是3+4+5=12

public class Homework0312_05{
	public static void main(String[] args){
		xiangJia();
	}
	public static int shuRu(){
		System.out.println("请输入一个0~1000的整数");
		java.util.Scanner s = new java.util.Scanner(System.in);
		int num = s.nextInt();
		while (num < 0 || num > 1000){
			System.out.println("您输入的数据有误,请重新输入");
			num = s.nextInt();
		}
		return num;
	}
	public static void xiangJia(){
		int num = shuRu();
		int sum = 0;
		for (int a = 0 ; num > 0; num /= 10){
			a = num % 10;
			sum += a;
		}
		System.out.println("该整数每位相加的和是" + sum);
		return;
	}
}

  

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

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

public class Homework0312_06{
	public static void main(String[] args){
		char daXie = 'A';
		System.out.println("由" + daXie + "转化为" + (char)(daXie + 32));
		char xiaoXie = 'a';
		System.out.println("由" + xiaoXie + "转化为" + (char)(xiaoXie - 32));
	}
}

  

posted @ 2021-03-12 10:06  音无彩名  阅读(58)  评论(0编辑  收藏  举报