Java基础程序题Day2

Day2程序题

1.格林威治时间
开发一个以GMT来显示当前时间的程序,以小时:分钟:秒的格式来显示。
System.out.currentTimeMillis()返回从GMT1970年1月1日00:00:00开始到当前时间的毫秒数。

class Showtime{
 public static void main(String[] args){
  /*
  数据:1970年1月1日 0点开始到现在的总毫秒数
  指令:
  1.计算总毫秒数
  2.计算总分钟数
  3.计算总小时数
  4.计算当前小时数
  5.计算当前分钟数
  6.计算当前秒数
  7.输出
  */
  long totalMillis=System.currentTimeMillis();
  long totalSeconds=totalMillis/1000;
  long totalMinutes=totalSeconds/60;
  long totalHours=totalMinutes/60;
  long currentHours=(totalHours+8)%24;
  long currentMinutes=totalMinutes%60;
  long currentSeconds=totalSeconds%60;
  System.out.println(currentHours+":"+currentMinutes+":"+currentSeconds); 
 }
}

2.计算圆柱体的体积
编写程序,读入圆柱体的半径和高,并使用下列公式计算圆柱体的面积:
面积=半径×半径×Pi
体积=面积×高

import java.util.Scanner;
class Demo20{
 public static void main(String[] args){
  /*
  数据:输入一个0~1000之间的整数,把它们各位相加
  指令:
  1.输入一个0~1000之间的整数number
  2.取出个位数
  3.取出十位数
  4.取出百位数
  5.输出
  */
  Scanner scanner=new Scanner(System.in);
  System.out.print("Enter a number between 0 and 1000:");
  int number=scanner.nextInt();
  int sum=0;
  sum+=number%10;
  number=number/10;
  sum+=number%10;
  number=number/10;
  sum+=number%10;
  System.out.println("The sum of the digits is "+sum);
 }
}

3.求一个整数各位数的和
编写程序,读取一个在0和1000之间的整数,并将该整数的各位数字相加。例如:整数932,各位数字相加之和为14.
提示:利用操作符%分解数字,然后用操作符/去掉分解出来的数字。932%10=2,932/10=93

import java.util.Scanner;
class Demo21{
 public static void main(String[] args){
  /*
  数据:半径 高 底面积 体积 圆周率
  指令:
  1.输入半径和高
  2.计算底面积=半径*半径*圆周率
  3.计算体积=底面积*高
  4.输出体积
  */  
  Scanner scanner=new Scanner(System.in);
  System.out.print("Enther the radius and length of a cyclinder:");
  double radius=scanner.nextDouble();
  double length=scanner.nextDouble();
  double area=radius*radius*3.14159265;
  double volume=area*length;
  System.out.printf("The area is %.4f \n",area);
  System.out.printf("The volume is %.1f",volume);
 }
}

4.求出年数
提示用户输入分钟数(例如十亿)然后显示这些分钟代表多少年和多少天。为了简化问题,假设一年有365天。

import java.util.Scanner;
class Demo23{
 public static void main(String[] args){
  /*
  数据:总分钟数 年 天
  指令:
  1.输入总分钟数
  2.计算天数
  3.计算年数
  4.输出多少年多少天
  */
  Scanner scanner=new Scanner(System.in);
  System.out.print("Enter the number of minutes:");
  long totalMinutes=scanner.nextLong();
  long totalDays=totalMinutes/60/24;
  long totalYears=totalDays/365;
  totalDays%=365;
  System.out.println(totalMinutes+" minutes is approximately "+totalYears+" years and "+totalDays+" days");
 }
}

5. 当前时间
程序清单1给出了当前格林威治标准时间的程序。修改这个程序,提示用户输入相对于GMT的时区偏移量,然后显示在这个特定时区的时间。

import java.util.Scanner;
class Demo24{
 public static void main(String[] args){
  /*
  数据:从1970年1月1日 零点开始到现在的总毫秒数 时间偏移量
  指令:
  1.计算总毫秒数
  2.计算总分钟数
  3.计算总小时数
        4.输入时区偏移量
  5.计算当前小时
  6.计算当前分钟
  7.计算当前秒数
  8.输出当前时间
  */
  long totalMillis=System.currentTimeMillis();
  long totalSeconds=totalMillis/1000;
  long totalMinutes=totalSeconds/60;
  long totalHours=totalMinutes/60;
  Scanner scanner=new Scanner(System.in);
  System.out.print("Enter the number of minutes:");
  long timeZoneOffset=scanner.nextLong();
  long currentHours=(totalHours+timeZoneOffset)%24;
  long currentMinutes=totalMinutes%60;
  long currentSeconds=totalSeconds%60;
  System.out.println("The current time is "+currentHours+":"+currentMinutes+":"+currentSeconds);
 }
}

6.几何(两点间的距离)
编写程序,提示用户输入两个点(x1,y1)(x2,y2),然后显示两点间的距离。计算两点间距离的公式是((x2-x1) ^2+(y2-y1) ^2) ^0.5。注意:可以使用Math.pow(a,0.5)来计算a ^0.5。

import java.util.Scanner;
class Demo25{
 public static void main(String[] args){
  /*
  数据:两个点的坐标(x1,y1)(x2,y2)
  指令:
  1.输入两个点的坐标x1,y1,x2,y2
  2.计算两点之间的距离
  3.输出
  */
  Scanner scanner=new Scanner(System.in);
  System.out.print("Enter x1 and y1:");
  double x1=scanner.nextDouble();
  double y1=scanner.nextDouble();
  System.out.print("Enter x2 and y2:");
  double x2=scanner.nextDouble();
  double y2=scanner.nextDouble();
  double distance=Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2));
  System.out.println("The distance between the two points is "+distance);
 }
}

7.几何(三角形的面积)
编写程序,提示用户输入三角形的三个点(x1,y1),(x2,y2)和(x3,y3),然后显示它的面积。计算三角形面积的公式:
s=(边1+边2+边3)/2
面积=(s(s-边1)(s-边2)(s-边3))^0.5

import java.util.Scanner;
class Demo26{
 public static void main(String[] args){
  /*
  数据:3个点的坐标,三个点组成的三角形的面积
  指令:
  1.输入三个点的坐标;
  2.计算三条边的长度
  3.计算s=(边1+边2+边3)/2
  4.计算面积area
  5.输出面积
  */
  Scanner scanner=new Scanner(System.in);
  System.out.print("Enter three points for a triangle:" );
  double x1=scanner.nextDouble();
  double y1=scanner.nextDouble();
  double x2=scanner.nextDouble();
  double y2=scanner.nextDouble();
  double x3=scanner.nextDouble();
  double y3=scanner.nextDouble();
  double distance1=Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2));
  double distance2=Math.sqrt(Math.pow(x3-x2,2)+Math.pow(y3-y2,2));
  double distance3=Math.sqrt(Math.pow(x3-x1,2)+Math.pow(y3-y1,2));
  double s=(distance1+distance2+distance3)/2;
  double area=Math.sqrt(s*(s-distance1)*(s-distance2)*(s-distance3));
  System.out.println("The area of the triangle is "+area);
 }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值