所以我在编程方面很糟糕,我必须为大学课程做这件事,并且不能理解一些概念。这就是我对此的看法。我究竟做错了什么?如何调用这些方法将主要消息返回给main?检查方法是从另一个站点借来填补空白,而我试图找出验证回文的字符串缓冲方法。很多评论都是尚未完成的进展。如果这太混乱了,请告诉我,我会重新开始。谢谢。
import java.util.*;
import java.lang.StringBuffer;
public class palindrome {
public static void main(String[] args) {
}
public static String retrieveInput(String number) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a 5 digit integer: ");
number = input.toString();
while (number.length() != 5) {
System.out.println("ERROR!!! Integer must be 5 digits. Please enter a 5 digit integer: ");
System.out.print("Enter a 5 digit integer: ");
number = input.toString();
}
if (number.matches("[0-9]+")) {
} else {
System.out.println("ERROR!!! Input must be numeric. Please enter a 5 digit integer: ");
System.out.print("Enter a 5 digit integer: ");
number = input.toString();
}
return number;
}
// Change, TESTING ONLY!!!!!!!
public static String check(String number) {
String original = number;
String reverse = "";
int length = original.length();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string is not a palindrome.");
return number;
//StringBuffer Palindrome = new StringBuffer(number);
//return number.equals(reverseString);
}
//StringBuffer original = new StringBuffer();
// StringBuffer reverse = original.reverse();
// if (original != original.reverse()) {
// System.out.print("This is not a palindrome");
//}
//return number;
public static String display(String number) {
return number + " is a valid palindrome!";
}
}
答案 0 :(得分:0)
试试这个(经过微小修改后修改)
import java.util.Scanner;
public class Palindrome { // Prefer beginning class name with a capital letter
// (Java naming convention)
public static void main(String[] args) {
}
public static String retrieveInput(String number) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a 5 digit integer: ");
number = input.nextLine(); // Replace this
while (number.length() != 5) {
System.out
.println("ERROR!!! Integer must be 5 digits. Please enter a 5 digit integer: ");
System.out.print("Enter a 5 digit integer: ");
number = input.nextLine(); // Replace this
}
if (number.matches("[0-9]+")) {
} else {
System.out.println("ERROR!!! Input must be numeric. Please enter a 5 digit integer: ");
System.out.print("Enter a 5 digit integer: ");
number = input.nextLine(); // Replace this
}
input.close(); // Close your scanner after you are done using it.
return number;
}
// Change, TESTING ONLY!!!!!!!
public static String check(String number) {
String original = number;
String reverse = "";
int length = original.length();
for (int i = length - 1; i >= 0; i--)
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string is not a palindrome.");
return number;
// StringBuffer Palindrome = new StringBuffer(number);
// return number.equals(reverseString);
}
// StringBuffer original = new StringBuffer();
// StringBuffer reverse = original.reverse();
// if (original != original.reverse()) {
// System.out.print("This is not a palindrome");
// }
// return number;
public static String display(String number) {
return number + " is a valid palindrome!";
}
}