十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
你的数组声明跟初始化没有正确
创新互联主打移动网站、网站设计制作、做网站、网站改版、网络推广、网站维护、主机域名、等互联网信息服务,为各行业提供服务。在技术实力的保障下,我们为客户承诺稳定,放心的服务,根据网站的内容与功能再决定采用什么样的设计。最后,要实现符合网站需求的内容、功能与设计,我们还会规划稳定安全的技术方案做保障。
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
*任意输入东西,以"空格"间隔
*当输入不是数字时,重新输入
*输入正确后,用回车结束输入的数字
*最后升序输出
*
*@author Yang
*/
public class JJArrangedNumbers
{
public static void main(String args[])
{
new ArrangedNumbers();
}
}
/**
*任意输入数字,并按升序排列输出
*
*@author Yang
*/
class ArrangedNumbers
{
Integer arryNum[] ;
int count = 0;//统计数字
public ArrangedNumbers()
{
this.inputNum();
this.compareNum();
this.outputNum();
}
/**
*@inputNum 任意输入字符,并判断为数字
*/
public void inputNum()
{
boolean judgeIsNum = true;
StringBuffer sb = new StringBuffer();
Scanner s = new Scanner(System.in);
while(judgeIsNum)
{
System.out.println("请任意输入数字,以空格间隔,以回车结束!");
String character = s.nextLine();
char[] figures = character.toCharArray();
for(int i=0;ifigures.length;i++)
{
if(!(figures[i]='1' figures[i]='9') figures[i]!=32)
{
judgeIsNum = true;
break;
}
judgeIsNum = false;
}
if(!judgeIsNum)
sb.append(character);
}
String inputContent = sb.toString();
ListInteger dbList = new ArrayListInteger();
String[] numbers= inputContent.split("\s+");
for (int i=0;inumbers.length;i++)
{
try
{
dbList.add(Integer.parseInt(numbers[i]));
count++;
}
catch(Exception e)
{
//如果不出现异常,则说明肯定是数字。
}
}
arryNum= new Integer[count];
int c=0;
for (Integer inNumber :dbList)
{
arryNum[c] = inNumber ;
c++;
}
}
/**
*@compareNum 对数字进行排序
*/
public void compareNum()
{
for (int i=0;iarryNum.length;i++)
{
for (int j=0;jarryNum.length-i-1;j++)
{
if(arryNum[j]arryNum[j+1])
{
Integer temp = arryNum[j];
arryNum[j] = arryNum[j+1];
arryNum[j+1] = temp;
}
}
}
}
/**
*@outputNum 按升序排列输出
*/
public void outputNum()
{
System.out.println("按升序排序:");
for(int i=0;iarryNum.length;i++)
{
System.out.println(arryNum[i]);
}
}
}
这个样子就可以了
java数组sort()方法降序方式:
用Arrays类sort()对数组元素进行降序排列
import java.util.Arrays;
class SortDemo
{
public static void main(String[] args)
{
int[] vec={0,15,-14,45,20,70};
Arrays.sort(vec);
System.out.println("/n");
System.out.println("降序排列:");
for (int i=vec.length-1;i=0 ;i-- )
{
System.out.print(vec[i]+" ");
}
}
}
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));
}
}
int[] array = {1,2,5,8,12}; //随便定义一个数组,用冒泡排序法
for (int i = 0;i array.length;i++){
for(int j = i;j array.length;j++){
if (array[i] array[j]){
int temp = array[i];
array[i] = array[j];
array[i] = temp;
}
}
}
或者你有一个数组
List list = new ArrayList();
list.sort();
直接调用jdk里面的方法Arrays.sort(args)方法。而且这个方法重载实现了多个参数,排序下标[N~M]位数字~倒叙,升序等等~
for (int i = 0; i list.size(); i++) {
for (int j = i + 1; j list.size(); j++) {
if (list.get(i) list.get(j)) {
int temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
}
}
System.out.println("升序:" + list);
for (int i = 0; i list.size(); i++) {
for (int j = i + 1; j list.size(); j++) {
if (list.get(i) list.get(j)) {
int temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
}
}
System.out.println("降序:" + list);