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");
}
}