【java】第3章 选择 测试题(作业部分)

文章目录

3.15

在这里插入图片描述

//3.15 (3-8改)
import java.util.Scanner;
public class Demo {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in); 
		System.out.print("Enter your lottery pick: ");
		int guess = input.nextInt();
//		int lottery = (int)(Math.random() * 900 + 100);
		int lottery = 456;
		int lo = lottery, gu = guess;
		System.out.println("The lottery number is " + lottery);
		int l[] = new int[4], g[] = new int[4]; 
		l[1]= lo / 100; g[1] = gu / 100;
		lo %= 100; gu %= 100;
		l[2] = lo / 10; g[2] = gu / 10;
		l[3] = lo % 10; g[3] = gu % 10;
		boolean flag = false;
		for(int i = 1; i <= 3; i++){
			for(int j = 1; j <= 3; j++){
				if(l[i] == g[j]){
					flag = true;
					break;
				}
			}
			if(flag)
				break;
		}
		if(guess == lottery)
			System.out.println("Exact match: you win $10,000");
		else if(l[1] == g[1] && l[2] == g[3] && l[3] == g[2] ||
			    l[1] == g[2] && l[2] == g[1] && l[3] == g[3] ||
			    l[1] == g[2] && l[2] == g[3] && l[3] == g[1] ||
			    l[1] == g[3] && l[1] == g[1] && l[3] == g[2] ||	
			    l[1] == g[3] && l[2] == g[2] && l[3] == g[1])
			System.out.println("Match all digit: you win $3,000");
		else if(flag)
			System.out.println("Match one digit: you win $1,000");
		else
			System.out.println("Sorry, no match");			
	}

}
/*
123
132
213
231
312
321
*/

3.22

在这里插入图片描述

//3.22
import java.util.Scanner;
public class Demo {

	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();
		if(Math.sqrt((x * x + y * y)) <= 10)
			System.out.print("Point (" + x + ", " + y + ") is in the circle" );
		else
			System.out.print("Point (" + x + ", " + y + ") is not in the circle" );
	}

}

3.27

在这里插入图片描述

//3.27
import java.util.Scanner;
public class Demo {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter a point's x- and y- coordinates: ");
		double x = input.nextDouble(), y = input.nextDouble();
		if(100 - 0.5 * x - y >= 0 && x >= 0 && y >= 0)
			System.out.print("The point is in the triangle");
		else
			System.out.print("The point is not in the triangle");
		
	}

}

/*
y = 100 - 0.5 * x
y = 0
x = 0
*/

3.28

在这里插入图片描述
在这里插入图片描述

//3.28
import java.util.Scanner;
public class Demo {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter r1's center x-, y-coordinates, width, and height: ");
		double x1 = input.nextDouble(), y1 = input.nextDouble(), 
			   w1 = input.nextDouble(), h1 = input.nextDouble(),
			   lx1 = x1 - w1 / 2, ly1 = y1 + h1 / 2,
		       rx1 = x1 + w1 / 2, ry1 = y1 - h1 / 2;
		System.out.print("Enter r2's center x-, y-coordinates, width, and height: ");
		double x2 = input.nextDouble(), y2 = input.nextDouble(), 
			   w2 = input.nextDouble(), h2 = input.nextDouble(),
			   lx2 = x2 - w2 / 2, ly2 = y2 + h2 / 2,
			   rx2 = x2 + w2 / 2, ry2 = y2 - h2 / 2;
		if(lx1 <= lx2 && ly1 >= ly2 && rx1 >= rx2 && ry1 <= ry2)
			System.out.println("r2 is inside r1");
		else if(lx1 <= rx2 && ly1 >= ry2 && rx1 >= lx2 && ry1 <= ly2)
			System.out.println("r2 overlaps r1");
		else
			System.out.println("r2 does not overlap r1");
	}

}

3.29

在这里插入图片描述

//3.29
import java.util.Scanner;
public class Demo {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter circle1's center x-, y-coordinates, and tadius: ");
		double x1 = input.nextDouble(), y1 = input.nextDouble(), r1 = input.nextDouble();
		System.out.print("Enter circle1's center x-, y-coordinates, and tadius: ");
		double x2 = input.nextDouble(), y2 = input.nextDouble(), r2 = input.nextDouble();
		double squareOfDistance = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
		if(squareOfDistance <= (r1 - r2) * (r1 - r2))
			System.out.println("circle2 is inside circle1");
		else if(squareOfDistance <= (r1 + r2) * (r1 + r2))
			System.out.println("circle2 overlaps circle1");
		else
			System.out.println("circle2 does not overlap circle1");
		
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_碗碗儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值