java 练习题
Demo01
public class Demo01 {
public static void main(String[] args) {
System.out.println((9.5 * 4.5 - 2.5 * 3) / (45.5 - 3.5));
}
}
Demo05
public class Demo05{
public static void main(String[] args) {
System.out.println((24 * 1.6) /(1 + 40.35 / 60));
}
}
Demo07
public class Demo07{
public static void main (String[] args) {
long totalMlliseconds = System.currentTimeMillis();
System.out.println(totalMlliseconds);
long totalSeconds = totalMlliseconds / 1000;
long Seconds = totalSeconds % 60;
long totalMinutes = totalSeconds / 60;
long Minutes = totalMinutes % 60;
long totalHours = totalMinutes / 60;
long Hours = totalHours % 24;
System.out.println(Hours + ":" + Minutes + ":" + Seconds);
}
}
Demo09
import java.util.Scanner;
public class Demo09{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a degree in Celsius: ");
double Celsius = in.nextDouble();
double Fahrenheit = ((9.0 / 5) * Celsius) + 32;
System.out.println(Celsius + "Celsius is" + Fahrenheit);
}
}
Demo10
```java
import java.util.Scanner;
public class Demo10 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter radius and length of a cylinder: ");
double r = in.nextDouble();
double l = in.nextDouble();
double s = r * r * 3.14;
double v = s * l;
System.out.println("The area is: " + s);
System.out.println("The volnme is: " + v);
}
}
Demo12
import java.util.Scanner;
public class Demo12{
public static void main (String[] args) {
long totalMlliseconds = System.currentTimeMillis();
System.out.println(totalMlliseconds);
long totalSeconds = totalMlliseconds / 1000;
long Seconds = totalSeconds % 60;
long totalMinutes = totalSeconds / 60;
long Minutes = totalMinutes % 60;
long totalHours = totalMinutes / 60;
long Hours = totalHours % 24;
Scanner in = new Scanner(System.in);
System.out.println("Enter the time zone offset to Gmt:");
int time = in.nextInt();
long H = Hours + time;
System.out.printf("The current time is %d:%d:%d",H, Minutes, Seconds);
}
}
Demo13
import java.util.Scanner;
public class Demo13{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter the amount of water in kilograms: ");
double kilograms = in.nextDouble();
System.out.println("Enter the initial temperature: ");
double temperture = in.nextDouble();
System.out.print("Enter the final temperature :");
double finaltemperture = in.nextDouble();
double Q = kilograms * (finaltemperture - temperture) * 4184;
System.out.print("The energy needed is: " + Q);
}
}
Demo14
import java.util.Scanner;
public class Demo14{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter the monthly saving amount: ");
double money = in.nextDouble();
double sum = 0;
double year = 0.05;
//计算6个月后账户的总钱数 double monRates = year / 12;
//用循环语句简化程序,计算6个月后账户的总钱数
for(int mon = 1; mon < 7; mon++){
sum = (money + sum) * Math.pow(1 + monRates, 1);
}
System.out.print(sum);
}
}
Demo15
import java.util.Scanner;
public class Demo15{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in); //输入点1坐标
System.out.print("Enter x1 and y1 :");
double x1 = scanner.nextDouble();
double y1 = scanner.nextDouble();//输入点2坐标
System.out.print("Enter x2 and y2 :");
double x2 = scanner.nextDouble();
double y2 = scanner.nextDouble();//计算两点间距离
double dis = Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2),0.5);
System.out.println(dis);
}
}
Demo16
import java.util.Scanner;
public class Demo16{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter three points for a triangle:");
//三角形三顶点坐标
double x1 = in.nextDouble();
double y1 = in.nextDouble();
double x2 = in.nextDouble();
double y2 = in.nextDouble();
double x3 = in.nextDouble();
double y3 = in.nextDouble();
//计算三角形三边长度
double l1 = Math.pow(Math.pow((x2 - x1),2)+Math.pow((y2 - y1),2),0.5);
double l2 = Math.pow(Math.pow((x3 - x2),2)+Math.pow((y3 - y2),2),0.5);
double l3 = Math.pow(Math.pow((x1 - x3),2)+Math.pow((y1 - y3),2),0.5);
//计算公式1中s
double s =(l1 + l2 + l3) / 2;
//计算面积
double area = Math.pow(s * (s - l1) * (s - l2) * (s - l3),0.5);
System.out.print("The area of the triangle is :" + area);
}
}
Demo17
```java
import java.util.Scanner;
public class Demo17{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("输入以英磅为单位的体重:");
double bang = in.nextDouble();
System.out.print("输入以英寸为单位的身高:");
double length = in.nextDouble();
//将英磅转换kg 英寸转换为m
double kg = bang / 0.45359237;
double m = length / 0.0254;
//计算BMI
double BMI = kg / (Math.pow(m,2));
//判断健康状况
if(BMI < 18.5){
System.out.print("偏瘦");
}else if(BMI >= 18.5){
System.out.print("正常");
}else if(BMI < 25.0){
System.out.print("正常");
}else if(BMI >= 25.0){
System.out.print("超重");
}else if(BMI < 30){
System.out.print("超重");
}else{
System.out.print("过胖");
}
}
}
Demo19
import java.util.Scanner;
import java.util.Random;
public class Demo19{
public static void main(String[] args){
Random random = new Random();
int num = random.nextInt(90)+10; // 生成一个10到99的随机整数
//System.out.print(num);
Scanner in = new Scanner(System.in);
System.out.print("输入一个两位数:");
int a = in.nextInt();
//将随机二位数与用户二位数都拆分开来
int num1 = num / 10;
int num2 = num % 10;
int a1 = a / 10;
int a2 = a % 10;
//判断被拆分后4个数字之间的关系,来确定用户中的奖项
if(a == num){
System.out.print("中奖,10 000美元");
}else if(a1 == num2 & a2 == num1){
System.out.print("中奖,3 000美元");
}else if(a1 == num1 || a1 == num2 || a2 == num1 || a2 == num2){
System.out.print("中奖,1 000美元");
}else{
System.out.print("未中奖");
}
}
}
Demo21
import java.util.Scanner;
public class Demo21{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter a, b, c, d, e, f :");
double a = in.nextDouble();
double b = in.nextDouble();
double c = in.nextDouble();
double d = in.nextDouble();
double e = in.nextDouble();
double f = in.nextDouble();
//计算分母,并判断方程有无解
if(a * d - b * c == 0){
System.out.print("The equation has no solution");
}else{
double x = (e * d - b * f) / (a * d - b * c);
double y = (a * f - e * a) / (a * d - b * c);
System.out.print("x is " + x);
System.out.print(" and y is " + y);
}
}
}
Demo22
```java
import java.util.Scanner;
public class Demo22{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
//输入今天的星期数
System.out.print("Enter today's day : ");
int day = in.nextInt();
//输入未来过的天数
System.out.print("Enter the number of days elapsed since today : ");
int number = in.nextInt();
//求出过到了未来那一个星期的几天
int day1 = number % 7;
//计算出未来的星期数
int finallyday = day1 + day;
System.out.print("Today is " + day);
System.out.print(" and the future day is " + finallyday);
}
}
Demo23
import java.util.Scanner;
public class Demo23{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter a three-digit : ");
int num = in.nextInt();
//提出百位与各位
int bai = num / 100;
int a = num % 100;
int ge = a % 10;
//判断各位与百位是否相等,相等则是回文数,不相等则不是
if(bai == ge){
System.out.print(num + " is a palindrome");
}else{
System.out.print(num + " is not a palindrome");
}
}
}
Demo25
import java.util.Scanner;
public class Demo25{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
//输入年份
System.out.print("Enter year:(e.g.,2012): ");
int year = in.nextInt();
//输入月份
System.out.print("Enter month:1-12: ");
int m = in.nextInt();
//输入该月的某一天
System.out.print("Enter the day of the month: 1-31: ");
int q = in.nextInt();
int j = (int)Math.ceil(year / 100); //世纪
int k = year % 100; //该世纪的第几年
//1月和2月在该题所给的公式中用13和14表示,将1转换为13,2转换为14
if(m == 1){
m = 13;
}else if(m == 2){
m = 14;
//同时将年份改为前一年
}if(m == 13 | m == 14){
k -= 1;
}
//System.out.print(m + " ");
System.out.print(k + " ");
int h =((q + (26 * (m + 1) / 10) + k + (k / 4) + (j / 4) + 5 * j)) % 7;//公式
System.out.print("Day of the week is " + h);
}
}
Demo27
import java.util.Scanner;
public class Demo27{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter a point with two coordinates:");
double x = in.nextDouble();
double y = in.nextDouble();
//水平距离最小值,垂直距离最小值
double sl = 5.0;
double cl = 2.5;
//给x,y取绝对值
x = Math.abs(x);
y = Math.abs(y);
if(x == sl & y == cl){
System.out.print("在矩形的边上");
}else if(x > sl & y > cl){
System.out.print("在矩形内");
}else{
System.out.print("在矩形外");
}
}
}
Demo29
import java.util.Scanner;
public class Demo29 {
public static void main (String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter r1's center x-, y-coordinates, width, and height: ");
double x1 = in.nextDouble();
double y1 = in.nextDouble();
double w1 = in.nextDouble();
double h1 = in.nextDouble();//提示用户输入第一个矩形(x,y)与长宽。
System.out.println("Enter r2's center x-, y-coordinates, width, and height: ");
double x2 = in.nextDouble();
double y2 = in.nextDouble();
double w2 = in.nextDouble();
double h2 = in.nextDouble();//提示输入第二个矩形的相关数据。
if ((x1 + w1 < x2 + w2) && (x1 - w1 > x2 - w2) && ( y1 + h1 < y2 + h2) && (y1 - h1 > y2 - h2)) {
System.out.print("矩形2在矩形1里面");
//2左边界在1左边界右边,2右边界在1右边界左边,2上边界在1上边界下面,2下边界在1下边界上边。
}else if ((x1 + w1 < x2 - w2) || (x1 - w1 > x2 +w2) && (y1 + h1 < y2 - h2) || (y1 - h1 > y2 + h2)) {
System.out. print("矩形2在矩形1外面");
//若2右边界在1左边界左边,2左边界在1右边界右边,2上边界在1下边界下面,2下边界在1上边界上面。
}else {
System.out.print("矩形2与矩形1重合");
}
}
}
Demo30
import java.util.Scanner;
public class Demo30{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter circle1's center x-,y-coordinates,and radius:");
double x = in.nextDouble();
double y = in.nextDouble();
double r = in.nextDouble();
System.out.print("Enter circle2's center x-,y-coordinates,and radius:");
double x0 = in.nextDouble();
double y0 = in.nextDouble();
double r0 = in.nextDouble();
double d = Math.pow((Math.pow((x - x0),2)+Math.pow((y - y0),2)), 0.5);
if(d <= Math.abs(r - r0)){
System.out.print("circle2?circle1?");
}else if(d <= r0 + r){
System.out.print("circle2?circle??");
}else{
System.out.print("circle2?circle?");
}
}
}
Demo31
import java.util.Scanner;
public class Demo31{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter an integer:");
int num = in.nextInt();
if((num % 5 == 0) && (num % 6 ==0)){
System.out.print("Is num divisible 5 and 6 ? ture");
}else if((num % 5 == 0) || (num % 6 ==0)){
System.out.print("Is num divisible 5 or 6 ? ture");
}else if(num % 5 == 0){
System.out.print("Is num divisible 5 or 6 ,but not both ? ture");
}else if(num % 6 ==0){
System.out.print("Is num divisible 5 or 6 ,but not both ? ture");
}else{
System.out.print("不能被5和6,5或6,5 6中的任一数整除");
}
}
}