作业1:
请输入10位同学的java成绩,
1、求平均成绩,最高成绩、最低成绩
2、对10的成绩按照从低到高的顺序排列(选做)
import java.util.Scanner;
/*
请输入10位同学的java成绩,
1、求平均成绩,最高成绩、最低成绩
2、对10的成绩按照从低到高的顺序排列(选做)
*/
public class Demo01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入10位同学的java成绩:");
double[] grades = new double[10];
for (int i = 0; i < grades.length; i++){
grades[i] = sc.nextDouble();
}
for (double i : grades){
System.out.print(i + " ");
}
System.out.println();
System.out.println("平均成绩为:" + averageGrade(grades));
maxAndMin(grades);
System.out.println("成绩按照从低到高的顺序排列:");
sort(grades);
for (double i : grades){
System.out.print(i + " ");
}
}
public static double averageGrade(double[] arr){
double sum = 0;
for (int i = 0; i < arr.length; i++){
sum += arr[i];
}
double average = sum / arr.length;
return average;
}
public static void maxAndMin(double[] arr){
double max = arr[0], min = arr[0];
for (int i = 0; i < arr.length; i++){
if (max < arr[i]){
max = arr[i];
}
if (min > arr[i]){
min = arr[i];
}
}
System.out.println("最高成绩为:" + max + ",最低成绩为:" + min);
}
public static void sort(double[] arr){
for (int i = 0; i < arr.length - 1; i++){
for (int j = i + 1; j > 0; j--){
if (arr[j] < arr[j - 1]){
arr[j] = arr[j] + arr[j - 1];
arr[j - 1] = arr[j] - arr[j - 1];
arr[j] = arr[j] - arr[j - 1];
}
}
}
}
}

作业2:
给定一个数组,判断某个元素是否在该数组中
import java.util.Scanner;
/*
给定一个数组,判断某个元素是否在该数组中
*/
public class Demo02 {
public static void main(String[] args) {
int[] arr = {12, 23, 5, 9, 34, 88, 90, 100};
for (int i : arr){
System.out.print(i + " ");
}
System.out.println();
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数:");
int n = sc.nextInt();
boolean b = isExists(arr, n);
if (b){
System.out.println(n + "在该数组中");
}else {
System.out.println(n + "不在该数组中");
}
}
public static boolean isExists(int[] arr, int n){
for (int i = 0; i < arr.length; i++){
if (arr[i] == n){
return true;
}
}
return false;
}
}

作业3:
给定一个数组,大小为10,输入十个同学的成绩,求这些成绩的总分数
某一天转入2个新同学,请再次求12个同学的平均成绩(尝试着使用扩容的形式做)
import java.util.Arrays;
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入10个同学的成绩:");
double[] grades = new double[10];
for (int i = 0; i < grades.length; i++){
grades[i] = sc.nextDouble();
}
for (double i : grades){
System.out.print(i + " ");
}
System.out.println();
double sum = sumGrade(grades);
System.out.println("这十个同学的总成绩为:" + sum);
System.out.println("转入2个新同学,他们的成绩分别为:");
double s1 = sc.nextDouble();
double s2 = sc.nextDouble();
double[] grades2 = Arrays.copyOf(grades, 12);
grades2[grades.length] = s1;
grades2[grades.length + 1] = s2;
for (double i : grades2){
System.out.print(i + " ");
}
System.out.println();
double avg = averageGrade(grades2);
System.out.println("这12个同学的平均成绩为:" + avg);
}
public static double sumGrade(double[] arr){
double sum = 0;
for (double i : arr){
sum += i;
}
return sum;
}
public static double averageGrade(double[] arr){
double sum = sumGrade(arr);
double average = sum / arr.length;
return average;
}
}

