栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > Java面试题

Java十进制数换算二进制数(栈实现)

Java面试题 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java十进制数换算二进制数(栈实现)

import java.io.*;
public class textBoard {
public Node head = null;

public static void main(String[] args){
String s = “”;
int num = 0;
textBoard board = new textBoard();
System.out.print(“Please input a Decimal number : “);
//Input function!
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
s = in.readLine();
num = Integer.parseInt(s);
}catch(IOException e) {}
System.out.println(“Your entering Binary number is: ” + board.swap(num));
}

public String swap(int i){
while(i != 1) {
Node node = new Node(i % 2);
push(node);
i = i / 2;
}
Node node1 = new Node(1);
push(node1);
String s = “”;
while(!isEmpty()){
s += head.getkey();
head = head.getnext();
}
return s;
}

public void push(Node element){
if(element != null){
element.setnext(head);
head = element;
}
}

public boolean isEmpty(){
if(head == null){
return true;
}
return false;
}
}
class Node {
private int key = 0;
private Node next = null;

public Node(int i) {
this.key = i;
this.next = null;
}

public int getkey(){
return key;
}

public void setkey(int i){
this.key = i;
}

public Node getnext(){
return next;
}

public void setnext(Node next){
this.next = next;
}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/264282.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号