Java升序降序
要求:将12名选手的成绩按照降序排列输出将12名选手的成绩按照升序排列输出(但不能使用数组的排列方法sort();)源代码:package**;importjava.ut...
要求:将12名选手的成绩按照降序排列输出
将12名选手的成绩按照升序排列输出(但不能使用数组的排列方法sort();)
源代码:
package **;
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
int[] score = new int[4];
int classNum = 3;
double sum = 0.0;
double[] avgrage = new double[classNum];
Scanner input = new Scanner(System.in);
for (int i = 0; i < classNum; i++)
{
sum = 0.0;
System.out.println("请输入第" + (i + 1) + "个班级成绩:");
for (int j = 0; j < score.length; j++)
{
System.out.print("第" + (j + 1) + "个学员的成绩:");
score[j] = input.nextInt();
sum = sum + score[j];
}
avgrage[i] = sum / score.length;
System.out.println("第" + (i + 1) + "个班级参赛学员的平均分是:" + avgrage[i]
+ "\n");
}
}
} 展开
将12名选手的成绩按照升序排列输出(但不能使用数组的排列方法sort();)
源代码:
package **;
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
int[] score = new int[4];
int classNum = 3;
double sum = 0.0;
double[] avgrage = new double[classNum];
Scanner input = new Scanner(System.in);
for (int i = 0; i < classNum; i++)
{
sum = 0.0;
System.out.println("请输入第" + (i + 1) + "个班级成绩:");
for (int j = 0; j < score.length; j++)
{
System.out.print("第" + (j + 1) + "个学员的成绩:");
score[j] = input.nextInt();
sum = sum + score[j];
}
avgrage[i] = sum / score.length;
System.out.println("第" + (i + 1) + "个班级参赛学员的平均分是:" + avgrage[i]
+ "\n");
}
}
} 展开
2个回答
2015-12-01
展开全部
import java.util.*;
public class Test {
public static void main(String[] args) {
int stuNum = 4;
int classNum = 3;
double[] score = new double[stuNum];
double sum = 0;
double[] avgrage = new double[classNum];
double[] arr = new double[classNum * stuNum];
Scanner input = new Scanner(System. in );
for (int i = 0; i < classNum; i++) {
sum = 0;
System.out.println("请输入第" + (i + 1) + "个班级成绩:");
for (int j = 0; j < score.length; j++) {
System.out.print("第" + (j + 1) + "个学员的成绩:");
double s = input.nextDouble();
score[j] = s;
sum += s;
arr[i * stuNum + j] = s;
}
avgrage[i] = sum / score.length;
System.out.println("第" + (i + 1) + "个班级参赛学员的平均分是:" + avgrage[i] + "\n");
}
input.close();
System.out.println("原序: " + Arrays.toString(arr));
//升序
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
double tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
System.out.println("升序: " + Arrays.toString(arr));
//降序
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] < arr[j]) {
double tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
System.out.println("降序: " + Arrays.toString(arr));
}
}
展开全部
package sum;
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
int[] score = new int[4];
int classNum = 3;
double sum = 0.0;
double[] avgrage = new double[classNum];
int[] cscores = new int[12];
Scanner input = new Scanner(System.in);
int index = 0;
for (int i = 0; i < classNum; i++)
{
sum = 0.0;
System.out.println("请输入第" + (i + 1) + "个班级成绩:");
for (int j = 0; j < score.length; j++)
{
System.out.print("第" + (j + 1) + "个学员的成绩:");
score[j] = input.nextInt();
cscores[index] = score[j];
index++;
sum = sum + score[j];
}
avgrage[i] = sum / score.length;
System.out.println("第" + (i + 1) + "个班级参赛学员的平均分是:" + avgrage[i]
+ "\n");
}
for(int i = 0; i < cscores.length - 1; i++){
for(int j = i; j <cscores.length; j++){
if(cscores[i] < cscores[j]){
int temp = cscores[i];
cscores[i] = cscores[j];
cscores[j] = temp;
}
}
}
System.out.println("成绩降序输出的结果是");
for(int i = 0; i < cscores.length; i++){
System.out.print(cscores[i] + "\t");
}
for(int i = 0; i < cscores.length - 1; i++){
for(int j = i; j <cscores.length; j++){
if(cscores[i] > cscores[j]){
int temp = cscores[i];
cscores[i] = cscores[j];
cscores[j] = temp;
}
}
}
System.out.println();
System.out.println("成绩降序输出的结果是");
for(int i = 0; i < cscores.length; i++){
System.out.print(cscores[i] + "\t");
}
}
}
请采纳我也是写了一会的
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
int[] score = new int[4];
int classNum = 3;
double sum = 0.0;
double[] avgrage = new double[classNum];
int[] cscores = new int[12];
Scanner input = new Scanner(System.in);
int index = 0;
for (int i = 0; i < classNum; i++)
{
sum = 0.0;
System.out.println("请输入第" + (i + 1) + "个班级成绩:");
for (int j = 0; j < score.length; j++)
{
System.out.print("第" + (j + 1) + "个学员的成绩:");
score[j] = input.nextInt();
cscores[index] = score[j];
index++;
sum = sum + score[j];
}
avgrage[i] = sum / score.length;
System.out.println("第" + (i + 1) + "个班级参赛学员的平均分是:" + avgrage[i]
+ "\n");
}
for(int i = 0; i < cscores.length - 1; i++){
for(int j = i; j <cscores.length; j++){
if(cscores[i] < cscores[j]){
int temp = cscores[i];
cscores[i] = cscores[j];
cscores[j] = temp;
}
}
}
System.out.println("成绩降序输出的结果是");
for(int i = 0; i < cscores.length; i++){
System.out.print(cscores[i] + "\t");
}
for(int i = 0; i < cscores.length - 1; i++){
for(int j = i; j <cscores.length; j++){
if(cscores[i] > cscores[j]){
int temp = cscores[i];
cscores[i] = cscores[j];
cscores[j] = temp;
}
}
}
System.out.println();
System.out.println("成绩降序输出的结果是");
for(int i = 0; i < cscores.length; i++){
System.out.print(cscores[i] + "\t");
}
}
}
请采纳我也是写了一会的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询