저는 현재 Java를 배우고 있으며 첫 번째 프로그램을 마쳤습니다. 이 프로그램은 정수와 문자열 모두에 대해 회문을 구현합니다. 내 코딩 배경은 C++이지만, 코드를 더 잘 구조화 할 수있는 방법에 대한 조언이 있거나 (코드를 더 쉽게 읽을 수 있도록) 코드를 좀 더 압축 할 수있는 방법에 대한 조언이 있다면 궁금합니다. 가능한 한 많은 건설적인 비판을 주시기 바랍니다. 여름이 끝날 무렵 엔 엔트리 레벨의 소프트웨어 공학직을 신청할 계획이므로 모든 피드백을 환영합니다! 고마워.회문 코드를 어떻게 최적화 할 수 있습니까?
package projectprac;
import java.util.Scanner;
public class ProjectPrac {
static Scanner userInput = new Scanner(System.in);
public static int reverseInt(int x){
/* This function will reverse an integer value */
int reverse = 0;
int temp = x;
while(x != 0){
reverse = reverse * 10;
reverse = reverse + x % 10;
x = x/10;
}
intPalindromeCheck(temp, reverse);
return reverse;
}
public static String reverseString(String word){
/* This function will return a String value */
String reverse = new StringBuffer(word).reverse().toString();
stringPalindromeCheck(word, reverse);
return reverse;
}
public static void intPalindromeCheck(int one, int two){
/* This function will check to see if int
* is a Palindrome
*/
if(one == two){
System.out.println(one + " is a Palindrome!");
}
else{
System.out.println(one + " is NOT a Palindrome!");
}
}
public static void stringPalindromeCheck(String one, String two){
/* This function will check to see if String is a
* Palindrome
*/
if(one.equals(two)){
System.out.println(one + " is a Palindrome!");
}
else{
System.out.println(one + " is NOT a Palindrome!");
}
}
public static void main(String[] args) {
String word;
int x = 0;
while (x != -1){
System.out.print("What would you like to do 1. reverse int 2. reverse String: ");
x = userInput.nextInt();
if(x == 1){
System.out.print("Please input a number: ");
x = userInput.nextInt();
System.out.println(reverseInt(x));
}
else if (x == 2){
userInput.nextLine(); //skips the new line
System.out.print("Please enter a string: ");
word = userInput.nextLine();
System.out.println(reverseString(word));
}
}
}
}
내가 할 수있는 첫 번째 일은'int'에 대해서 문자열을 즉시 변환하고'int'에 대한 별도의 함수 세트를 갖기보다는'reverseString'을 통해 실행하는 것입니다. 그냥 생각. – lurker
감사! 바로 시작하겠습니다. – JSCOTT12
메서드 설명서는 javaDoc 주석 (/ **로 시작하는)을 사용하는 메서드 외부에서 typicall로 처리됩니다. –