题目
1.编写Java程序,设计MyArray1类来实现以下要求,定义一个长度为5的整数数组,要求用户从键盘输入赋值,计算最大值,最小值,总和及平均值;并将数组与所求出的最大值、最小值、总和以及平均值在屏幕上输出。
-
编写Java程序,设计MyArray2类来实现以下要求:使用二分法查询数组中的元素是否存在。数组元素个数可以由用户确定,数组元素值也可由用户输入;
注意:采用二分法对数组查询的前提条件是要求数组有序; -
编写Java程序,设计MyArray3类来实现以下要求:找出一个3×4矩阵二维数组中的最大最小值,二维数组的元素的值由系统随机数生成函数rand()产生,取值范围为两位数即[10,100)之间。输出最大最小值的行列位置序号。
第一题
1、代码
package array_;
import java.util.Scanner;
public class MyArray1 {
//定义max为最大值,min为最小值,total为总和,ave为平均值
double max;
double min;
double total;
double ave;
double[] A = new double[5];
Scanner scanner = new Scanner(System.in);
void count(){
//输入赋值
for (int i = 0; i < A.length; i++) {
A[i] = scanner.nextDouble();
//每添加一个,添加总和total
total += A[i];
}
//平均值ave
ave = total/A.length;
//最大值max,与最小值min
max=A[0];
min=A[0];
for (int i = 0; i < A.length; i++) {
//最大值
if (A[i]>max){
max=A[i];
}
//最小值
if (A[i]<min){
min=A[i];
}
}
}
void print(){
System.out.println("最大值:"+max+"最小值:"+min +
"总和:"+total+"平均值:"+ave);
}
public static void main(String[] args) {
MyArray1 myArray1 = new MyArray1();
myArray1.count();
myArray1.print();
}
}
2、运行结果
第二题
1、代码
package array_;
public class MyArray2 {
//2. 编写Java程序,设计MyArray2类来实现以下要求:使用二分法查询数组中的元素是否存在。
// 数组元素个数可以由用户确定,数组元素值也可由用户输入;
//注意:采用二分法对数组查询的前提条件是要求数组有序;
//定义数组,或者scanner输入
double[] array = {30,90,50,40,80};
//排序方法--冒泡排序,两层for循环
public void sort(double[] array){
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length-i-1; j++) {
if(array[j]>array[j+1]){
double temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
}
//二分法查询方法,寻找的是数组的位置
public int search(double[] array, double target) {
//如果数组没有大小,位置为-1
int loc = -1;
if(array==null ) return -1;
//使用排序方法
sort(array);
//左边,右边,中间值的位置
int left=0,right=array.length - 1,M = left+(right-left)/2;
// 但计算机只能用相减小于一个很小值判断double是否相等
while(left <=right ) {
//找到
if(Math.abs(array[M] - target) < 1.0E-5) {
loc = M;break;
}
//更新右边界
if(array[M] > target) {
right = M - 1;
}
//更新左边界
if(array[M] < target) {
left = M + 1;
}
//更新中间值
M = left+(right-left)/2;
}
return loc;
}
void print(){
for (int i = 0; i < array.length; i++) {
System.out.print(" "+array[i]);
}
}
public static void main(String[] args) {
double target = 80;
MyArray2 myArray2 = new MyArray2();
System.out.print("\n"+"原数组为:");
myArray2.print();
int loc = myArray2.search(myArray2.array, target)+1;
System.out.print("\n"+"新数组为:");
myArray2.print();
System.out.print("\n"+"查找到"+target+"在array的位置为"+loc);
}
}
2、运行结果
第三题
1、代码
package array_;
public class MyArray3 {
public static void main(String[] args) {
int[][] A = new int[3][4];
//循环添加
for(int row = 0;row < A.length;row++) {
for(int col = 0; col < A[row].length; col++) {
A[row][col] =(int)(Math.random()*90)+10;
}
}
//循环输出
for(int row = 0;row < A.length;row++) {
for(int col = 0; col < A[row].length; col++) {
System.out.print(A[row][col]+" ");
}
System.out.println();
}
// 找最大,最小值
int max_c =0,max_r=0;
int min_c=0,min_r=0;
for(int row = 0;row < A.length;row++) {
for(int col = 0; col < A[row].length; col++) {
//最大值
if(A[max_r][max_c]<A[row][col]) {
max_r = row;
max_c = col;
}
//最小值
if(A[min_r][min_c]>A[row][col]) {
min_r = row;
min_c = col;
}
}
}
System.out.printf("\n最大值为:A[%d][%d]:%d 最小值为:A[%d][%d]:%d",
max_r,max_c,A[max_r][max_c],
min_r,min_c,A[min_r][min_c]);
}
}