Demo01
解题思路:
题中的数据:公式中数字
题目要求:显示公式结果
步骤:
根据题目将运算输出即可
代码:
public class Demo01{
public static void main(String[] args){
System.out.println((9.5 * 4.5 - 2.5 * 3) / (45.5 - 3.5));
}
}
Demo02
解题思路:
题中的数据:公式中的数字,程序中用1.0代替1
题目要求:根据题中公式求出近似p,显示两个运算的结果。
步骤:
根据题目将运算输出即可,注意要求程序中1.0代替1
代码:
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));
}
}
Demo03
解题思路:
题中的数据:周长、面积、半径(5.5)、圆周率(3.1415926)
题目要求:根据公式显示半径为5.5的圆的面积和周长
步骤:
1.定义变量:半径、面积、周长;
2.根据题目要求写出周长、面积公式;
3.输出面积和周长
代码:
public class Demo03{
public static void main(String[] args){
double r = 5.5;
double c = 2 * r * 3.1415;
double s = r * r * 3.1415;
System.out.println("c = " + c );
System.out.println("s = " + s );
}
}
Demo04
解题思路:
题中的数据:45分钟30秒,14公里,1英里=1.6公里
题目要求:显示每小时英里为单位的平均速度值
步骤:
1.将14公里转换为英里;
2.将45分钟30秒,化为纯秒数;
3.将纯秒数转换为小时数;
4.根据平均速度公式v=s/t求出平均速度;
代码:
public class Demo04 {
public static void main(String[] args) {
System.out.println(14 / 1.6 / (45 * 60 + 30) * 60 * 60);
}
}
Demo05
Demo05
解题思路:
题中的数据:1小时40分钟35秒,24英里,1英里=1.6公里
题目要求:显示每小时公里为单位的平均速度值
步骤:
1.将24英里转换为公里;
2.1小时40分钟35秒,化为纯秒数;
3.将纯秒数转换为小时数;
4.根据平均速度公式v=s/t求出平均速度;
代码:
public class Demo05{
public static void main(String[] args){
System.out.println( 24 * 1.6 / ( 60 * 60 + 40 * 60 + 35 ) * 60 * 60 );
}
}
Demo06
解题思路:
题中的数据:已知a,b,c,d,e,f的值,未知x,y的值
题目要求:求解方程组并显示x,y的值
步骤:
1.根据要求将x所需的值带入公式;
2.根据要求将y所需的值带入公式;
3.输出x,y
代码:
public class Demo06 {
public static void main(String[] args){
System.out.print("x = ");
System.out.print((44.5 * 0.55 - 50.2 * 5.9) / (3.4 * 0.55 - 50.2 * 2.1));
System.out.print(" y = ");
System.out.print((3.4 * 5.9 - 44.5 * 2.1) / (3.4 * 0.55 - 50.2 * 2.1));
}
}
Demo07
解题思路:
题中的数据:
1.1970年1月1日00:00:00到现在的总毫秒数:totalMilliSeconds
2.1970年1月1日00:00:00到现在的总秒数:totalSeconds
3.当前的秒数:currentSecond
4.1970年1月1日00:00:00到现在的总分钟数:totalMinutes
5.当前的分钟数:currentMinute
6.1970年1月1日00:00:00到现在的总小时数:totalHour
7.当前的小时数:currentHour
题目要求:根据1970年1月1日00:00:00到现在的总毫秒数,获取当前的格林威治时间
步骤:
1.由于1970年1月1日00:00:00到现在的总毫秒数过于庞大,因此程序中最好用长整型long来定义变量;
2.获得系统的时间,单位为毫秒,转换为秒,totalMilliSeconds除以1000,获得totalSeconds;
3.求出现在的秒,totalSeconds余60,获得currentSecond;
4.求出现在的分,totalSeconds除以60,获得totalMinutes,totalMinutes余60,获得currentMinute;
5.求出现在的小时,totalMinutes除以60,获得totalHour,totalHour%24,获得currentHour
6.显示时间
代码:
public class Demo07{
public static void main(String[] args){
//获得系统的时间,单位为毫秒,转换为秒
long totalMilliSeconds = System.currentTimeMillis();
long totalSeconds = totalMilliSeconds / 1000;
//求出现在的秒
long currentSecond = totalSeconds % 60;
//求出现在的分
long totalMinutes = totalSeconds / 60;
long currentMinute = totalMinutes % 60;
//求出现在的小时
long totalHour = totalMinutes / 60;
long currentHour = totalHour % 24;
//显示时间
System.out.println("总毫秒为: " + totalMilliSeconds);
System.out.println(currentHour + ":" + currentMinute + ":" + currentSecond + " GMT");
}
}
Demo08
解题思路:
题中的数据:用户输入的总钱数,总钱数等价的dollar(1美元)、quarter(2角5分)、dime(1角)、nickel(5分)、penny(1分)
题目要求:列出总钱数等价的最大数量的dollar(1美元)、quarter(2角5分)、dime(1角)、nickel(5分)、penny(1分)的数名,并且按照此顺序,从而使得硬币最少。
步骤:
1.将钱数转换为美分数量(11.56——>1156)
2.找出美元数量
3.找quarter数量
4.找dime数量
5.找nickel数量
6.找penny数量
7.显示
代码:
import java.util.Scanner;
public class Demo08 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("输入一个double型的数(例如11.56):");
double money = scanner.nextDouble();
//将钱数转换为美分数量(11.56——>1156)
int temppenny = (int)(money * 100);
//找出美元数量
int dollars = temppenny / 100;
temppenny = temppenny % 100;
//找quarter数量
int quarters = temppenny / 25;
temppenny = temppenny % 25;
//找dime数量
int dimes = temppenny / 10;
temppenny = temppenny % 10;
//找nickel数量
int nickels = temppenny / 5;
temppenny = temppenny % 5;
//找penny数量
int pennys = temppenny / 1;
//显示
System.out.println("你的钱数 " + money);
System.out.println(dollars + " dollars");
System.out.println(quarters + " quarters");
System.out.println(dimes + " dimes");
System.out.println(nickels + " nickels");
System.out.println(pennys + " pennies");
}
}
Demo09
解题思路:
题中的数据:摄氏度,华氏度
题目要求:输入摄氏度转换为华氏度
步骤:
1.提示用户输入摄氏度
2.根据公式将摄氏温度转换为华氏温度
3.输出
代码:
import java.util.Scanner;
public class Demo09{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a degree in Celsius:");
double Celsius = scanner.nextDouble();
//将摄氏温度转换为华氏温度
double Fahrenheit = ((9.0 / 5) * Celsius) + 32;
//输出
System.out.println(Celsius + " Celsius is " + Fahrenheit + " Fahrenheit ");
}
}
Demo10
解题思路:
题中的数据:面积、半径、体积、高
题目要求:读入圆柱体的半径、高,计算其体积
步骤:
1.提示用户输入半径和高
2.根据公式计算圆柱体体积
3.输出
代码:
import java.util.Scanner;
public class Demo10 {
public static void main (String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter the radius and lenght of a cylinder:");
double r = in.nextDouble();
double l = in.nextDouble();
//计算圆柱体体积
double s = r * r * 3.1415926;
double v = s * l;
System.out.println("The area is: " + s);
System.out.println("The volume is: " + v);
}
}
Demo11
解题思路:
题中的数据:0~1000间的一个随机整数
题目要求:该数各位数相加
步骤:
1.提示用户输入一个0~1000间的一个随机整数;
2.求出它的个位数字;
3.求出它的十位数字
4.求出它的百位数字
5.个、十、百相加
6.输出相加结果
代码:
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 0 and 1000:");
int num = scanner.nextInt();
//各位
int a = num % 10;
num /= 10;
//十位
int b = num % 10;
num /= 10;
//百位
int c = num % 10;
num /= 10;
//各位数相加
int sum = a + b + c ;
System.out.println(sum);
}
}
Demo12
解题思路:
题中的数据:同上面Demo07,(此题多的)偏移量
题目要求:通过对于GMT的偏移量,显示特定时区的时间
步骤:
1.同上面Demo07
2.(此题多的)根据格林威治时间的小时数加减一个数(相对偏移量),得出区域特定时间
3.显示区域时间
代码:
import java.util.Scanner;
public class Demo12{
public static void main(String[] args){
//输入相对于GMT的时区偏移量
Scanner in = new Scanner(System.in);
System.out.print("Enter the time zone offset to GMT:");
//获得系统的时间,单位为毫秒,转换为秒
long totalMilliSeconds = System.currentTimeMillis();
long totalSeconds = totalMilliSeconds / 1000;
//求出现在的秒
long currentSecond = totalSeconds % 60;
//求出现在的分
long totalMinutes = totalSeconds / 60;
long currentMinute = totalMinutes % 60;
//求出现在的小时
long totalHour = totalMinutes / 60;
long currentHour = totalHour % 24;
//计算当前时区时间
int areatime = in.nextInt();
long currentTimearea = currentHour + areatime;
//显示时间
System.out.println("总毫秒为: " + totalMilliSeconds);
System.out.println(currentHour + ":" + currentMinute + ":" + currentSecond + " GMT");
System.out.println("当前时区时间为:" + currentTimearea + ":" + currentMinute + ":" + currentSecond );
}
}
Demo13
解题思路:
题中的数据:水的质量、初始温度、最终温度、所需能量
题目要求:输出一定质量下,水由初始温度加热到最终温度所需的能量
步骤:
1.提示用户输入水的质量、初始温度、最终温度
2.根据题中所给公式计算所需能量
3.输出结果
代码:
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.print("Enter the initial temperature: ");
double temperature = in.nextDouble();
System.out.print("Enter the final temperature: ");
double finaltemperature = in.nextDouble();
//根据公式计算,水由初始温度加热到最终温度所需的能量
double Q = Kilograms * (finaltemperature - temperature) * 4184;
System.out.print("The energy needed is: " + Q);
}
}
Demo14
解题思路:
题中的数据:每月存入100、年利率、月利率、最终总钱数(6个月后)
题目要求:在每月存入100美元的情况下,加上之前的总钱数,得出6个月后的总钱数
步骤:
1.提示用户输入每个月存入的钱数;
2.定义各个数据的数据类型;
3.计算月利率
4.计算6个月后账户的总钱数
5.输出
代码:
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
解题思路:
题中的数据:两个点的坐标,两点间距离公式
题目要求:计算两点间距离
步骤:
1.提示用户输入两点坐标;
2.根据两点间距离公式计算两点间距离,根据Math.pow(a,x)可计算n次方,根号
3.输出
代码:
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
解题思路:
题中的数据:三角形三个顶点的坐标
题目要求:根据题中公式计算三角形面积
步骤:
1.提示用户输入三角形三个顶点的坐标
2.由题中公式可以看出,需根据两点间距离公式,计算三角形三边长度
3.根据公式一,求出s
4.根据公式二,带入s,以及三角形三边长度,求出面积
5输出
代码:
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
解题思路:
题中的数据:体重(英磅)、体重(kg)、身高(英寸)、身高(m)、BMI、1磅=0.45359237kg 、一英寸=0.0254m
题目要求:将体重(英磅)、身高(英寸)转化为 体重(kg)、身高(m)计算出BMI,并根据BMI判断健康状况
步骤:
1.提示用户输入体重(英磅)、身高(英寸)
2.将英磅转换kg
3.将英寸转换为m
4.体重(kg)除以 身高(m)^2 得到BMI
5使用判断语句判断健康状况
6.输出
代码:
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("过胖");
}
}
}
Demo18
解题思路:
题中的数据:某年
题目要求:判断某年是平年还是闰年
步骤:
1.输入年份
2.判断是平年还是闰年
3.输出
代码:
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
解题思路:
题中的数据:一个两位随机数,用户输入的两位数字
题目要求:对比用户数字与随机数,判断用户是否中奖
步骤:
1.根据Random类生成一个10~99一共90个随机二位数
2.提示用户输入一个二位数
3.将随机二位数与用户二位数都拆分开来
4.判断被拆分后4个数字之间的关系,来确定用户中的奖项
5.告之用户中奖及奖金额,或者未中奖
代码:
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("未中奖");
}
}
}
Demo20
解题思路:
题中的数据:一元二次方程的系数,及常数c
题目要求:输入a,b,c判断方程式的求解状况
步骤:
1.提示用户输入a,b,c
2.求出一元二次方程判别式的值
3.根据一元二次方程判别式的值,判断方程有无实数根、有几个实数根以及有实数根的状况下方程的解为多少
4.输出结果
代码:
import java.util.Scanner;
public class Demo20{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter a , b , c :");
double a = in.nextDouble();
double b = in.nextDouble();
double c = in.nextDouble();
//求detl
double delt = b * b - 4 * a * c ;
//判断求解状况,并输出结果
if(delt > 0){
double x1 = (-b + Math.sqrt(delt) / 2 * a );
double x2 = (-b - Math.sqrt(delt) / 2 * a );
System.out.printf("two roots: %.2f,%.2f", x1 , x2);
}else if(delt == 0){
double x1 = -b / (2 * a);
System.out.println("one roots" + x1);
}else{
System.out.println("no roots");
}
}
}
Demo21
解题思路:
题中的数据:方程组的系数(abcd)、e、f、公式的分母
题目要求:先判断分母是否为零(输出无解),不为零时求出方程的解
步骤:
1.提示用户输入abcdef
2.计算分母
3.判断分母是否为零,为零无解
4.分母不为零,则根据题目公式计算出方程的解
5.输出方程的解
代码:
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
解题思路:
题中的数据:今天的星期数、今天之后未来的天数
题目要求:判断未来多少天之后为星期几
步骤:
1.提示用户输入今天的星期数
2.提示用户输入未来的天数
3.未来的天数余7,求出过到了未来那一个星期的几天
4.今天的星期数加上过到了未来那一个星期的几天,计算出未来是星期几
5.输出今天以及未来的星期数
代码:
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
解题思路:
题中的数据:用户输入的三位整数
题目要求:判断该数字是否为回文数
步骤:
1.提示用户输入一个三位整数
2.将这个三位数的个十百的数字拆分开来
3.判断各位与百位是否相等,相等则是回文数,不相等则不是
4.输出判断结果
代码:
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");
}
}
}
Demo24
解题思路:
题中的数据:随机生成的0、1、2的任一个,用户输入的0、1、2的任一个
题目要求:电脑生成0、1、2的随机数(剪刀、石头、布)与用户输入的0、1、2(剪刀、石头、布)的随机数对比判断,并且输出双方出的什么、谁输、谁赢、平局
步骤:
1.通过Random生成随机数0、1、2
2.提示用户输入0、1、2
3.判断电脑与用户是否为平局局、电脑胜局还是人胜利局
4.输出结果
代码:
import java.util.Scanner;
import java.util.Random;
public class Demo24{
public static void main(String[] args){
Random random = new Random();
int num = random.nextInt(3)+0; // 生成一个0到2的随机整数
Scanner in = new Scanner(System.in);
System.out.print("scissor(0),rock(1),paper(2): ");
int num1 = in.nextInt();
//平局局
if(num == 0 & num1 == 0){
System.out.print("The computer is scissor .You are is scissor .It is a draw");
}else if(num == 1 & num1 == 1){
System.out.print("The computer is rock .You are is rock .It is a draw");
}else if(num == 2 & num1 == 2){
System.out.print("The computer is paper .You are is paper .It is a draw");
//电脑胜局
}else if(num == 0 & num1 == 2){
System.out.print("The computer is scissor .You are is paper .computer won");
}else if(num == 1 & num1 == 2){
System.out.print("The computer is rock .You are is scissor .computer won");
}else if(num == 2 & num1 == 1){
System.out.print("The computer is paper .You are is rock .computer won");
//人胜利局
}else if(num == 0 & num1 == 1){
System.out.print("The computer is scissor .You are is rock .You won");
}else if(num == 1 & num1 == 2){
System.out.print("The computer is rock .You are is paper .You won");
}else if(num == 2 & num1 == 0){
System.out.print("The computer is paper .You are is rock .You won");
}else{
System.out.print("Please Enter 0, 1, 2");
}
}
}
Demo25
解题思路:
题中的数据:h一个星期中的某一天、q某月的第几天、m月份、j世纪数、k该世纪的第几年
题目要求:根据题目所给公式,输入年份、月份、某月的第几天,计算出某天是星期几
步骤:
1.提示用户输入年、月、该月的某一天
2.通过年除以100并向上取整计算出世纪数
3.通过年余100计算出是该世纪的第几年
4.1月和2月在该题所给的公式中用13和14表示,将1转换为13,2转换为14
5.同时将年份改为前一年
6.将所有数值代入公式中求得h,计算出这天为星期几
7.输出结果
代码:
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);
}
}
Demo26
解题思路:
题中的数据:以(0,0)为圆心,10为半径的圆,(x,y)
题目要求:判断用户输入的点与圆的关系
步骤:
1.提示用户输入点的坐标
2.构建圆的基本参数
3.通过两点间距离公式计算出点到圆心的距离
4.通过点到愿心的距离与半径作比较判断出点与圆的位置关系
5.输出结果
代码:
import java.util.Scanner;
public class Demo26{
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 x0 = 0;
double y0 = 0;
double r = 10;
//点到圆心的距离
double d = Math.pow((Math.pow((x - x0),2)+Math.pow((y - y0),2)), 0.5);
//判断位置关系
if(d == r){
System.out.print("在圆上");
}else if(d > r){
System.out.print("在圆外");
}else{
System.out.print("在圆内");
}
}
}
Demo27
解题思路:
题中的数据:以原点为中心、宽为10、高为5的矩形;(x,y)
题目要求:判断点与矩形的距离公式
步骤:
1.提示用户输入点的坐标
2.确定水平距离最小值,垂直距离最小值
3.考虑到输入可能有负数坐标,给x,y取绝对值
4.判断x与水平距离的关系,y与垂直距离的关系
5.输出结果
代码:
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("在矩形外");
}
}
}
Demo28
解题思路:
题中的数据:三角形三点坐标,用户输入坐标
题目要求:判断点与三角形的位置关系
步骤:
1.提示用户输入点的坐标
2.判断点与三角形关系
3.输出结果
代码:
import java.util.Scanner;
public class Demo28{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter two point:");
double x = in.nextDouble();
double y = in.nextDouble();
if(x >= 0 && x <= 200 && y >= 0 && y <= 100){
if(y / (200 - x) <= 0.2){
System.out.println("in trian");
}else{
System.out.println("out trian");
}
}else{
System.out.println("out rect");
}
}
}
Demo29
解题思路:
题中的数据:两个矩形的中心坐标,宽度,高度
题目要求:判断两个矩形的位置关系
步骤:
1.提示用户输入矩形的基本信息
2.若第二个矩形的左边界在第一个矩形左边界的右边,第二个矩形的右边界在第一个矩形右边界的左边,第二个矩形的上边界在第一个矩形上边界的下边,第二个矩形的下边界在第一个矩形下边界的上边,则第二个矩形在第一个矩形的内部
3.若第二个矩形的右边界在第一个矩形左边界的左边,第二个矩形的左边界在第一个矩形右边界的右边,第二个矩形的下边界在第一个矩形上边界的上边,第二个矩形的上边界在第一个矩形下边界的下边,则第二个矩形在第一个矩形的外部
4.否则两矩形重合
5.输出结果
代码:
import java.util.Scanner;
public class Demo29{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
//提示用户输入矩形1的x,y,宽,高
System.out.print("Enter r1's center x-,y-coordinates,width,and height:");
double x = in.nextDouble();
double y = in.nextDouble();
double w = in.nextDouble();
double h = in.nextDouble();
//提示用户输入矩形2的x,y,宽,高
System.out.print("Enter r2's center x-,y-coordinates,width,and height:");
double x0 = in.nextDouble();
double y0 = in.nextDouble();
double w0 = in.nextDouble();
double h0 = in.nextDouble();
/*若第二个矩形的左边界在第一个矩形左边界的右边,
第二个矩形的右边界在第一个矩形右边界的左边,
第二个矩形的上边界在第一个矩形上边界的下边,
第二个矩形的下边界在第一个矩形下边界的上边,
则第二个矩形在第一个矩形的内部*/
if((x0 + w0 < x + w) && (x0 - w0 > x - w) && (y0 + h0 < y + h) && (y0 - h0 > y - h)){
System.out.print("矩形2在矩形1内");
/*若第二个矩形的右边界在第一个矩形左边界的左边,
第二个矩形的左边界在第一个矩形右边界的右边,
第二个矩形的下边界在第一个矩形上边界的上边,
第二个矩形的上边界在第一个矩形下边界的下边,
则第二个矩形在第一个矩形的外部*/
}else if((x0 + w0 < x -w) || (x0 - w0 > x + w) && (y0 + h0 < y - h) || (y0 - h0 < y + h)){
System.out.print("矩形2在矩形1外");
}else{
System.out.print("矩形2和矩形1重合");
}
}
}
Demo30
解题思路:
题中的数据:两个圆的圆心坐标以及半径
题目要求:判断两个圆的位置关系
步骤:
1.提示用户输入两个圆的圆心坐标及半径
2.计算量圆心间距离
3.根据题目提示判断两圆心距离与两个半径间的关系
4.输出结果
代码:
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
解题思路:
题中的数据:用户输入的一个数字
题目要求:判断该数字是否能被5和6,5或6,以及能否被5或6但不能同时被它们整除
步骤:
1.提示用户输入一个数字
2.判断该数字是否能被5和6,5或6,以及能否被5或6但不能同时被它们整除
3.输出结果
代码:
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中的任一数整除");
}
}
}