几分之几
新手上路
- 注册
- 2003-12-05
- 消息
- 2,968
- 荣誉分数
- 0
- 声望点数
- 0
实在写不下去了,卡住了,有没有高手能帮忙指点一下?
Q2. Write the method public int[] partialSort(int[] anArray) which takes an array of int as a parameter and returns a new array of int. The new array should contain at the front all the odd numbers from the given array and all the even numbers of that array at the end. For example, if we are given an array with the numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 then the resulting array should contain numbers as follows: 1, 3, 5, 7, 9, 2, 4, 6, 8, 10. Notice that all the odd numbers are in the first half of the array and the even numbers in the second half. Note however, that there may be no odd numbers or possibly no even numbers. You may assume that there is at least one number in the given array.
(Hint: you can use the modulus (%) operator to decide if something is odd/even).
Write the above method in a SortTester class with a public static void main() method that tests the partialSort() method with the following arrays:
{1,2,3,4,5,6,7,8,9,10}
{8}
{5}
{9,7,5}
{98, 8}
{12, 57, 45, 9, 354, 3, 2, 56, 7,8 ,94, 45,67, 21}
Copy and paste your testing for marking. Your main method should prompt the user as follows:
Please enter number of elements in the array: 3
Please enter 1 element: 1
Please enter 2 element: 4
Please enter 3 element: 5
You entered the array: 1, 4, 5
Partially sorted as : 1, 5, 4
Note that you do not have to sort the input array.
Q2. Write the method public int[] partialSort(int[] anArray) which takes an array of int as a parameter and returns a new array of int. The new array should contain at the front all the odd numbers from the given array and all the even numbers of that array at the end. For example, if we are given an array with the numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 then the resulting array should contain numbers as follows: 1, 3, 5, 7, 9, 2, 4, 6, 8, 10. Notice that all the odd numbers are in the first half of the array and the even numbers in the second half. Note however, that there may be no odd numbers or possibly no even numbers. You may assume that there is at least one number in the given array.
(Hint: you can use the modulus (%) operator to decide if something is odd/even).
Write the above method in a SortTester class with a public static void main() method that tests the partialSort() method with the following arrays:
{1,2,3,4,5,6,7,8,9,10}
{8}
{5}
{9,7,5}
{98, 8}
{12, 57, 45, 9, 354, 3, 2, 56, 7,8 ,94, 45,67, 21}
Copy and paste your testing for marking. Your main method should prompt the user as follows:
Please enter number of elements in the array: 3
Please enter 1 element: 1
Please enter 2 element: 4
Please enter 3 element: 5
You entered the array: 1, 4, 5
Partially sorted as : 1, 5, 4
Note that you do not have to sort the input array.