序言
- 这是笔者无聊时候写的一些java小练习,不排除会持续更新,谁知道呢哈哈哈
小练习
1. 九九乘法表
public class MultiplicationTable {
public static void main(String[] args) {
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(i + "*" + j + "=" + (i * j) + "\t");
}
System.out.println();
}
}
}
2. 求数组中的最大值最小值
public class ArrayMaximum {
public static void main(String[] args) {
int[] arr = {2, 6, 1, 77, 52, 25, 7};
int max = arr[0];
int min = arr[0];
for (int i : arr) {
if (i > max) {
max = i;
}
if (i < min) {
min = i;
}
}
System.out.println("数组的的最大值:" + max);
System.out.println("数组的的最小值:" + min);
}
}
3. 给数组排序,数组默认长度为10,一次录入十个元素,然后排序
public class ArraySort {
private static final int ARRAY_LENGTH = 10;
private static final int[] arr = new int[ARRAY_LENGTH];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
try {
System.out.println("请输入第" + (i + 1) + "位元素:");
String next = sc.next();
int item = Integer.parseInt(next);
arr[i] = item;
} catch (NumberFormatException e) {
System.out.println("录入数据转化有误,非数字类型!");
}
}
System.out.println("录入数组数据完成");
int temp;
for (int j = 0; j < arr.length - 1; j++) {
for (int k = 0; k < arr.length - 1; k++) {
if (arr[k] > arr[k + 1]) {
temp = arr[k];
arr[k] = arr[k + 1];
arr[k + 1] = temp;
}
}
}
System.out.println("排序后的数组:" + Arrays.toString(arr));
}
}
4. 猜数字
public class GuessNumber {
public static void main(String[] args) {
guessNumber();
}
public static void guessNumber() {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int randomNum = random.nextInt(99) + 11;
int count = 0;
while (true) {
System.out.println("请输入你猜的数字(11~99):");
int guessNumber = scanner.nextInt();
if (guessNumber <= 99 && guessNumber >= 11) {
if (guessNumber < randomNum) {
System.out.println("你猜的数字小了!");
count++;
} else if (guessNumber > randomNum) {
System.out.println("你猜的数字大了!");
count++;
} else {
System.out.println("恭喜你猜对了!");
System.out.println("你一共猜了:" + count + "次");
break;
}
} else {
System.out.println("数字不在范围内!");
}
}
}
}
5. 输入一个整数,判断奇偶
public class JudgeParity {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数字:");
String next = sc.next();
try {
int number = Integer.parseInt(next);
if (number % 2 == 0) {
System.out.println("整数" + number + "是偶数");
} else {
System.out.println("整数" + number + "是奇数");
}
} catch (NumberFormatException e) {
System.out.println("读取输入不是整数!!!");
} catch (Exception e) {
System.out.println("系统异常!");
e.printStackTrace();
} finally {
sc.close();
}
}
}
6. 输入年份、月份,判断当月天数
public class ObtainMonthlyDays {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份:");
String year = sc.next();
System.out.println("请输入月份");
String month = sc.next();
try {
int yearNum = Integer.parseInt(year);
int monthNum = Integer.parseInt(month);
switch (monthNum) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println(yearNum + "年" + monthNum + "月为31天");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println(yearNum + "年" + monthNum + "月为30天");
break;
case 2:
if (yearNum % 4 == 0 && yearNum % 100 != 0 || yearNum % 400 == 0) {
System.out.println(yearNum + "年" + monthNum + "月为29天");
} else {
System.out.println(yearNum + "年" + monthNum + "月为28天");
}
break;
default:
System.out.println("月份数字不在1-12内,请检查月份");
break;
}
} catch (NumberFormatException e) {
System.out.println("输入年份或月份有误!!!");
} catch (Exception e) {
System.out.println("系统异常!");
e.printStackTrace();
} finally {
sc.close();
}
}
}
7. 输出1-100之间所有的整数,每五个换行
public class OutNumber {
public static void main(String[] args) {
int count = 0;
for (int i = 1; i <= 100; i++) {
count++;
System.out.print(i + "\t");
if (count % 5 == 0) {
System.out.println();
}
}
}
}
8. 输入密码验证,密码错误提示错误,正确则提示进入系统
public class PasswordVerification {
private static final String SYSTEM_PASSWORD = "123456";
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入系统密码:");
while (true){
String password = sc.next();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (SYSTEM_PASSWORD.equals(password)){
System.out.println("密码正确,欢迎进入系统");
sc.close();
System.exit(0);
} else {
System.out.println("密码错误哈!再试再试!");
System.out.println("请再次输入密码:");
}
}
}
}