码迷,mamicode.com
首页 > 其他好文 > 详细

Demo6

时间:2016-10-27 15:10:33      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:empty   scanner   []   system.in   highlight   while   --   new   exti   

public class Main {
	public static void main(String args[])
	{
		Stack a = new Stack();
		System.out.println(a.isEmpty());
		System.out.println(a.getTop());
		a.push(new String ("3"));

		System.out.println(a.getTop());
		a.push(new String ("1"));
		a.push(new String ("9"));
		System.out.println(a.pop());
		a.push(new String ("5"));
		a.push(new String ("6"));
		System.out.println(a.getTop());
		System.out.println(a.isEmpty());
		a.trans(32);
	}
}

 

 

import java.util.Scanner;
public class Stack {

	private Object[] stack;
	private int length ;
	
	public Stack(){
		
		stack = new Object[100]; 	
	}
	
	public boolean isEmpty()
	{
		if(length==0)
		return true;
		else return false;
	}
	public int len()
	{
		return length;
	}
	public void push(Object elem)
	{
		
		stack[length++]  =  elem;
	}
	public Object pop()
	{
		return stack[--length];	
	}
	public Object getTop()
	{
		if(length == 0)
			return -1;
		else
		{
			int len = length-1;
			return stack[len];
		}
	}
	public void trans(int n)
	{
		//Scanner scan = new Scanner(System.in);
		//while(scan.hasNext())
		//{
			//int n = scan.nextInt();
			Stack a = new Stack(); 
			while(n != 0)
			{
				a.push(n % 2);
				n = n / 2;
			}
			while(a.isEmpty() == false)
			{	
				System.out.print(a.pop());
			}
				System.out.println();
		//}
	}	
}

 

Demo6

标签:empty   scanner   []   system.in   highlight   while   --   new   exti   

原文地址:http://www.cnblogs.com/cwenliu/p/6003974.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!