1:请输入10位同学的java成绩,
(1)求平均成绩,最高成绩、最低成绩
(2)对10的成绩按照从低到高的顺序排列
package com.lele.day7_18;
import java.util.Scanner;
public class Test01 {
public static void main(String[] args) {
/*
* 作业1: 请输入10位同学的java成绩,
* 1、求平均成绩,最高成绩、最低成绩
* 2、对10的成绩按照从低到高的顺序排列(选做)
*/
System.out.println("请输入10位同学的成绩:");
Scanner scanner = new Scanner(System.in);
int arr [] = new int [10];
for (int i = 0; i < 10; i++) {
arr[i] = scanner.nextInt();
}
scanner.close();
ShuChu(arr);
System.out.println("未排序之前的顺序为:" );
print(arr);
MyShort(arr);
System.out.println();
System.out.println("排序后的顺序为:");
print(arr);
}
public static void ShuChu( int arr[]) {
int max = arr[0],min = arr[0],sum = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}else {
max = arr[i];
}
sum += arr[i];
}
System.out.println("平均成绩为:" + (sum / 10.0));
System.out.println("最高分为:" + max);
System.out.println("最低分为:" + min);
}
public static void MyShort(int arr []) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j > 0; j --) {
if (arr[j] < arr [j - 1]) {
int temp = arr[j];
arr[j] = arr[j -1];
arr[j -1 ] = temp;
}
}
}
}
public static void print(int arr []) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
/*
请输入10位同学的成绩:
1 2 3 6 5 4 7 8 9 5
平均成绩为:4.9
最高分为:5
最低分为:1
未排序之前的顺序为:
1 2 3 6 5 4 7 8 9 5
排序后的顺序为:
1 2 3 4 5 5 6 7 8 9
*/
2:给定一个数组,判断某个元素是否在该数组中
package com.lele.day7_18;
import java.util.Scanner;
public class Test02 {
public static void main(String[] args) {
// 给定一个数组,判断某个元素是否在该数组中
System.out.println("请输入你想判断的数字:");
Scanner scanner = new Scanner(System.in);
int in = scanner.nextInt();
int arr [] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
if (isIn(arr, in)) {
print(arr);
System.out.println(in + "在数组中!");
}else {
print(arr);
System.out.println( in + "不在数组中!");
}
}
public static boolean isIn( int arr[], int key) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key) {
return true;
}
}
return false;
}
public static void print(int arr []) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
/*
请输入你想判断的数字:
10
1 2 3 4 5 6 7 8 9
10不在数组中!
*/
3:
给定一个数组,大小为10,输入十个同学的成绩,求这些成绩的总分数
某一天转入2个新同学,请再次求12个同学的平均成绩(尝试着使用扩容的形式做)
package com.lele.day7_18;
import java.util.Scanner;
public class Test03 {
public static void main(String[] args) {
/*
* 作业3:
* 给定一个数组,大小为10,输入十个同学的成绩,求这些成绩的总分数
* 某一天转入2个新同学,请再次求12个同学的平均成绩(尝试着使用扩容的形式做)
*/
int[] arr = new int[]{1,2,3,4,5,6,7,8,9,5};
System.out.println("10人的总成绩是:"+Mysum(arr));
arr = bigarr(arr,12);
arr[10] = 12;
arr[11] = 14;
print(arr);
System.out.println();
System.out.println("12人的平均成绩是:"+(Mysum(arr) / arr.length));
}
// 求和
public static int Mysum(int[] arr) {
int sum = 0;
for (int i : arr) {
sum += i;
}
return sum;
}
public static int[] bigarr(int[] arr, int len) {
int[] newArr = new int[len];
for (int i = 0; i < arr.length; i++) {
newArr[i] = arr[i];
}
return newArr;
}
public static void print(int arr []) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
/*
10人的总成绩是:50
1 2 3 4 5 6 7 8 9 5 12 14
12人的平均成绩是:6
*/
4.将数组中的元素去重;
package com.lele.day7_18;
public class Test04 {
public static void main(String[] args) {
/*
将数组中的重复数据去重
*/
int arr [] = {1,1,1,1,2,2,2,2,2,3,3};
QuChong(arr);
}
public static void QuChong(int arr []) {
int newarr [] = new int [arr.length];
int count = -1;
for(int x=0;x<arr.length;x++) {
boolean b = true;
for (int y=x+1;y<arr.length;y++){
if (arr[x]==arr[y]){
b=false;
break;
}
}
if (b) {
count ++ ;
newarr[count] = arr[x];
}
}
print(newarr);
}
public static void print(int arr []) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
/*
1 2 3 0 0 0 0 0 0 0 0
*/
5.如何将10个0-10随机存入数组中
package com.lele.day7_18;
public class Test05 {
public static void main(String[] args) {
// 如何将10个0-10随机存入数组中
int arr [] = new int [10];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int)(Math.random() * 10 + 1);
}
print(arr);
}
public static void print(int arr []) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
/*
2 1 2 2 3 1 6 6 7 3
*/
6.存在整数数组nums,将该数组中的所有偶数元素排列到奇数元素前
package com.lele.day7_18;
public class Test06 {
public static void main(String[] args) {
// 存在整数数组nums,将该数组中的所有偶数元素排列到奇数元素前
int nums [] = {1,5,9,2,4,6,8,3,7};
System.out.println("未排序之前:");
print(nums);
Pai(nums);
}
public static void Pai(int arr[]) {
int[] sortedArray = new int[arr.length];
int OIndex = 0, JIndex = arr.length - 1;
for (int i = 0; i < arr.length; i++) {
if ((arr[i] % 2 ) == 0) {
sortedArray[OIndex++] = arr[i];
}
else {
sortedArray[JIndex--] = arr[i];
}
}
System.out.println();
System.out.println("排序之后为:");
print(sortedArray);
}
public static void print(int arr []) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
/*
未排序之前:
1 5 9 2 4 6 8 3 7
排序之后为:
2 4 6 8 7 3 9 5 1
*/
7.
执行下列程序的输出结果为
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
使用==号比较的是两个对象的地址值,重写equals方法则比较的是两个对象的内容!!!!
8.有一堆硬币,每次只能拿一个或者两个,求最少多少次可以拿完硬币
{10, 8, 5, 3, 27, 99}
package com.lele.day7_18;
public class Test07 {
public static void main(String[] args) {
// 有一堆硬币,每次只能拿一个或者两个,求最少多少次可以拿完硬币
//{10, 8, 5, 3, 27, 99}
int arr [] = {10, 8, 5, 3, 27, 99};
System.out.println("至少需要取:"+GetS(arr) + "次");
}
public static int GetS(int arr[]) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 == 0) {
sum += (arr[i] / 2);
}else {
sum += (arr[i] + 1) / 2;
}
}
return sum;
}
}
//至少需要取:78次