2019.12.06 冒泡排序2.0(二维数组)
/**
* MainView.java
* com.oracle.view
*
* Function: TODO
*
* ver date author
* ──────────────────────────────────
* 2019年12月3日 17671
*
* Copyright (c) 2019, TNT All Rights Reserved.
*/
package com.oracle.view;
/**
* ClassName:MainView
* Function: TODO ADD FUNCTION
* Reason: TODO ADD REASON
*
* @author 17671
* @version
* @since Ver 1.1
* @Date 2019年12月3日 下午6:03:25
*
* @see
*/
import java.util.Scanner;
public class MainView {
public static Scanner scanner=new Scanner(System.in);
public static int[] array=new int[5];
public static String[][] music={{"boyfriend","5440"},
{"on my own","1500"},{"closer","10000"},{"shivers","6664"}};
public static void main(String[] args) {
menu();
}
public static void menu() {
boolean flag=true;
while(flag) {
System.out.println("1.添加数据");
System.out.println("2.按从小到大排序(asc)");
System.out.println("3.按从大到小排序(desc)");
System.out.println("4.查看数组");
System.out.println("5.按从小到大二维数组排序(asc)");
System.out.println("6.按从大到小二维数组排序(desc)");
System.out.println("7.查看二维数组");
System.out.println("8.退出");
System.out.println("请选择:");
int choice=scanner.nextInt();
switch (choice) {
case 1:
addData();
break;
case 2:
ascSort();
break;
case 3:
descSort();
break;
case 4:
showData();
break;
case 5:
tDascSort();
break;
case 6:
tDdescSort();
break;
case 7:
showtd();
break;
case 8:
flag=false;
System.out.println("欢迎下次使用!");
break;
default:
System.out.println("无效命令,请重新输入!");
break;
}
}
}
public static void addData() {
for (int i = 0; i < array.length; i++) {
System.out.println("请输入"+(i+1)+"个数据:");
array[i]=scanner.nextInt();
}
System.out.println("添加数据成功!");
}
public static void showData() {
//增强型for循环 类型 变量:数组
for (int i : array) {
//不换行打印
System.out.print(i+"\t");
}
System.out.println("\n***********");
}
//从小到大排序
public static void ascSort() {
//每轮的比较次数 n-1 n-2 n-3...1
for (int i = array.length-1; i > 0; i--) {
//两个相邻数据进行比较
for (int j = 0; j < i; j++) {
if (array[j]>array[j+1]) {
int temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
showData();
}
}
//从大到小排序
public static void descSort() {
//每轮的比较次数 n-1 n-2 n-3...1
for (int i = array.length-1; i > 0; i--) {
//两个相邻数据进行比较
for (int j = 0; j < i; j++) {
if (array[j]<array[j+1]) {
int temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
showData();
}
}
//二维数组的升序
public static void tDascSort() {
//控制比较次数
for (int i = music.length-1; i >0 ; i--) {
for (int j = 0; j < i; j++) {
//把String类型转为int
int data1=Integer.parseInt(music[j][1]);
int data2=Integer.parseInt(music[j+1][1]);
if (data1>data2) {
//两行数组调换
String temp=music[j][0];
String temp1=music[j][1];
music[j][0]=music[j+1][0];
music[j][1]=music[j+1][1];
music[j+1][0]=temp;
music[j+1][1]=temp1;
}
}
}
}
//二维数组的降序
public static void tDdescSort() {
//控制比较次数
for (int i = music.length-1; i >0 ; i--) {
for (int j = 0; j < i; j++) {
//把String类型转为int
int data1=Integer.parseInt(music[j][1]);
int data2=Integer.parseInt(music[j+1][1]);
if (data1<data2) {
//两行数组调换
String temp=music[j][0];
String temp1=music[j][1];
music[j][0]=music[j+1][0];
music[j][1]=music[j+1][1];
music[j+1][0]=temp;
music[j+1][1]=temp1;
}
}
}
}
public static void showtd() {
System.out.println("*********音乐排行榜*********");
System.out.println("*************************");
System.out.println("歌曲名称\t\t点击数");
for (int i = 0; i < music.length; i++) {
for (int j = 0; j < music[i].length; j++) {
System.out.print(music[i][j]+"\t\t");
}
System.out.println();
}
}
}

浙公网安备 33010602011771号