Demo01
public class Demo01 {
public static void main(String[] args) {
System.out.println((9.5 * 4.5 - 2.5 * 3) / (45.5 - 3.5));
}
}
0.8392857142857143
Demo02
public class Demo02 {
public static void main(String[] args) {
System.out.println(4 * (1.0 - 1.0 / 3 + 1.0 / 5 - 1.0 / 7 + 1.0 / 9 - 1.0 / 11));
System.out.println(4 * (1.0 - 1.0 / 3 + 1.0 / 5 - 1.0 / 7 + 1.0 / 9 - 1.0 / 11 + 1.0 / 13));
}
}
2.9760461760461765
3.2837384837384844
Demo03
public class Demo03 {
public static void main(String[] args) {
System.out.println("周长 = " + 2 * 5.5 * 3.14);
System.out.println("面积 = " + 5.5 * 5.5 * 3.14);
}
}
周长 = 34.54
面积 = 94.985
Demo04
public class Demo04 {
public static void main(String[] args) {
System.out.println((14 / 1.6) / (45 * 60 + 30) * 60 * 60);
}
}
11.538461538461537
Demo05
public class Demo05 {
public static void main(String[] args) {
System.out.println((24 * 1.6) / ((60 * 60 + 40 * 60 + 35) / 3600));
}
}
38.400000000000006
Demo06
public class Demo06 {
public static void main(String[] args) {
double a = 3.4;
double b = 50.2;
double c = 2.1;
double d = 0.55;
double e = 44.5;
double f = 5.9;
double x = (e * d - b * f) / (a * d - b * c);
double y = (a * f - e * c) / (a * d - b * c);
System.out.println("x = "+ x + "y = "+ y );
}
}
x = 2.623901496861419y = 0.7087397392563978
Demo07
public class Demo07 {
public static void main(String[] args){
long totalMilliseconds = System.currentTimeMillis(); //调用System.currentTimeMillis()获取毫秒数
long totalSeconds = totalMilliseconds / 1000; //通过总毫秒数得到总秒数
long nowSeconds = totalSeconds % 60; //求得当前秒数
long totalMinutes = totalSeconds / 60 ; // 通过总秒数得到总分钟数
long nowMinutes = totalMinutes % 60; //求得当前分钟数
long totalHours = totalMinutes / 60; //通过总分钟数求得总小时数
long nowHours = totalHours % 24; //求得当前小时数
System.out.println("总毫秒数为:" + totalMilliseconds);
System.out.printf("当前时间为:" + nowHours + ":" + nowMinutes + ":" + nowSeconds);
}
}
总毫秒数为:1615800442439
当前时间为:9:27:22
Demo08
import java.util.Scanner;
public class Demo08 {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("请输入总钱数:");
double money = scanner.nextDouble();
money = money * 100; //将钱数转换为1分币
double dollar = (int) money / 100; //求出1美元的个数
money = money % 100; //剩余1分币的个数
double quarter = (int) money / 25; //求出两角五分币的个数
money = money % 25; //剩余1分币的个数
double dime = (int) money / 10; //求出1角币的个数
money = money % 10; //剩余1分币的个数
double nickel = (int) money / 5; //求出5分币的个数
money = money % 5; //剩余1分币的个数
double penny = money;
System.out.print("dollar=" + dollar +" quarter=" + quarter +" dime=" +dime +" nickel=" + nickel + " penny=" + penny);
}
}
请输入总钱数:11.56
dollar=11.0 quarter=2.0 dime=0.0 nickel=1.0 penny=1.0
Demo09
import java.util.Scanner; //导入输入语句的包
public class Demo09 {
public static void main(String[] args){
Scanner Input = new Scanner(System.in);
System.out.print("Enter a degree in Celsius: "); //提示用户输入摄氏温度
double Celsius = Input.nextDouble();
double Fahrenheit = (9.0 / 5) * Celsius + 32; //利用公式计算华氏温度的值
System.out.println(Celsius + "Celsius is" + Fahrenheit + "Fahrenheit");
}
}
Enter a degree in Celsius: 43
43.0Celsius is109.4Fahrenheit
Demo10
import java.util.Scanner;
public class Demo10{
public static void main(String[] args){
Scanner Input = new Scanner(System.in);
System.out.print("Enter the radius length of a cylinder: "); //提示用户输入圆的半径和高
double PI = 3.14; //令π=3.14
double radius = Input.nextDouble();
double height = Input.nextDouble();
double area = radius * radius * PI; //利用公式求得面积
double volume = area * height; //利用公式求得体积
System.out.println("The area is: " + area); //输出结果
System.out.println("The volume is: " + volume);
}
}
Enter the radius length of a cylinder: 5.5 12
The area is: 94.985
The volume is: 1139.82
Demo11
import java.util.Scanner;
public class Demo11 {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number between 0 and 1000: "); //提示用户输入0~1000之间的任意整数
int number = scanner.nextInt();
int a = number % 10; //让该数对10取余,得到个位数字
number = number / 10; //让该数除以10,去掉分解出来的数字
int b = number % 10;
number = number / 10;
int c = number % 10;
number = number / 10;
int sum = a + b + c; //求该整数各位数字相加
System.out.printf("The sum of digits is : %d",sum); //输出结果
}
}
Enter a number between 0 and 1000: 999
The sum of digits is : 27
Demo12
import java.util.Scanner;
public class Demo12{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
long totalMilliseconds = System.currentTimeMillis();
long totalSeconds = totalMilliseconds / 1000;
long currentSeconds = totalSeconds % 60;
long totalMinutes = totalSeconds / 60;
long currentMinutes = totalMinutes % 60;
long totalHours = totalMinutes / 60;
long currentHours = totalHours % 24;
System.out.print("Enter the time zone offset to GTM: ");
byte UTC = in.nextByte();
System.out.print("The current time is " + (currentHours + UTC) + " : " + currentMinutes + " : " + currentSeconds);
}
}
Enter the time zone offset to GTM: -5
The current time is 3 : 56 : 28
Demo13
import java.util.Scanner; //导入一个输入的包
public class Demo13 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount of water in kilograms: "); //提示用户输入水的重量
double kilograms = input.nextDouble();
System.out.print("Enter the initial temperature: "); //提示用户输入水的初始温度
double initialTemperature = input.nextDouble();
System.out.print("Enter the final temperature: "); //提示用户输入水的最终温度
double finalTemperature = input.nextDouble();
double Q = kilograms * (finalTemperature - initialTemperature) * 4184; //利用公式计算结果
System.out.println("The energy needed is: " + Q); //输出结果
}
}
Enter the amount of water in kilograms: 55.5
Enter the initial temperature: 3.5
Enter the final temperature: 10.5
The energy needed is: 1625484.0
Demo14
import java.util.Scanner;
public class Demo14 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter the monthly saving amount: "); //提示用户输入账户存款
double money = input.nextDouble();
double oneMonth = money * (1 + 0.00417);
double twoMonth = (oneMonth + money) * (1 + 0.00417);
double threeMonth = (twoMonth + money) * (1 + 0.0417);
double fourMonth = (threeMonth + money) * (1 + 0.00417);
double fiveMonth = (fourMonth + money) * (1 + 0.00417);
double sixMonth = (fiveMonth + money) * (1 + 0.0417);
System.out.println("After the six month, the account value is: " + sixMonth);
}
}
Enter the monthly saving amount: 100
After the six month, the account value is: 643.4480801029789
Demo15
import java.util.Scanner;
public class Demo15{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("输入第一个点的坐标:");
double x1 = scanner.nextDouble();
double y1 = scanner.nextDouble();
System.out.print("输入第二个点的坐标:");
double x2 = scanner.nextDouble();
double y2 = scanner.nextDouble();
double distance = Math.pow(Math.pow(x1 - x2,2) + Math.pow(y1 - y2,2),0.5);
System.out.println(distance);
}
}
输入第一个点的坐标:1.5 -3.4
输入第二个点的坐标:4 5
8.764131445842194
Demo16
import java.util.Scanner;
public class Demo16 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter three points for a triangle: ");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double x3 = input.nextDouble();
double y3 = input.nextDouble();
double d1 = Math.sqrt(Math.pow(x1 - x2,2) + Math.pow(y1 - y2,2));
double d2 = Math.sqrt(Math.pow(x2 - x3,2) + Math.pow(y2 - y3,2));
double d3 = Math.sqrt(Math.pow(x3 - x1,2) + Math.pow(y3 - y1,2));
double s = (d1 + d2 + d3) / 2;
double area = Math.sqrt(s * (s - d1) * (s - d2) * (s - d3));
System.out.print("The area of the triangle is: " + area);
}
}
Enter three points for a triangle: 1.5 -3.4 4.6 5 9.5 -3.4
The area of the triangle is: 33.600000000000016
Demo17
import java.util.Scanner;
public class Demo17{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("请输入体重(磅)和身高(英寸):");
double weight = input.nextDouble();
double height = input.nextDouble();
double Weight = weight * 0.45359237;
double Height = height * 0.0254;
double BMI = Weight / Height;
if(BMI < 18.5){
System.out.println("偏瘦");
}else if(18.5 <= BMI & BMI < 25.0){
System.out.println("正常");
}else if(25.0 <= BMI & BMI < 30.0){
System.out.println("超重");
}else if(30.0 <= BMI){
System.out.println("过胖");
}
}
}
请输入体重(磅)和身高(英寸):119 63
过胖
Demo18
public class Demo18 {
public static void main(String[] args) {
int year = 2021;
if (year % 4 == 0 && year % 100 !=0 || year % 400 == 0) {
System.out.println("闰年");
} else {
System.out.println("平年");
}
}
}
平年
Demo19
import java.util.Scanner;
public class Demo19{
public static void main(String[] args){
int a = (int)(Math.random() * 90 + 10); //使用(int)(Math.random() * 90 + 10)产生随机数
System.out.print("本次中奖号码为: " + a);
Scanner input = new Scanner(System.in);
System.out.print("请输入一个两位数: ");
int b = input.nextInt(); //输入数
int ssw = a / 10; //随机数的十位
int sgw = a % 10; //随机数的个位
int srsw = b / 10; //输入数的十位
int srgw = b % 10; //输入数的个位
if(a == b){
System.out.println("奖金为10000美元");
}else if(sgw == srsw && ssw == srgw){
System.out.println("奖金为3000美元");
}else if(ssw == srsw || ssw == srgw || sgw == srsw || sgw ==srgw){
System.out.println("奖金为1000美元");
}
}
}
本次中奖号码为: 57请输入一个两位数: 57
奖金为10000美元
本次中奖号码为: 26请输入一个两位数: 62
奖金为3000美元
本次中奖号码为: 53请输入一个两位数: 56
奖金为1000美元
Demo20
import java.util.Scanner;
public class Demo20{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a,b,c : ");
double a = scanner.nextDouble();
double b = scanner.nextDouble();
double c = scanner.nextDouble();
double delt = b * b - 4 * a * c;
if(delt > 0){
double x1 = (-b + Math.pow(delt,0.5))/(2*a);
double x2 = (-b - Math.pow(delt,0.5))/(2*a);
System.out.printf("Has two roots %.2f,%2f",x1,x2);
}else if(delt == 0){
double x1 = (-b )/(2*a);
System.out.printf("Has one root %2f",x1);
}else{
System.out.printf("Has no root");
}
}
}
Enter a,b,c : 1.0 3 1
Has two roots -0.38,-2.61803
Enter a,b,c : 1.2 0 1
Has no root
Enter a,b,c : 1 2 3
Has no root
Demo21
import java.util.Scanner;
public class Demo21 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a, b, c, d, e, f: ");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
double d = input.nextDouble();
double e = input.nextDouble();
double f = input.nextDouble();
double z = a * d - b * c;
double x = (e * d - b * f) / (a * d - b * c);
double y = (a * f - e * c) / (a * d - b * c);
if(z == 0){
System.out.println("The equation has no solution");
}else{
System.out.println("x = "+ x + "y = "+ y );
}
}
}
Enter a, b, c, d, e, f: 9.0 4.0 3.0 -5.0 -6.0 -21.0
x = -2.0y = 3.0
Enter a, b, c, d, e, f: 1.0 2.0 2.0 4.0 4.0 5.0
The equation has no solution
Demo22
import java.util.Scanner;
public class Demo22{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter today's day: ");
int day = input.nextInt();
System.out.print("Enter the number of days elapsed since today: ");
int today = input.nextInt();
if(today == 0){
System.out.println("Today is Sunday");
}else if(today == 1){
System.out.println("Today is Monday");
}else if(today == 2){
System.out.println("Today is Tuesday");
}else if(today == 3){
System.out.println("Today is Wednesday");
}else if(today == 4){
System.out.println("Today is Thursday");
}else if(today == 5){
System.out.println("Today is Friday");
}else if(today == 6){
System.out.println("Today is Saturday");
}
day = today % 7;
if(day == 0){
System.out.println("and the future day is Sunday");
}else if(day == 1){
System.out.println("and the future day is Monday");
}else if(day == 2){
System.out.println("and the future day is Tuesday");
}else if(day == 3){
System.out.println("and the future day is Wednesday");
}else if(day == 4){
System.out.println("and the future day is Thursday");
}else if(day == 5){
System.out.println("and the future day is Friday");
}else if(day == 6){
System.out.println("and the future day is Saturday");
}
}
}
Enter today's day: 1
Enter the number of days elapsed since today: 3
Today is Wednesday
and the future day is Wednesday
Demo23
import java.util.Scanner;
public class Demo23{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter a three-digit integer: ");
int number = input.nextInt();
int gewei = number / 100;
int baiwei = number % 10;
if(gewei == baiwei){
System.out.print(number + " is a palindrome");
}else{
System.out.print(number + " is not a palindrome");
}
}
}
Enter a three-digit integer: 121
121 is a palindrome
Enter a three-digit integer: 123
123 is not a palindrome
Demo24
import java.util.Scanner;
public class Demo24{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
final int userGuess,computerGuess = (int)(Math.random()*3);//电脑随机产生一个数
System.out.print("scissor(0), rock(1), paper(2): ");
userGuess = input.nextInt();
if(computerGuess == 0) // 电脑出剪刀
{
if(userGuess == 0)
{
System.out.print("The computer is scissor.");
System.out.print("You are scissor too.");
System.out.print("It is a draw");
}
else if(userGuess == 1)
{
System.out.print("The computer is scissor.");
System.out.print("You are rock.");
System.out.print("You won");
}
else if(userGuess == 2)
{
System.out.print("The computer is scissor.");
System.out.print("You are paper.");
System.out.print("You lost");
}
}
else if(computerGuess == 1) // 电脑出石头
{
if(userGuess == 0)
{
System.out.print("The computer is rock.");
System.out.print("You are scissor.");
System.out.print("You lost");
}
else if(userGuess == 1)
{
System.out.print("The computer is rock.");
System.out.print("You are rock too.");
System.out.print("It is a draw");
}
else if(userGuess == 2)
{
System.out.print("The computer is rock.");
System.out.print("You are paper.");
System.out.print("You won");
}
}
else //电脑出布
{
if(userGuess == 0)
{
System.out.print("The computer is paper.");
System.out.print("You are scissor.");
System.out.print("You won");
}
else if(userGuess == 1)
{
System.out.print("The computer is paper.");
System.out.print("You are rock.");
System.out.print("You lost");
}
else if(userGuess == 2)
{
System.out.print("The computer is paper.");
System.out.print("You are paper too.");
System.out.print("It is a draw");
}
}
}
}
scissor(0), rock(1), paper(2): 1
The computer is paper.You are rock.You lost
Demo25
import java.util.Scanner;
public class Demo25{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter a year: (e.g, 2012): ");
int year = input.nextInt();
System.out.print("Enter a month: 1-12: ");
int m = input.nextInt();
if(m == 1 || m == 2){
m = m + 12;
year = year - 1;
}
System.out.print("Enter the day of the month: 1-31 ");
int q = input.nextInt();
int h = (q + 26 * (m + 1) / 10 + year % 100 + year % 100 /4 + year / 100 / 4 + 5 * year / 100) % 7;
if(h == 0){
System.out.println("Day of the week is Saturday");
}else if(h == 1){
System.out.println("Day of the week is Sunday");
}else if(h == 2){
System.out.println("Day of the week is Monday");
}else if(h == 3){
System.out.println("Day of the week is Tuesday");
}else if(h == 4){
System.out.println("Day of the week is Wednesday");
}else if(h == 5){
System.out.println("Day of the week is Thursday");
}else if(h == 6){
System.out.println("Day of the week is Friday");
}
}
}
Enter a year: (e.g, 2012): 2015
Enter a month: 1-12: 1
Enter the day of the month: 1-31 25
Day of the week is Sunday
Demo26
import java.util.Scanner;
public class Demo26{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter a point with two coordinates: ");
double x = input.nextDouble();
double y = input.nextDouble();
double distance = Math.pow((x * x + y * y) , 0.5);
if (distance <= 10){
System.out.println("point" + "(" + x + "," + y + ")" + "is in the circle");
} else {
System.out.println("point" + "(" + x + "," + y + ")" + "is not in the circle");
}
}
}
Enter a point with two coordinates: 4 5
point(4.0,5.0)is in the circle
Enter a point with two coordinates: 9 9
point(9.0,9.0)is not in the circle
Demo27
import java.util.Scanner;
public class Demo27{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter a point with two coordinates: ");
double x = input.nextDouble();
double y = input.nextDouble();
double xdistance = x;
double ydistance = y;
if (xdistance <= 10 / 2 & ydistance <= 5.0 / 2){
System.out.println("point" + "(" + x + "," + y + ")" + "is in the rectangle");
} else {
System.out.println("point" + "(" + x + "," + y + ")" + "is not in the rectangle");
}
}
}
Enter a point with two coordinates: 2 2
point(2.0,2.0)is in the rectangle
Enter a point with two coordinates: 6 4
point(6.0,4.0)is not in the rectangle
Demo28
import java.util.Scanner;
public class Demo28{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a point's x- and y-coordinates: ");
double x = scanner.nextDouble();
double y = scanner.nextDouble();
if(x >= 0 && x <= 200 || y >= 0 && y <= 100){
if(y / (200 - x) <= 0.5){
System.out.println("in triangle");
}else{
System.out.println("out triangle in ranctangla");
}
}else{
System.out.println("out ranctangla");
}
}
}
Enter a point's x- and y-coordinates: 100.5 25.5
in triangle
Enter a point's x- and y-coordinates: 100.5 50.5
out triangle in ranctangla
Demo29
import java.util.Scanner;
public class Demo29{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter r1's center x, y, width, and height: ");//提示用户输入两个矩形中点的坐标及它们的宽度和高度
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double w1 = input.nextDouble();
double h1 = input.nextDouble();
System.out.print("Enter r2's center x, y, width, and height: ");//提示用户输入两个矩形中点的坐标及它们的宽度和高度
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double w2 = input.nextDouble();
double h2 = input.nextDouble();
double a = x1 - (w1 - w2) / 2;//小矩形在大矩形里的左临界距离
double b = x1 + (w1 - w2) / 2;//小矩形在大矩形里的右临界距离
double c = y1 - (h1 - h2) / 2;//小矩形在大矩形里的上临界距离
double d = y1 + (h1 - h2) / 2;//小矩形在大矩形里的下临界距离
double a1 = x1 - (w1 + w2) / 2;//小矩形在大矩形外的左临界距离
double b1 = x1 + (w1 + w2) / 2;//小矩形在大矩形外的右临界距离
double c1 = y1 - (h1 + h2) / 2;//小矩形在大矩形外的上临界距离
double d1 = y1 + (h1 + h2) / 2;//小矩形在大矩形外的下临界距离
if(x2 >= a & x2 <= b & y2 >= c & y2 <= d){
System.out.println("r2 is inside r1");
}else if(x2 >= a1 & x2 <= b1 & y2 >= c1 & y2 <= d1){
System.out.println("r2 overlaps r1");
}else {
System.out.println("r2 does not overlap r1");
}
}
}
Enter r1's x-, y-coordinates, width, and height: 1 2 3 5.5
Enter r2's x-, y-coordinates, width, and height: 3 4 4.5 5
r2 overlaps r1
Enter r1's x-, y-coordinates, width, and height: 1 2 3 3
Enter r2's x-, y-coordinates, width, and height: 40 45 3 2
r2 does not overlap r1
Demo30
import java.util.Scanner;
public class Demo30{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter circle1's center x-,y-coordinates,and radius: ");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double r1 = input.nextDouble();
System.out.print("Enter circle2's center x-,y-coordinates,and radius: ");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double r2 = input.nextDouble();
double d = Math.pow((Math.pow(x1 - x2, 2)+Math.pow(y1 - y2, 2)), 0.5);
if(r1 + r2 >= d){
System.out.println("circle2 is inside circle1");
}
else if(r1 - r2 >= d){
System.out.println("circle2 overlaps circle1");
}else{
System.out.println("circle2 does not overlap circle1");
}
}
}
Enter circle1's center x-,y-coordinates, and radius: 0.5 5.1 13
Enter circle2's center x-,y-coordinates, and radius: 1 1.7 4.5
circle2 is inside circle1
Demo31
import java.util.Scanner;
public class Demo31{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = input.nextInt();
int a = number % 5;
int b = number % 6;
if(a == 0 & b == 0){
System.out.println("Is 10 divisible by 5 and 6? true");
}
else{
System.out.println("Is 10 divisible by 5 and 6? flase");
}
if(a == 0 || b == 0){
System.out.println("Is 10 divisible by 5 or 6? true");
System.out.println("Is 10 divisible by 5 or 6, but not both? flase");
}else{
System.out.println("Is 10 divisible by 5 or 6? flase");
System.out.println("Is 10 divisible by 5 or 6, but not both? flase");
}
}
}
Enter an integer: 10
Is 10 divisible by 5 and 6? flase
Is 10 divisible by 5 or 6? true
Is 10 divisible by 5 or 6, but not both? flase