关于java的一个程序,急求各位大虾帮助!!!运行不出来啊! 10
importjava.util.Scanner;interfaceIStack{publicvoidclear();publicbooleanisEmpty();publ...
import java.util.Scanner;
interface IStack{
public void clear();
public boolean isEmpty();
public int length();
public Object peek();
public void push(Object x)throws Exception;
public Object pop();
}
class SqStack implements IStack {
private Object[] stackElem;
private int top;
public SqStack(int maxSize){
top = 0;
stackElem = new Object[maxSize];
}
public void clear(){
top = 0;
}
public boolean isEmpty(){
return top == 0;
}
public int length(){
return top ;
}
public Object peek(){
if(!isEmpty())
return stackElem[top-1];
else
return null;
}
public void push(Object x)throws Exception{
if(top == stackElem.length)
throw new Exception("栈已满");
else
stackElem[top++] = x;
}
public Object pop(){
if(isEmpty())
return null;
else
return stackElem[--top];
}
public void display(){
for(int i = top - 1; i>= 0; i--)
System.out.print(stackElem[i].toString() +" ");
}
}
public class homework{
public static void main(String[] args){
System.out.println("请输入一个十进制数:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
SqStack s = new SqStack(100);
while(n!= 0)
{
int m =(n%2);
s.push(m);
n=n/2;
}
}
} 展开
interface IStack{
public void clear();
public boolean isEmpty();
public int length();
public Object peek();
public void push(Object x)throws Exception;
public Object pop();
}
class SqStack implements IStack {
private Object[] stackElem;
private int top;
public SqStack(int maxSize){
top = 0;
stackElem = new Object[maxSize];
}
public void clear(){
top = 0;
}
public boolean isEmpty(){
return top == 0;
}
public int length(){
return top ;
}
public Object peek(){
if(!isEmpty())
return stackElem[top-1];
else
return null;
}
public void push(Object x)throws Exception{
if(top == stackElem.length)
throw new Exception("栈已满");
else
stackElem[top++] = x;
}
public Object pop(){
if(isEmpty())
return null;
else
return stackElem[--top];
}
public void display(){
for(int i = top - 1; i>= 0; i--)
System.out.print(stackElem[i].toString() +" ");
}
}
public class homework{
public static void main(String[] args){
System.out.println("请输入一个十进制数:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
SqStack s = new SqStack(100);
while(n!= 0)
{
int m =(n%2);
s.push(m);
n=n/2;
}
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询