Java 學習記錄 40 — 代碼挑戰 13

張小雄
5 min readFeb 10, 2021

--

題目1:

  • Write a method called readIntegers() with a parameter called count that represents how many integers the user needs to enter
  • The method needs to read from the console until all the numbers are entered, and then return an array containing the numbers entered.
  • Write a method findMin() with the array as a parameter. The method needs to return the minimum value in the array.
  • In the main() method read the count from the console and call the method readIntegers() with the count parameter.
  • Then call the findMin() method passing the array returned from the call to the readIntegers() method.
  • Finally, print the minimum element in the array.

Tips:

  • Assume that the user will only enter numbers, never letters
  • For simplicity, create a Scanner as a static field to help with data input
  • Create a new console project with the name ‘MinElementChallenge’

題目2:

Write a method called readInteger() that has no parameters and returns an int.

It needs to read in an integer from the user — this represents how many elements the user needs to enter.

Write another method called readElements() that has one parameter of type int

The method needs to read from the console until all the elements are entered, and then return an array containing the elements entered.

And finally, write a method called findMin() with one parameter of type int[]. The method needs to return the minimum value in the array.

The scenario is:

  1. readInteger() is called.
  2. The number returned by readInteger() is then used to call readElements().
  3. The array returned from readElements() is used to call findMin().
  4. findMin() returns the minimum number.

[Do not try and implement this. It is to give you an idea of how the methods will be used]

  • TIP: Assume that the user will only enter numbers, never letters.
  • TIP: Instantiate (create) the Scanner object inside the method. There are two scanner objects, one for each of the two methods that are reading in input from the user.
  • TIP: Be extremely careful about spaces in the printed message.

NOTE: All methods should be defined as private static.

NOTE: Do not add a main method to the solution code.

題目3:

Write a method called reverse() with an int array as a parameter.

The method should not return any value. In other words, the method is allowed to modify the array parameter.

In main() test the reverse() method and print the array both reversed and non-reversed.

To reverse the array, you have to swap the elements, so that the first element is swapped with the last element and so on.

For example, if the array is {1, 2, 3, 4, 5}, then the reversed array is {5, 4, 3, 2, 1}.

Tip:

Create a new console project with the name ReverseArrayChallenge

題目4:

Write a method called reverse() with an int array as a parameter.

The method should not return any value. In other words, the method is allowed to modify the array parameter.

To reverse the array, you have to swap the elements, so that the first element is swapped with the last element and so on.

For example, if the array is [1, 2, 3, 4, 5], then the reversed array is [5, 4, 3, 2, 1].

The method should first print out the newly passed in array as Array = [1, 2, 3, 4, 5]

and then once it’s been reversed, print it out as Reversed array = [5, 4, 3, 2, 1]

TIP: When swapping the elements, use a variable to temporarily hold the current element.

NOTE: The method should be defined as private static.

NOTE: Do not add a main method to the solution code.

答案1:

我的:

import java.util.Arrays;
import java.util.Scanner;
public class ChallengeMinimumElement { private static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { System.out.println("Enter count");
int count = scanner.nextInt();
int[] returnedArray = readIntegers(count);
System.out.println("Min is: " + findMin(returnedArray));
} private static int[] readIntegers(int count) {
System.out.println("Enter " + count + " integer value.\r");
int[] values = new int[count];
for (int i = 0; i < values.length; i++) {
values[i] = scanner.nextInt();
}
System.out.println(Arrays.toString(values));
return values;
}
public static int findMin(int[] array) {
for (int a = 0; a < array.length; a++) {
for (int b = 0; b < array.length; b++) {
if (array[a] < array[b]) {
int temp = array[b];
array[b] = array[a];
array[a] = temp;
}
}
}
return array[0];
}
}

老師的:

import java.util.Scanner;public class ChallengeMinimumElement_TimVersion {    private static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) {        System.out.println("Enter count:");
int count = scanner.nextInt();
scanner.nextLine();
int[] returnedArray = readIntegers(count);
System.out.println("Min: " + findMin(returnedArray));
}
private static int[] readIntegers(int count) { int[] array = new int[count]; for (int i = 0; i < array.length; i++) {
System.out.println("Enter a number:");
int number = scanner.nextInt();
scanner.nextLine();
array[i] = number;
}
return array;
} private static int findMin(int[] array) {
int min = Integer.MAX_VALUE;
for (int i = 0; i < array.length; i++) {
int value = array[i];
if (value < min) {
min = value;
}
}
return min;
}}

答案2:

import java.util.Scanner;public class MinimumElement {    private static int readInteger() {
Scanner scanner = new Scanner(System.in);
int integer = scanner.nextInt();
scanner.nextLine();
return integer;
}
private static int[] readElements(int integer) {
Scanner scanner = new Scanner(System.in);
int[] array = new int[integer];
for (int i = 0; i < integer; i++) {
int number = scanner.nextInt();
scanner.nextLine();
array[i] = number;
}
return array;
}
private static int findMin(int[] array) {
int min = Integer.MAX_VALUE;
for (int i = 0; i < array.length; i++) {
int value = array[i];
if (value < min) {
min = value;
}
}
return min;
}}

答案3:

我的:

import java.util.Arrays;public class ReverseArrayChallenge {    public static void main(String[] args) {        int[] ar = new int[]{11, 22, 3, 22, 44};
System.out.println(Arrays.toString(reverse(ar)));
} private static int[] reverse(int[] array) {
int[] reverseArray = new int[array.length];
for (int i = 0; i < array.length; i++) {
for (int j = (array.length - i - 1); j == (array.length - i - 1); j++) {
reverseArray[j] = array[i];
}
}
return reverseArray;
}
}

老師的:

import java.util.Arrays;public class ReverseArrayChallenge_TimVersion {    public static void main(String[] args) {        int [] array = {2, 64, 34, 66};
reverse(array);
System.out.println(Arrays.toString(array));
} private static void reverse(int [] array){
int maxIndex = array.length - 1;
int halfLength = array.length / 2;
for (int i = 0; i < halfLength; i++) {
int temp = array[i];
array[i] = array[maxIndex - i];
array[maxIndex - i] = temp;
}
}
}

答案4:

import java.util.Arrays;public class ReverseArray {    private static void reverse(int[] array) {
int backToStart = array.length - 1;
System.out.println("Array = " + Arrays.toString(array));
for (int i = 0; i < array.length / 2; i++) {
int temp = array[i];
array[i] = array[backToStart];
array[backToStart] = temp;
backToStart--;
}
System.out.println("Reversed array = " + Arrays.toString(array));
}
}

--

--

張小雄
張小雄

Written by 張小雄

記錄成為軟體工程師的過程

No responses yet