A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 落幕繁华 中级黑马   /  2014-10-18 09:37  /  1182 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

有心人帮忙做一下吧,知道如何,键盘录入,也知道如何数组排序,就是两个衔接不好

4 个回复

倒序浏览
可以提供你的思路让坛友们帮忙指正。或者寻求思路。开卷考试,直接拿别人的,就没有考试的意义了
回复 使用道具 举报
  1. import java.util.*;
  2. import java.io.*;

  3. class ArrayDemo
  4. {
  5.         public static void main(String[] args) throws Exception
  6.         {
  7.                 BufferedReader bis =
  8.                         new BufferedReader(new InputStreamReader(System.in));

  9.                 int[] arr = new int[1024];

  10.                 String line=null;
  11.                 int index = 0;
  12.                 while((line=bis.readLine())!=null)
  13.                 {
  14.                         if(line.equals("over"))
  15.                                 break;
  16.                         arr[index++] = Integer.parseInt(line);
  17.                 }
  18.                 int[] newArr = new int[index];
  19.                 System.out.print("排序前数组:[");
  20.                 for(int i=0;i<index;i++)
  21.                 {
  22.                         newArr[i]=arr[i];
  23.                         if(i!=index-1)
  24.                                 System.out.print(newArr[i]+",");
  25.                         else
  26.                                 System.out.println(newArr[i]+"]");
  27.                 }
  28.        
  29.                 Arrays.sort(newArr);
  30.                 System.out.print("排序后数组:[");
  31.                 for(int i=0;i<index;i++)
  32.                 {
  33.                         if(i!=index-1)
  34.                                 System.out.print(newArr[i]+",");
  35.                         else
  36.                                 System.out.println(newArr[i]+"]");
  37.                 }
  38.         }
  39. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
杨佳名 + 1

查看全部评分

回复 使用道具 举报
IO读取个字符串,然后剪切下得到个字符串数组,然后转换成Integer在赋给数组,最后排序吧
回复 使用道具 举报
本帖最后由 wtjohn 于 2014-10-19 13:43 编辑
  1. import java.util.ArrayList;
  2. import java.util.Scanner;

  3. public class test {
  4.         public static void main(String[] args) {
  5.                 sort(scan());
  6.         }
  7.         public static ArrayList<Integer> scan()
  8.         {
  9.                 ArrayList<Integer> al=new ArrayList<Integer>();
  10.                 Scanner reader=new Scanner(System.in);
  11.                 while(true)
  12.                 {
  13.                  Integer i=reader.nextInt();
  14.                         if(i==00)
  15.                                 break;
  16.                         al.add(i);
  17.                 }
  18.                 return al;
  19.         }
  20.         public static void sort(ArrayList al)
  21.         {
  22.                 Integer[] a=new Integer[al.size()];
  23.                 al.toArray(a);
  24.                 System.out.println("排序前:");
  25.                 for(Integer i:a)
  26.                 {
  27.                 System.out.print(i+" ");
  28.                 }
  29.                 for(int i=0;i<al.size()-1;i++)
  30.                 {
  31.                         for(int x=0;x<al.size()-1;x++)
  32.                         {
  33.                                 Integer temp=0;
  34.                                 if(a[x]>=a[x+1])
  35.                                 {
  36.                                         temp=a[x];
  37.                                         a[x]=a[x+1];
  38.                                         a[x+1]=temp;
  39.                                 }
  40.                         }
  41.                 }
  42.                 System.out.println();
  43.                 System.out.println("排序后:");
  44.                 for(Integer i:a)
  45.                 {
  46.                 System.out.print(i+" ");
  47.                 }
  48.         }
  49. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
杨佳名 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马