Java入门

[toc]

Helloworld

1
2
3
4
5
public class Helloworld {
public static void main(String[] args) {
System.out.println("你好世界!!!");
}
}

Scanner

1
2
3
4
5
6
7
8
9
10
11
12
import java.util.Scanner;

public class ScannerDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数字");
int number01 = sc.nextInt();
System.out.println("请输入第二个数字");
int number02 = sc.nextInt();
System.out.println(number01 + number02);
}
}

键盘录入三位数,取其个十百三位

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.Scanner;

public class TextDemo1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个三位数");
int number01 = sc.nextInt();
int ge = number01 % 10;
int shi = number01 / 10 % 10;
int bai = number01 / 100 % 10;
System.out.println(ge);
System.out.println(shi);
System.out.println(bai);

}
}

三个和尚

1
2
3
4
5
6
7
8
9
10
11
12
public class Tests {
public static void main(String[] args) {
int height1 = 150;
int height2 = 210;
int height3 = 165;

int temp = height1 > height2 ? height1 : height2;
int max = temp > height3 ? temp : height3;
System.out.println(max);
}
}

求回文数

image-20230811223733345

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Demo01 {
public static void main(String[] args) {
int x = 121;
int temp = x; //备份x
int num = 0;
while (x != 0){ //从右往左取每一位数字
int ge = x % 10;
x = x / 10; //修改x记录值
num = num * 10 + ge;//拼接
}
System.out.println(num == temp);

}

}

求商和余数

image-20230811223828826

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Demo01 {
public static void main(String[] args) {
//定义被除数
int dividend = 10;
//定义除数
int divisor = 3;
//定义一个变量用来统计相减了多少次
int count = 0;
//循环while,在循环中不断用 被除数 - 被数,只要 被除数 > 除数,则一直循环,减剩下的则是余数
while (dividend >= divisor){
dividend = dividend - divisor;
count ++;
}
System.out.println("余数为:" + dividend);
System.out.println("商为:" + count);

}

}

逢七过

image-20230812114558912

1
2
3
4
5
6
7
8
9
10
11
public class Demo01 {
public static void main(String[] args) {
for(int i = 1;i <= 100;i ++){
if(i % 10 == 7 || i / 10 % 10 == 7 || i % 7 == 0){
System.out.println("过");
continue;
}
System.out.println(i);
}
}
}

求平方根

image-20230812114624491

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Scanner;

public class Demo01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入一个整数:");
int number = sc.nextInt();
for (int i = 1;i <= number;i ++){
if(i * i == number){
System.out.println(i + "就是" + number + "的平方根");
break;
}else if(i * i > number){
System.out.println((i - 1) + "就是" + number + "平方根的整数部分");
break;
}

}

}

}

Random

1
2
3
4
5
6
7
8
9
10
import org.w3c.dom.ranges.Range;
import java.util.Random;

public class Demo01 {
public static void main(String[] args) {
Random r = new Random();
int number = r.nextInt(100) + 1; // 1~100
System.out.println(number);
}
}

猜数字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import org.w3c.dom.ranges.Range;
import java.util.Random;
import java.util.Scanner;

public class Demo01 {
public static void main(String[] args) {
Random r = new Random();
int number = r.nextInt(100) + 1; // 1~100
Scanner sc = new Scanner(System.in);
while (true){
System.out.print("输入你想要输入的数字:");
int guessNumber = sc.nextInt();
if (guessNumber > number){
System.out.println("大了");
}else if(guessNumber < number){
System.out.println("小了");
}else{
System.out.println("猜中了");
break;
}
}
}
}