很长时间都没做过这样的题了,研究了半天从键盘输入int型数组(数组个数亦从键盘输入),如果不是数字,通过异常的方式,使其重新输入,直到是数字为止;
然后对其用冒泡排序法使其由大到小排序,最后输出排序前,排序后的数组。
然后对其用冒泡排序法使其由大到小排序,最后输出排序前,排序后的数组。
解决方案 »
- 问一个关于ClassLoader层次的问题
- 出来乍到,请问一下问题点数有何作用?
- 大家到这里帮忙.这里没有分数了/谢谢!! http://community.csdn.net/Expert/topic/4917/4917299.xml?temp=.1222956
- 课程设计中的DATE问题~?急急~~~~~~~~~~~~~~~~~
- webstart程序,都通过什么方式和别的程序通信的?
- 请问这是什么问题
- socket编程 在线等待 回答正确马上给分
- 对象数组的构造函数调用问题
- jbuilder5 web program error
- 如何用java读.properties文件中的配置信息
- ACER待机问题!!!!!!
- 急问菜鸟问题!读取list的问题
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Integer> datas = new ArrayList<Integer>();
System.out.println("请输入5个整数:");
while(datas.size() < 5) {
String s = sc.next();
if(s.matches("^[0-9]*[1-9][0-9]*$")) {
datas.add(Integer.parseInt(s));
}
}
Integer[] a = datas.toArray(new Integer[] {});
for(int i = a.length ; i>0; i--) {
for(int j = 1; j<i ; j++) {
int temp = 0;
if(a[j] < a[j-1]) {
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
}
System.out.println(Arrays.toString(a));
}
}
随便写了一个
循环结构的代码:
public static void BubbleSort(int a []){
int temp=0;
for (int i = 0; i < a.length; i++) { //第一层循环
for (int j = 0; j < a.length-i-1; j++) { //第二层循环
if (a<a[j]) { //把这里改成大于号,就是升序了
temp=a[j];
a[j]=a;
a=temp;
}
}
}
}
3楼的的确是冒泡排序啊,冒泡排序有很多写法,但是原理都一样啊貌似你的代码有点问题啊public static void BubbleSort(int a []){
int temp=0;
for (int i = 0; i < a.length; i++) { //第一层循环
for (int j = 0; j < a.length-i-1; j++) { //第二层循环
if (a[j] <a[j+1]) { //把这里改成大于号,就是升序了
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
import java.util.Scanner;public class TestOrder {
//排序输出
public static void order(int []array){
for(int i=0;i<array.length;i++){
for(int j=0;j<array.length-i-1;j++){
int temp=0;
if(array[j]<array[j+1]){
temp=array[j+1];
array[j+1]=array[j];
array[j]=temp;
}
}
}
for(int i=0;i<array.length;i++){
System.out.println(array[i]);
}
}
public static void main(String[] args) {
System.out.println("请输入数组的大小:");
Scanner s=new Scanner(System.in);
int length=s.nextInt();
System.out.println("请输入"+length+"个数:");
int []array=new int [length];
boolean flag=true;
int index=0;
try{
while(flag){
array[index]=s.nextInt();
if(index>=length-1){
flag=false;
}
index++;
}
}catch(Exception e){
System.out.println("请输入数字!");
return ;
}
//排序前
for(int i=0;i<array.length;i++){
System.out.println(array[i]);
} System.out.println("..............................");
order(array);
}
}
不过还是有点问题,若是输入的不是数字就结束了,还是没达到要求,期待高手现身!
//import java.util.Scanner;
import java.io.*;public class TestOrder {
//排序输出
public static void order(int []array){
for(int i=0;i<array.length;i++){
for(int j=0;j<array.length-i-1;j++){
int temp=0;
if(array[j]<array[j+1]){
temp=array[j+1];
array[j+1]=array[j];
array[j]=temp;
}
}
}
for(int i=0;i<array.length;i++){
System.out.println(array[i]);
}
}
public static void main(String[] args) {
String str="";
int length=0;
System.out.println("请输入数组的大小:");
BufferedReader s=new BufferedReader(new InputStreamReader(System.in));
try{
str=s.readLine();
length=Integer.parseInt(str);
}catch(Exception e){}
System.out.println("请输入"+length+"个数:");
int []array=new int [length];
boolean flag=true;
int index=0;
while(flag){
try{
str=s.readLine();
array[index]=Integer.parseInt(str);
index++;
if(index>length-1){
flag=false;
}
}catch(Exception e){
System.out.println("请输入数字!");
}
}
//排序前
for(int i=0;i<array.length;i++){
System.out.println(array[i]);
} System.out.println("..............................");
order(array);
}
}这个如果输入的不是数字,重新输入,但是不是用的Scanner,用Scanner好像实现不了但是这样第一个输入的数组数没有验证是不是数字,其实很简单,第二个验证了,第一个就知道了,一样的