Created
September 21, 2016 09:27
-
-
Save tipochka/74925e9eacfd4eff0ca22efe3e3d1b61 to your computer and use it in GitHub Desktop.
Блинов. Глава 2. Вариант A. 8. Ввести N чисел с консоли. Среди чисел найти число-палиндром. Если таких чисел больше одного, найти второе.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package oop.lesson1.homework; | |
import java.util.Scanner; | |
public class Palindromic { | |
public static void main(String[] args) { | |
int n = 4; | |
Scanner scanner = new Scanner(System.in); | |
int countResult = 0; | |
int result = 0; | |
for (int i = 0; i<n; i++) { | |
int number = readInt(scanner); | |
if (isPalindrome(number)) { | |
if (countResult < 2) { | |
result = number; | |
countResult++; | |
} | |
} | |
} | |
System.out.println(stringResult(countResult, result)); | |
} | |
public static int readInt(Scanner scanner) throws IllegalArgumentException { | |
if (!scanner.hasNextInt()) { | |
throw new IllegalArgumentException("Argument may by only int"); | |
} | |
return scanner.nextInt(); | |
} | |
public static boolean isPalindrome(int number) { | |
if (number == reverseInt(number)) { | |
return true; | |
}else { | |
return false; | |
} | |
} | |
public static int reverseInt(int number) { | |
int reverseInt = 0; | |
while (number != 0) { | |
reverseInt = reverseInt*10 + number%10; | |
number /= 10; | |
} | |
return reverseInt; | |
} | |
public static String stringResult(int countResult, int result) { | |
if (countResult == 0) { | |
return "Non palindrome number"; | |
}else { | |
return Integer.toString(result); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment