2016-10-01 2 views
0

몇 가지 도움을 요청할 수 있습니까? 나는 프로그래밍에 초보자입니다. 내가 프로그래밍을 시작한 지 일주일이 지났어. 나는 숫자를 입력하고 입력 한 숫자의 문자를 출력해야하는 프로그램을 작업 해왔다. 내 질문은. 이 프로그램을위한 대체 코드가 있습니까? 또한, 입력이 -1이고 "음수"로 읽히는 경우 어떻게 코드를 추가 할 수 있습니까? 너희들이 줄 수있는 도움에 정말 감사한다.프로그램 번호를 단어로 변환

예제 출력은 다음과 같습니다 :

내가 작업했던 코드 입력 번호 : 12345 출력 :이 질문은 이미 요청했다 이천 삼백 위로 fourty 다섯

import java.util.Scanner; 

public class Numberconvert { 

    public boolean Number(String s) { 
     boolean tama = true; 
     for (int i = 0; i < s.length(); i++) { 
      if (!Character.isDigit(s.charAt(i))) { 
       return false; 
      } 
     } 

     return tama; 
    } 

    public static String convert(int digit) { 
     String ArrayOnes[] = {"zero", " One ", " Two ", " Three ", " Four ", " Five ", " Six ", " Seven ", " Eight ", " Nine ", " Ten ", 
      " Eleven ", " Twleve ", " Thirteen ", " Fourteen ", " Fifteen ", " Sixteen ", " Seventeen ", " Eighteen ", " Ninteen "}; 
     String ArrayOnes2[] = {" ", " Ten ", " Twenty ", " Thirty ", " Fourty ", " Fifty ", " Sixty ", " Seventy ", " Eighty ", " Ninety "}; 
     String ArrayNegative[] = {"Negative "}; 

     if (digit < 0) { 
      return ArrayNegative[0]; 
     } 
     if (digit % 10 == 0) { 
      ArrayOnes[0] = ""; 
     } 
     if (digit < 20) { 
      return ArrayOnes[digit]; 
     } 
     if (digit < 100) { 
      return ArrayOnes2[digit/10] + ArrayOnes[digit % 10]; 
     } 
     if (digit < 10000) //return convert(digit/100)+" Hundred "+convert(digit %100); 
     { 
      return convert(digit/100) + " Thousand " + convert(digit % 100); 
     } 
     if (digit < 100000000) { 
      return convert(digit/1000) + " Thousand " + convert(digit % 1000); 
     } 
     return convert(digit/1000000000) + " Million " + convert(digit % 1000000000); 
     //return convert(digit); 
    } 

    public static void main(String[] args) { 
     Scanner scann = new Scanner(System.in); 
     String Number; 

     // checker - class to loop until the input is number 
     Numberconvert checkIfNumber = new Numberconvert(); 

     // convert string in to number 
     int result; 

     try { 
      // this do while method is only to check if the input is Number 
      do { 
       System.out.print("Enter a Number: "); 
       Number = scann.nextLine(); 
      } while (!checkIfNumber.Number(Number)); 

      // this if statement is only works if the input is Number 
      if (checkIfNumber.Number(Number)) { 
       // String into int to process the String 
       result = Integer.parseInt(Number); 
       // initialize "converted" to the function; 
       String converted = convert(result); 
       // print the result 
       System.out.println(converted); 
      } 

      // if ever some error detected. The user want to input again 
     } catch (Exception e) { 
      System.out.println(""); 
      System.err.println("Some Errors Detected "); 
      System.out.println("Want to input again? Press Enter "); 
      scann.nextLine(); 
      main(null); 
     } 
    } 

} 

답변