Java基础-方法练习题
习题一 用方法输出数组中的最值和平均值
public class ArrayOperate {
public static void main(String[] args) {
int[] arr = {8, 3, 6, 9, 14};
arrayOperate(arr);
}
private static void arrayOperate(int[] arr) {
int max = arr[0];
int min = arr[0];
int sum = 0;
for (int i = 0; i <= arr.length - 1; i++) {
max = Math.max(max, arr[i]);
sum += arr[i];
}
for (int i = 1; i < arr.length; i++) {
min = Math.min(min, arr[i]);
}
System.out.println("数组中的最大值为:" + max);
System.out.println("数组中的最小值为:" + min);
System.out.println("数组内所有元素的平均值为:" + (sum / arr.length));
}
}
练习二 获取数组平均值并统计数组中高于平均数的值个数
public class ArrayGetAvg {
public static void main(String[] args) {
Random rd = new Random();
int[] arr = new int[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = rd.nextInt(100);
}
int avg = getAvg(arr);
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > avg) {
count++;
}
}
System.out.printf("高于平均分%d的个数有%d个",avg, count);
}
private static int getAvg(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum / arr.length;
}
}
习题三 获取水仙花数个数并打印所有水仙花数
public class GetDaffodilNum {
public static void main(String[] args) {
int count = getCount();
int[] arr = getArr(count);
System.out.println("三位水仙花数有:");
printArray(arr);
}
private static boolean isDaffodil(int num) {
int singleDigit = num % 10;
int tensDigit = num / 10 % 10;
int hundredDigit = num / 100;
return (singleDigit * singleDigit * singleDigit + tensDigit * tensDigit * tensDigit + hundredDigit * hundredDigit * hundredDigit == num);
}
private static int getCount() {
int count = 0;
for (int i = 100; i < 1000; i++) {
if (isDaffodil(i)) {
count++;
}
}
return count;
}
private static int[] getArr(int count) {
int[] arr = new int[count];
for (int i = 100; i < 1000; i++) {
if (isDaffodil(i)) {
arr[--count] = i;
}
}
return arr;
}
private static void printArray(int[] arr) {
for (int value : arr) {
System.out.println(value);
}
}
}
习题四 获取数组中指定元素的索引值
public class ArrayGetIndex {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要查询的数字:");
int i = sc.nextInt();
int[] arr = {11, 22, 33, 22, 11};
int a = getIndex(arr,i);
if (a != -1) {
System.out.println(i + "最后出现位置的索引是:" + a);
} else {
System.out.println("你要查找的数字不在数组内");
}
}
private static int getIndex(int[] arr,int i) {
int index = -1;
for (int j = 0; j < arr.length; j++) {
if (arr[j] == i) {
index = j;
}
}
return index;
}
}
习题五 九九乘法表
public class MultiTable {
public static void main(String[] args) {
multiTable();
}
private static void multiTable() {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.printf("%d * %d = %d\t", j, i, (i * j));
}
System.out.println();
}
}
}
习题六 升级版猜数游戏
public class LeveledNumGame {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
leveledGame();
}
private static void leveledGame() {
System.out.println("请选择游戏等级:1.简单(1~10) 2.中等 (1~50) 3.困难 (1~100):");
int i = sc.nextInt();
switch (i) {
case 1:
numberGame(10);
break;
case 2:
numberGame(50);
break;
case 3:
numberGame(100);
break;
default:
System.out.println("输入有误");
break;
}
}
private static void numberGame(int num) {
Random rd = new Random();
int seed;
seed = rd.nextInt(num) + 1;
while (true) {
System.out.printf("请输入你要猜的数字(1~%d):\n",num);
int guess = sc.nextInt();
if (guess >= 1 && guess <= num) {
if (guess > seed) {
System.out.printf("您猜的数字%d大了,再猜一次吧。\n", guess);
} else if (guess < seed) {
System.out.printf("您猜的数字%d小了,再猜一次吧。\n", guess);
} else {
System.out.println("恭喜你猜对了!");
break;
}
} else {
System.out.printf("输入有误,请输入1~%d之间的数字\n", num);
}
}
}
}
习题七 获取班级成绩中大于90分的人数和具体分数
public class GetScore {
public static void main(String[] args) {
int[] arr = {59, 88, 99, 89, 96, 85, 100, 66};
int[] score = getScore(arr);
System.out.printf("大于90分的同学共有%d个,分别是: ", score.length);
for (int i = 0; i < score.length; i++) {
System.out.print(score[i] + " ");
}
}
private static int[] getScore(int[] arr) {
int count = 0;
for (int item : arr) {
if (item >= 90) {
count++;
}
}
int[] score = new int[count];
for (int value : arr) {
if (value >= 90) {
score[--count] = value;
}
}
return score;
}
}