用eclipse运行java:用键盘输入数据后排序
2个回答
2013-10-21
展开全部
冒个泡就好了for(int i=0;i <=集合大小-1; i++){for(int j=0; j<集合大小-i-1; j++){int temp = arr[j];if(arr[j]>arr[j+1]){arr[j]=arr[j+1];arr[j+1]=temp;}}}
2013-10-21
展开全部
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;public class Bubble {
public static void main(String[] args) {
String s;
String str[] = new String[10];
int a[] = new int[10];
int i = 0;
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(System.in));
try {
s = bufferedReader.readLine();
StringTokenizer stringTokenizer = new StringTokenizer(s, " ");
while (stringTokenizer.hasMoreTokens()) {
str[i] = stringTokenizer.nextToken();
i++;
}
for (i = 0; i < 10; i++)
try {
a[i] = Integer.parseInt(str[i]);
// System.out.print(a[i] + " ");
} catch (NumberFormatException e) {
}
bufferedReader.close();
} catch (IOException e) {
}
for (i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - i - 1; j++) {
if (a[j] > a[j + 1]) {
a[j] = a[j] + a[j + 1];
a[j + 1] = a[j] - a[j + 1];
a[j] = a[j] - a[j + 1];
}
}
}
for (i = 0; i < a.length; i++)
System.out.print(a[i] + " "); }}
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;public class Bubble {
public static void main(String[] args) {
String s;
String str[] = new String[10];
int a[] = new int[10];
int i = 0;
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(System.in));
try {
s = bufferedReader.readLine();
StringTokenizer stringTokenizer = new StringTokenizer(s, " ");
while (stringTokenizer.hasMoreTokens()) {
str[i] = stringTokenizer.nextToken();
i++;
}
for (i = 0; i < 10; i++)
try {
a[i] = Integer.parseInt(str[i]);
// System.out.print(a[i] + " ");
} catch (NumberFormatException e) {
}
bufferedReader.close();
} catch (IOException e) {
}
for (i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - i - 1; j++) {
if (a[j] > a[j + 1]) {
a[j] = a[j] + a[j + 1];
a[j + 1] = a[j] - a[j + 1];
a[j] = a[j] - a[j + 1];
}
}
}
for (i = 0; i < a.length; i++)
System.out.print(a[i] + " "); }}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询