2016-11-16 36 views
0

以下是我在java编程类中的一项任务。我已经编写了所有的代码,我只是不知道如何使输出显示什么,我需要它显示。在最后一个元素前面打印带“and”的int数组

对于我的任务,我必须编写一个程序,其中包含一个1维和100个整数的单维数组,并使用冒泡排序对数组进行排序。

什么样的输出需要的样子的一个例子是这样的:

的未分类的列表是:54,27,13,97,5,63,78,34,47,和81
排序后的列表是:5,13,27,34,47,54,63,78,81,和97

我的输出显示此:
的未分类的列表是:54,27,13, 97,5,63,78,34,47,81,
排序的列表是:5,13,​​27,34,47,54,63,78,81,9 7,

我不知道如何将"and"写入输出。

public class Chpt7_Project { 
/** The method for sorting the numbers */ 
public static void bubbleSort(int[] numbers) 
    { 
    int temp; 

     for (int i = numbers.length - 1; i > 0; i--) 
     { 
     for (int j = 0; j < i; j++) 
     { 
      if (numbers[j] > numbers[j + 1]) 
      { 
      temp = numbers[j]; // swap number[i] with number[j] 
      numbers[j] = numbers[j + 1]; 
      numbers[j + 1] = temp; 
      } 
     } 
     } 
    } 
public static void main(String[] args) { // Test Method 

    System.out.print("The unsorted list is: "); 
    // Generate 10 random numbers between 1 and 100 
    int[] numbers = new int[10]; 

    for (int i=0;i<numbers.length;i++) { 
     numbers[i] = (int) (Math.random() * 100); 
     System.out.print(numbers[i] + ", "); 
    } 
    System.out.println(); 

    bubbleSort (numbers); // numbers are sorted from smallest to largest 
    System.out.print("The sorted list is: "); 
    for (int i=0;i<numbers.length;i++) { 
     System.out.print(numbers[i] + ", "); 
    } 
} 


} 
+0

你有什么试图修改最后的主要方法的循环?你也有一个尾随的逗号,所以计算出来会让你关注何时添加“和” –

+2

我会修改for循环到'numbers.length - 1',并且在循环之后你可以打印' “和”+数字。最后“或类似的东西。 –

+0

也许一个简单的if-else会帮助你吗? –

回答

1

更改这个循环

for (int i=0;i<numbers.length;i++) { 
    System.out.print(numbers[i] + ", "); 
} 

for (int i=0;i<numbers.length;i++) { 
    if(i== numbers.length-1) { 
     System.out.println("and "+numbers[i]); 
    } else { 
     System.out.print(numbers[i] + ", "); 
    } 
} 
+0

仍然有一个尾随逗号 – fafl

+1

需要一个逗号“和” – abhinav

+0

看起来很奇怪,但你是对的 – fafl

0

输出:

The unsorted list is: 67, 86, 78, 80, 56, 45, 24, 2, 67, and 98 

The sorted list is: 2, 24, 45, 56, 67, 67, 78, 80, 86, and 98 

修改代码(集成什么@abhinav提到):

public class Chpt7_Project { 
/** The method for sorting the numbers */ 
    public static void bubbleSort(int[] numbers) { 
     int temp; 
     for (int i = numbers.length - 1; i > 0; i--) { 
      for (int j = 0; j < i; j++) { 
       if (numbers[j] > numbers[j + 1]) { 
        temp = numbers[j]; // swap number[i] with number[j] 
        numbers[j] = numbers[j + 1]; 
        numbers[j + 1] = temp; 
       } 
      } 
      } 
     } 

    public static void main(String[] args) { // Test Method 

     System.out.print("The unsorted list is: "); 
     // Generate 10 random numbers between 1 and 100 
     int[] numbers = new int[10]; 
     for (int i=0;i<numbers.length;i++) { 
      numbers[i] = (int) (Math.random() * 100); 
      if(i== numbers.length-1) { 
       System.out.println("and "+numbers[i]); 
      } else { 
       System.out.print(numbers[i] + ", "); 
      } 
     } 
     System.out.println(); 
     bubbleSort (numbers); // numbers are sorted from smallest to largest 
     System.out.print("The sorted list is: "); 
     for (int i=0;i<numbers.length;i++) { 
      if(i== numbers.length-1) { 
       System.out.println("and "+numbers[i]); 
      } else { 
       System.out.print(numbers[i] + ", "); 
      } 
     } 
    } 
} 
相关问题