import java.util.Scanner;
public class Test3_28 {
public static void main(String[] args) {
// 从控制台获取数据
Scanner input = new Scanner(System.in);
System.out.println("Enter r1's center x-. y- coordinates, width, and height: ");
double x1 = input.nextDouble(), y1 = input.nextDouble();
double w1 = input.nextDouble(), h1 = input.nextDouble();
System.out.println("Enter r2's center x-, y- coordinates, width, and height: ");
double x2 = input.nextDouble(), y2 = input.nextDouble();
double w2 = input.nextDouble(), h2 = input.nextDouble();
// 计算两个中心之间距离的分量
double x0 = Math.abs(x2 - x1);
double y0 = Math.abs(y2 - y1);
// 判断位置
if(((x0 + w2 / 2) <= (w1 / 2)) && ((y0 + h2 / 2) <= (h1 / 2))) {
System.out.println("r2 is inside r1");
}else if(((w1 - w2) / 2) < x0 && x0 < ((w1 + w2) / 2) && ((h1 - h2) / 2) < y0 && y0 < ((h1 + h2) / 2))
System.out.println("r2 overlaps r1");
else
System.out.println("r2 does not overlap r1");
}
}
Demo36
Demo37
Demo38
Demo39
Demo40
import java.util.Scanner;
public class Test{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入一个整数:");
int intInput = input.nextInt();
boolean bool = true;
int a = intInput, b = 0;
while(bool){
for(int m = 2;m <= a; m++){
b = a / m;
if(b == 1){ // 此时只有两个因子
bool = false;
System.out.println(a);
return;
}else{
System.out.print(m+" ");
a = b;
b = 0;
break;
}
}
}
}
}
结果:
Demo41
Demo42
public class Test {
public static void main(String[] args) {
System.out.println("图案1 图案2 图案3 图案4");
int lines = 6;
for(int n = 1;n <= lines;n++){
// ①(图案1数字部分)打印数字从1到n;
for(int a = 1;a <= n;a++)
System.out.printf("%d ", a);
// ②(图案1空格部分)打印2 * (lines - n)个空格;
if(lines != n){
for(int b = lines - n;b > 0;b--)
System.out.print(" ");
}
// ③(分隔)打印3个空格来分隔不同图案;
System.out.print(" ");
// ④(图案2数字部分)从1打印到(lines-n+1);
for(int c = 1;c < (lines - n + 2); c++){
System.out.printf("%d ", c);
}
// ⑤(图案2空格部分)打印2 * (n - 1)个空格;
if(n != 1){
for(int d = 1;d <= n - 1;d++)
System.out.print(" ");
}
// ⑥(分隔)打印3个空格来分隔不同图案;
System.out.print(" ");
// ⑦(图案3空格部分)打印2 * (lines - n)个空格;
if(n != 6){
for(int e = 1;e < (lines - n + 1);e++)
System.out.print(" ");
}
// ⑧(图案3数字部分)从n打印到1;
for(int f = n; f > 0;f--)
System.out.printf("%d ",f);
// ⑨(分隔)打印3个空格来分隔不同图案;
System.out.print(" ");
// ⑩(图案4空格部分)打印2 * (n - 1)个空格;
if(n != 1){
for(int g = 1;g < n;g++)
System.out.print(" ");
}
// ⑪(图案4数字部分)从1打印到(lines - n + 1)
for(int h = 1;h < lines - n + 2;h++)
System.out.printf("%d ", h);
System.out.println();
}
}
}
结果:
Demo43
import java.util.*;
public class Test {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("请输入要打印的行数: ");
int x=sc.nextInt();
for(int j=1;j<=x;j++) {//这个for循环是用来控制行数
for(int z=1;z<=x-j;z++) {
System.out.print(" ");
}
for(int i=1;i<=j+1;i++) {
if(i<=j) {
System.out.printf("%4d",(int)Math.pow(2, i-1));//printf和"%4d"为格式化打印,具体用法请看下文
}
else {
for(int y=j-2;y>=0;y--) {
System.out.printf("%4d",(int)Math.pow(2,y));
}
}
}
System.out.println();//控制一行打完后的换行
}
}
}
结果:
Demo44
Demo45
Demo46
Demo47
Demo48
public class Cheng {
public static void main(String[] args) {
String url = "http://www.163.com?userName=admin&pwd=123456";
System.out.println(userName(url));
System.out.println(urlAddres(url));
}
public static String userName(String url) {
String temp;
temp = url.substring(url.indexOf("userName")+9, url.lastIndexOf("pwd")-1); // 左闭右开
return "用户名为:"+temp;
}
public static String urlAddres(String url) {
String temp;
temp = url.substring(url.indexOf("//")+2, url.indexOf("?")); // 左闭右开
return "域名为:"+temp;
}
}
结果为:
用户名为:admin
域名为:www.163.com
Demo49
import java.util.Random;
import java.util.Scanner;
public class Test01 {
public static void main(String[] args) {
int count=0;
while (count<=3) {
System.out.println("请用户输入:");
Scanner iu = new Scanner(System.in);
int a = iu.nextInt();
//0,石头 1,剪刀 2,布
Random r = new Random();
int b = r.nextInt(3);
if ((a == 0 && b == 1) || (a == 1 && b == 2) || (a == 2 && b == 0)) {
System.out.println("用户赢了");
System.out.println("用户输入的是" + a);
System.out.println("计算机输入的是" + b);
count++;
} else if ((b == 0 && a == 1) || (b == 1 && a == 2) || (b == 2 && a == 0)) {
System.out.println("计算机赢了");
System.out.println("用户输入的是" + a);
System.out.println("计算机输入的是" + b);
count++;
} else {
System.out.println("平局");
}
}
}
}
Demo50
import java.util.*;
public class Test01 {
static List list=new ArrayList();
public static void main(String[] args) {
Scanner iu=new Scanner(System.in);
System.out.print("请输入一个数:");
int num=iu.nextInt();
int count=0;
while (num!=0){
count=num%2;
list.add(count);
num=num/2;
}
System.out.println(list);
for(int i= list.size()-1; i>=0; i--)
{
System.out.print(list.get(i));
}
}
}
Demo51
import java.util.Scanner;
public class Test02 {
public static void main(String[] args) {
Scanner iu=new Scanner(System.in);
System.out.println("请输入一个0-15之间的整数:");
int a=iu.nextInt();
System.out.println("转换为八进制为:"+octal(a));
}
public static String octal(int n) {
if (n == 0)
return "0";
StringBuilder o = new StringBuilder();
while (n > 0) {
o.append(n % 8);
n = n / 8;
}
return o.reverse().toString();
}
}
结果为:
请输入一个0-15之间的整数:
12
转换为八进制为:14
Demo52
Demo53
import java.util.Scanner;
public class Test01 {
public static void main(String[] args) {
Scanner iu=new Scanner(System.in);
System.out.print("请输入三角形三条边:");
double a=iu.nextDouble();
double b=iu.nextDouble();
double c= iu.nextDouble();
System.out.println(sqrt(a,b,c));
}
public static double sqrt(double a,double b,double c){
double A= Math.acos(a*a-b*b-c*c)/(-2*b*c);
double B= Math.acos(b*b-c*c-a*a)/(-2*b*c);
double C= Math.acos(c*c-a*a-c*c)/(-2*b*c);
return A;
}
}
Demo54
Demo55
Demo56
import java.util.Scanner;
public class Test02 {
public static void main(String[] args) {
Scanner iu=new Scanner(System.in);
System.out.println("请输入边长:");
double s=iu.nextDouble();
double area=(5*Math.pow(s,2))/(4*Math.tan(Math.PI/5));
System.out.println("面积为:"+area);
}
}
结果为:
请输入边长:
5
面积为:43.01193501472417
Demo57
public class Test02 {
public static void main(String[] args) {
Scanner iu=new Scanner(System.in);
int a=iu.nextInt();
int b=iu.nextInt();
double area=(a*b)/(4*Math.tan(Math.PI/a));
System.out.println("面积为:"+area);
}
}
结果为:
10
5
面积为:38.47104421469067
Demo58
import java.util.Scanner;
public class Test{
public static void main(String[] args) {
// 获取数据
Scanner input = new Scanner(System.in);
System.out.print("输入一个 ASCII码: ");
int i = input.nextInt();
if (i>=0&&i<=127){
System.out.println((char)i);
}else {
System.out.println("输入错误");
}
}
}
Demo59
import java.util.Scanner;
public class case03 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入0~15中的一个整数:");
int n = scan.nextInt();
char ch = (char)( n>9?('A'+(n-10)):('0'+(n-0)) );
System.out.println(n + "对应的十六进制数是:" + ch);
}
}
结果:
Demo60
import java.util.Scanner;
public class Test02 {
public static void main(String[] args) {
Scanner iu=new Scanner(System.in);
System.out.println("请输入第一个字符串:");
String s=iu.next();
System.out.println("请输入第二个字符串:");
String s2=iu.next();
if (s.contains(s2)){
System.out.println(s2+"是"+s+"的子串");
}else {
System.out.println(s2+"不是"+s+"的子串");
}
}
}
结果为:
请输入第一个字符串:
1234
请输入第二个字符串:
23
23是1234的子串
Demo61
public class Test {
public static void main(String[] args) {
// 获取用户输入
Scanner input = new Scanner(System.in);
System.out.println("请输入: ");
String str = input.next();
// 判断长度是否符合要求(11位)
int length = str.length();
if(length != 11){
System.out.println(str + " is an invalid social security number");
System.exit(1);
}
//将每位分别转为char类型,再强转为int类型(ASCII码)
char c1 = str.charAt(0);int i1 = (int) c1;
char c2 = str.charAt(1);int i2 = (int) c2;
char c3 = str.charAt(2);int i3 = (int) c3;
char c4 = str.charAt(3);int i4 = (int) c4;
char c5 = str.charAt(4);int i5 = (int) c5;
char c6 = str.charAt(5);int i6 = (int) c6;
char c7 = str.charAt(6);int i7 = (int) c7;
char c8 = str.charAt(7);int i8 = (int) c8;
char c9 = str.charAt(8);int i9 = (int) c9;
char c10 = str.charAt(9);int i10 = (int) c10;
char c11 = str.charAt(10);int i11 = (int) c11;
boolean bool4 = false, bool7 = false, bool_47 = false;
if(i4 == 45)
bool4 = true;
if(i7 == 45)
bool7 = true;
if(bool4 && bool7)
bool_47 = true;
// 判断其它int数值是否在'0'~'9'之间(即[45, 54])
boolean bool1 = false, bool2 = false, bool3 = false, bool5 = false, bool6 = false;
boolean bool8 = false, bool9 = false, bool10 = false, bool11 = false, bool_13 = false;
if(