作业4:有一堆硬币,每次只能拿一个或者两个,求最少多少次可以拿完硬币
{10, 8, 5, 3, 27, 99}
public class Demo04 {
public static void main(String[] args) {
int[] arr = {10, 8, 5, 3, 27, 99};
int sum = 0;
for (int i = 0; i < arr.length; i++){
if (arr[i] % 2 == 0){
sum += arr[i] / 2;
}else {
sum += arr[i] / 2 + 1;
}
}
System.out.println("最少要" + sum + "次可以拿完硬币");
}
}

作业5:将数组中的重复数据去重
public class Demo05 {
public static void main(String[] args) {
int[] arr1 = {2, 2, 7, 8, 4, 4, 6, 6, 4, 1, 9, 7};
System.out.println("去重前:");
for (int i : arr1){
System.out.print(i + ", ");
}
System.out.println();
//去重
int[] arr2 = deduplication(arr1);
System.out.println("去重后:");
for (int i : arr2){
System.out.print(i + ", ");
}
}
public static int[] deduplication(int[] arr){
int[] newArr = new int[arr.length];
int index = 0;
for (int i = 0; i < arr.length; i++){
if (!isExists(newArr, arr[i])){
newArr[index++] = arr[i];
}
}
return newArr;
}
public static boolean isExists(int[] arr, int n){
for (int i = 0; i < arr.length; i++){
if (arr[i] == n){
return true;
}
}
return false;
}
}

作业6:如何将10个0-10随机存入数组中
public class Demo06 {
public static void main(String[] args) {
int[] arr = new int[10];
for (int i = 0; i < arr.length; i++){
arr[i] = random(0, 10);
}
for (int i : arr){
System.out.print(i + ", ");
}
}
//产生一个[min, max)的随机数
public static int random(int max, int min){
return (int) (Math.random() * (max - min) + min);
}
}

作业7:存在整数数组nums,将该数组中的所有偶数元素排列到奇数元素前
public class Demo07 {
public static void main(String[] args) {
int[] nums = {1, 8, 6, 5, 3, 4, 2, 12, 19, 22};
sort(nums);
for (int i : nums){
System.out.print(i + " ");
}
}
public static void sort(int[] arr){
for (int i = 0; i< arr.length - 1; i++){
for (int j = i + 1; j > 0; j--){
if (arr[j] % 2 == 0){
arr[j] = arr[j] + arr[j - 1];
arr[j - 1] = arr[j] - arr[j - 1];
arr[j] = arr[j] - arr[j - 1];
}
}
}
}
}

作业8:
执行下列程序的输出结果为 D
public class Test {
public static void main(String[] args) {
String s1 = "HelloWorld";
String s2 = new String("HelloWorld");
if (s1 == s2) {
System.out.println("s1 == s2");
} else {
System.out.println("s1 != s2");
}
if (s1.equals(s2)) {
System.out.println("s1 equals s2");
} else {
System.out.println("s1 not equals s2");
}
}
}
A. s1 == s2
s1 not equals s2
B. s1 == s2
s1 equals s2
C. s1 != s2
s1 not equals s2
D. s1 != s2
s1 equals s2
作业(选做题):
某个人进入如下一个棋盘中,要求从左上角开始走,
最后从右下角出来(要求只能前进,不能后退),
问题:共有多少种走法?
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
public class Demo08 {
public static void main(String[] args) {
System.out.println(moveMethod(5, 8)); //330
}
public static int moveMethod(int m, int n){ //输入行和列
int[][] path = new int[m][n];
for (int i = 0; i < path.length; i++){
path[i][0] = 1;
}
for (int i = 0; i < path[0].length; i++){
path[0][i] = 1;
}
for (int i = 1; i < path.length; i++){
for (int j = 1; j < path[0].length; j++){
path[i][j] = path[i - 1][j] + path[i][j - 1];
}
}
return path[m - 1][n - 1];
}
}

这篇博客通过一系列Java编程作业,涵盖了数组操作的基本技能,包括计算平均值、排序、查找、求和、去重、随机生成等,并涉及了算法应用,如找最大最小值、判断元素存在性以及解决棋盘走法问题。适合初学者巩固Java基础知识和提高算法能力。
375

被折叠的 条评论
为什么被折叠?



