Java 实验一

1、编写程序,读入圆柱体的半径和高,计算其体积并输出。(教材61页2.2)

import java.util.*;
public class volume {
	public static void main(String[] args){
		System.out.println("Enter the radius and length of a cylinder:");
		Scanner input = new Scanner(System.in);
		double radius = input.nextDouble();
		double length = input.nextDouble();
		final double PI=3.1415926;
		double area =Math.pow(radius)*PI;
		double volume =area*length;
		System.out.println("The area is :"+area);
		System.out.println("The volume is:"+volume);
	}
}

2、读取一个0~1000之间的整数,将该整数各位数字相加,输出得到的和。(教材61页2.6)

import java.util.*;
class digits {
	public static void main(String[] args){
		System.out.println("Enter a number between 0 and 1000:");
		Scanner input = new Scanner(System.in);
		int number = input.nextInt();
		int bai = number/100;
		int shi = number/10%10;
		int ge = number%10;
		int sum = ge + shi +bai;
		System.out.println("The sum of digits is:"+sum);
	}
}

3、随机产生1和12之间的整数,并显示对应的英文月份。(教材94页3.4)

import java.util.*;
class month {
	public static void main(String[] args){
		System.out.println("Enter a number:");
		int number = (int)(Math.random()*12);
		switch(number){
			case 1:  System.out.print("1 January");break;
			case 2:  System.out.print("2 February");break;
			case 3:  System.out.print("3 March");break;
			case 4:  System.out.print("4 April");break;
			case 5:  System.out.print("5 May");break;
			case 6:  System.out.print("6 June");break;
			case 7:  System.out.print("7 July");break;
			case 8:  System.out.print("8 August");break;
			case 9:  System.out.print("9 September");break;
			case 10:  System.out.print("10 October");break;
			case 11:  System.out.print("11 November");break;
			case 12:  System.out.print("12 December");break;
		}

		
	}
}

4、提示用户输入一个点(x, y),检查这个点是否在以原点(0, 0)为圆心,半径为10的圆内。(教材97页3.22)

import java.util.*;
class point{
	public static void main(String[] args){
		System.out.println("Enter the radius and length of a cylinder:");
		Scanner input = new Scanner(System.in);
		double x = input.nextDouble();
		double y = input.nextDouble();
		final double PI=3.1415926;
		double result =Math.pow(x,2)+Math.pow(y,2);
		if(result<=100){
			System.out.println("Point("+x+","+y+ ")is in the ciecle");
		}
		System.out.println("Point("+x+","+y+ ")is not in the ciecle");
	}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值