java编程:从键盘输入3个数,求最大值,求最小值,求从大到小进行排列!!在线等!
3个回答
展开全部
import java.util.Scanner;
/**
* 从键盘输入n个整数,求着n个数的最大值
* @author young
*
*/
public class Exam {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("输入一个整数n确定数组大小:");
int n = input.nextInt();
int[] score = new int[n];
for (int i = 0; i < score.length; i++) {
System.out.println("请输入第" + (i + 1) + "个数:");
score[i] = input.nextInt();
}
System.out.println("\n冒泡排序(从大到小): ");
for (int k = 0; k < score.length - 1; k++) {
for (int j = k + 1; j < score.length; j++) { // 升序把<改成>
if (score[k] < score[j]) {
int temp = score[k];
score[k] = score[j];
score[j] = temp;
}
}
}
for (int k = 0; k < score.length; k++) {
System.out.print(score[k] + " ");
}
System.out.println("\n最大数: " + score[0]);
System.out.println("\n最小数: " + score[2]);
}
}
展开全部
完整代码如下:
import java.util.Scanner;
public class Max {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("输入个数:");
int[] arr = new int[in.nextInt()];
for (int i = 0; i < arr.length; i++) {
System.out.print("输入第(" + (i + 1) + ")个数:");
arr[i] = in.nextInt();
}
int max = Integer.MIN_VALUE;
for (int i : arr) {
max = max > i ? max : i;
}
System.out.println("最大数是:" + max);
Max m = new Max();
m.bubbleSort(arr);
System.out.print("从大到小依次是:");
m.printArray(arr);
}
public void bubbleSort(int[] array) {
int temp;
for(int i=0;i<array.length;i++){//趟数
for(int j=0;j<array.length-i-1;j++){//比较次数
if(array[j]<array[j+1]){
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
}
public void printArray(int[] array){
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]+" ");
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package Main;
import java.util.Arrays;
import java.util.Scanner;
public class test2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a[]= new int[3];
a[0] = sc.nextInt();
a[1] = sc.nextInt();
a[2] = sc.nextInt();
Arrays.sort(a);
System.out.println("min "+a[0]);
System.out.println("max "+a[2]);
for (int i = (a.length-1); i >= 0; i--) {
System.out.println(a[i]);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询