两个java程序

1、编写程序,通过命令行参数获取用户输入的整数序列,并按照从小到大的顺序输出该整数序列。例如,运行时输入:
java Test 5 1 10 9 11 12
则输出结果为:
1 5 9 10 11 12

2、编写程序,通过命令行参数获取用户输入的10个整数,将第一个数字和最后一个交换,第二个和倒数第二个交换,以此类推,然后输出。要求数字格式不对抛异常。

第一题 public static void main(String[] args) throws Exception {
try {

showSortInput(args);
}
catch (Exception e) {
throw new Exception("输入数字的格式不合法!");
}
}

public static void showSortInput(String[] inputStr) {
int length = inputStr.length;
int[] numbers = new int[length];
for (int i = 0; i < length; i++) {
int num = Integer.parseInt(inputStr[i]);
numbers[i] = num;
}
Arrays.sort(numbers);
for (int n : numbers) {
System.out.print(n + "\t");
}
}

第二题:
public static void main(String[] args) throws Exception {
try {
showReverseInput(args);
// showSortInput(args);
}
catch (Exception e) {
throw new Exception("输入数字的格式不合法!");
}
}

public static void showReverseInput(String[] inputStr){
int length = inputStr.length;
int[] numbers = new int[length];
for (int i = 0; i < length; i++) {
int num = Integer.parseInt(inputStr[i]);
numbers[length - 1 - i] = num;
}
for (int n : numbers) {
System.out.print(n + "\t");
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-03-29
去看看数据结构中的排序,有好中方法做的,选择排序,插入排序什么的都可以
第2个回答  2011-03-20
80分,我也来下
(1)
import javax.swing.*
public class test{
public static void main(String[] agrs){
int[] array = int[10];
for(double i = o ; i < array.lenght; i ++){
String num1 = JOptionPane.showInputDialog("请输入参数,如:50");
array[i] = Double.parseDouble(num1);
}
java.util.Arrays.sort(array);
for(int i = 0; i < array.lenght; i++)
JOptionPane.showMeesageDialog(array[i] + " ");
}
}
(2)
for(int i = 0; i < array.lenght; i++){
array[i] = array[array.lenght--];
JOptionPane.showMeesageDialog(array[i] + " ");
}
第3个回答  2011-03-19
public class Test
{
public void myMethod(String[] args)
{
if(args.length>1)
{
try{
int[] k=new int[args.length];
for(int i=0;i<k.length;i++)
{
k[i]=Integer.parseInt(args[i]);
}
int p=0;

for(int i=0;i<k.length;i++)
{
for(int j=k.length-1;j>i;j--)
{
p=k[j];
if(p<k[j-1])
{
k[j]=k[j-1];
k[j-1]=p;
}
}
}
for(int i=0;i<k.length;i++)
{
System.out.print(k[i]+" ");
}
}catch(NumberFormatException e)
{
System.out.println("请输入整数");
}
}else
{
System.out.print("请输入多个整数");
}

}
public void myMethod2(String [] args)
{
if(args.length>1)
{
try{
int[] k=new int[args.length];
for(int i=0;i<k.length;i++)
{
k[i]=Integer.parseInt(args[i]);
}
int p=0;

for(int i=0,j=k.length-1;i<k.length/2;i++,j--)
{
p=k[i];
k[i]=k[j];
k[j]=p;
}
for(int i=0;i<k.length;i++)
{
System.out.print(k[i]+" ");
}
}catch(NumberFormatException e)
{
System.out.println("请输入整数");
}
}else
{
System.out.print("请输入多个整数");
}
}
public static void main(String[] args)
{
Test t=new Test();
t.myMethod(args);
System.out.println();
t.myMethod2(args);
}
}
第4个回答  2011-03-19
public class Test{
public static void main(String[] args){
int [] a = new int[args.length];
for(int i=0;i<args.length;i++){
a[i] = Integer.parseInt(args[i]);
}
print(a);
selectSort(a);
prnt(a);
}
private static void selectSort(int[] a){
int k ,temp;
for(int i=0;i<a.length;i++){
k = i;
for(int j=k+1;j<a.length;j++){
if(a[j]<a[k])
k = j;
}
}
if(k!=i){
temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
}
private static void print(int[] a){
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ")
}
System.out.println(" ");
}
}
第5个回答  2011-03-19
给你个C的参照,自己写吧。
#include <stdio.h>
int main(void)
{
int a[10],temp;
int i = 0, j = 0;

printf("请输入10个要比较的数字:\n");

for(i=0; i<10; i++) //循环接收10个数字
{
scanf("%d", &a[i]);
}

for(i=1; i<10; i++)
{
for(j=9; j>=i; j--)
{
if(a[j]<a[j-1])
{
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
}
printf("排序后该数列为:\n");
for(i=0; i<10; i++) //排序后,循环输出这10个数字
{
printf("%d\n", a[i]);
}
}