คืนค่าการตั้งค่าทั้งหมด
คุณแน่ใจว่าต้องการคืนค่าการตั้งค่าทั้งหมด ?
ลำดับตอนที่ #2 : palin
import java.util.Scanner;
import java.util.Stack;
import sun.misc.Queue;
public class Palindrome{
public static boolean test(String candidate){
char a;
int length;
int numDigits;
int charCount;
boolean stillPalindrome;
char fromStack;
char fromQueue = 0;
Stack<Character> stack = new Stack<Character>();
Queue queue = new Queue();
length = candidate.length();
numDigits = 0;
for(int i = 0 ;i<length;i++){
a = candidate.charAt(i);
if(Character.isDigit(a)){
numDigits++;
stack.push(a);
queue.enqueue(a);
}
}
stillPalindrome = true;
charCount = 0;
while(stillPalindrome && (charCount<numDigits)){
fromStack = (Character)stack.peek();
stack.pop();
try {
fromQueue = (Character)queue.dequeue();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(fromStack != fromQueue)
stillPalindrome = false;
charCount++;
}
return stillPalindrome ;
}
public static void main(String[] args){
Scanner fill = new Scanner(System.in);
String candidate = null;
String more = null;
do{
System.out.println("Enter a years to be evaluated: ");
candidate = fill.nextLine();
if(Palindrome.test(candidate))
System.out.println("is a palindrome");
else
System.out.println("is not a palindrome");
System.out.println();
System.out.print("Evaluate another string? (Y = Yes): ");
more = fill.nextLine();
System.out.println();
}
while(more.equalsIgnoreCase("y"));
}
}
ความคิดเห็น