jeudi 20 octobre 2016

How to determine if the sequence of array values are palindromic?

Write a program that will take 10 integers and store them into an array. The program should test to see if the sequence of numbers is palindromic. This means that it read the same forwards as it does backwards. Here are two examples:

Please type 10 integers: [1|2|3|7|7|7|7|3|2|1] The sequence is palindromic.

Please type 10 integers: [1|2|3|7|7|7|7|2|3|1] The sequence is not palindromic.

And my code is:

 import java.util.Scanner;

public class problem4 {

public static void main (String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Please type 10 integers:");
    int[] num = new int[10];
    int[] reverse = new int[10];
    for (int a = 0; a < num.length; a++) {
        num[a] = scan.nextInt();
    }
    int b = 0;
    int c = 9;
    while (c != 0) {
        reverse[c] = num[b];
        c--;
        b++;
        if (c == 0) break;
    }
    if (num[0] == reverse[9] && num[1] == reverse[8] && num[2] == reverse[7] && num[3] == reverse[6] && num[4] == reverse[5])
        System.out.println("The sequence is palindromic.");
            else 
                System.out.println("The sequence is not palindromic.");
}

}

No build messages however, it always says that the sequence is palindromic regardless of what I input.

Aucun commentaire:

Enregistrer un commentaire