자바-10 진법 을 임 의 진법 으로 변환(두 가지 방법)

1517 단어 Java고전 제목
1.방법 1 
package com.zth;
import java.util.Scanner;
public class JinZhi {
  //        n   
  public String fun(int n,int num) {
    // n       , num      
    String str= "";
    int yushu ;      //     
    int shang = num;      //    
    while (shang>0) {
      yushu = shang %n;
      shang = shang/n;
      
      // 10-15 -> a-f
      if(yushu>9) {
        str =(char)('a'+(yushu-10)) + str;
      }else {
        str = yushu+str;
      }
    }
    
    return str;
  }

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    JinZhi s1 = new JinZhi();
    
    Scanner scanner = new Scanner(System.in);
    System.out.print("       :");
    int jinzhi = scanner.nextInt();
    System.out.print("         :");
    int num = scanner.nextInt();
    scanner.close();
    
    System.out.println(s1.fun(jinzhi, num));
  }

}

실행 표시:
       :2
         :15
1111

2.  방법 2
package demo;

import java.util.Scanner;
/**
 *           
 * @author   ·  
 *
 */
public class Test {
  public static void main(String[] args) {
    
    Scanner scanner = new Scanner(System.in);
    
    System.out.print("         :");
    int jinzhi = scanner.nextInt();
    
    System.out.print("         :");
    int num = scanner.nextInt();
    
    scanner.close();
    
    String str = Integer.toString(num,jinzhi);
    System.out.println(str);
  }

}

 

좋은 웹페이지 즐겨찾기