请帮忙。
class stak{
int tos=-1;
int size;
int a[];
stak(int l){
size=l;
tos=-1;
a=new int[l];
}
void push(int x){
if(tos==size-1){
System.out.println("Stack Overflow");
}
else{
a[++tos]=x;
}
}
void pop(){
if(tos<0){
System.out.println("Stack Underflow");
}
else{
System.out.println(a[tos--]);
}
}
void display(){
if(tos>=0){
for(int i=tos;i>=0;--i){
System.out.println(a[i]);
}
}
else
System.out.println("Stack is Empty");
}
}
public class stack{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("size of stack?");
int n=sc.nextInt();
stak s1=new stak(n);
String z;
do{
System.out.println("1.PUSH");
System.out.println("2.POP");
System.out.println("3.Display");
int ch=sc.nextInt();
switch(ch){
case 1 : System.out.println("number for push?");
int p=sc.nextInt();
s1.push(p);
break;
case 2 : System.out.println("POP!");
s1.pop();
break;
case 3 : System.out.println("Display!");
s1.display();
break;
default: System.out.println(" OOPS! owner says only take input as: 1,2,3");
break;
}
System.out.println("continue?");
z=sc.nextLine();
}while(z.equals("yes")|| z.equals("y"));
sc.close();
}
}
[1]: https://i.stack.imgur.com/BHZQO.png
问题出在扫描器类上。因为当您使用callz=sc.nextline();
时,这个调用首先从输入数字的行中获取输入,然后将其推入堆栈,因为int p=sc.nextint();
这一行只获取一个整数,但没有完成整行。
因此,在输入yes或No之前,您可以通过添加sc.nextline()
来手动完成这一行。通过添加额外的nextLine()将在inter整数时完成当前行。
import java.util.Scanner;
class stak{
int tos=-1;
int size;
int a[];
stak(int l){
size=l;
tos=-1;
a=new int[l];
}
void push(int x){
if(tos==size-1){
System.out.println("Stack Overflow");
}
else{
a[++tos]=x;
}
}
void pop(){
if(tos<0){
System.out.println("Stack Underflow");
}
else{
System.out.println(a[tos--]);
}
}
void display(){
if(tos>=0){
for(int i=tos;i>=0;--i){
System.out.println(a[i]);
}
}
else
System.out.println("Stack is Empty");
}
}
public class stack{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("size of stack?");
int n=sc.nextInt();
stak s1=new stak(n);
String z;
do{
System.out.println("1.PUSH");
System.out.println("2.POP");
System.out.println("3.Display");
int ch=sc.nextInt();
switch(ch){
case 1 : System.out.println("number for push?");
int p=sc.nextInt();
s1.push(p);
break;
case 2 : System.out.println("POP!");
s1.pop();
break;
case 3 : System.out.println("Display!");
s1.display();
break;
default: System.out.println(" OOPS! owner says only take input as: 1,2,3");
break;
}
System.out.println("continue?");
sc.nextLine();
z=sc.nextLine();
}while(z.equals("yes")|| z.equals("y"));
sc.close();
}
}
希望这对你有帮助。另外,您可以使用BufferReader来解决这个问题
与while循环顶部测试循环条件的for和while循环不同, do...while循环do...while循环底部检查其条件。 do...while循环类似于while循环,除了do ... while循环保证至少执行一次。 语法 (Syntax) Perl中do...while循环的语法是 - do { statement(s); }while( condition ); 应该注意的是
与while循环顶部测试循环条件的for和while循环不同,Objective-C编程语言中的do...while循环检查循环底部的条件。 do...while循环类似于while循环,除了do ... while循环保证至少执行一次。 语法 (Syntax) Objective-C编程语言中do...while循环的语法是 - do { statement(s); } while( co
Pascal中的while-do循环语句允许重复计算,直到满足某些测试条件。 换句话说,只要给定条件为真,它就会重复执行目标语句。 语法 (Syntax) while-do循环的语法是 - while (condition) do S; 其中, condition是布尔值或关系表达式,其值为true或false, S是BEGIN ... END块中的简单语句或语句组。 例如, while num
do-while语句用于模拟其他编程语言中存在的简单while循环。 语法 (Syntax) do-while语句的语法如下 - do while (condition) statement #1 statement #2 ... end 通过首先计算条件表达式(布尔值)来执行while语句,如果结果为true,则执行while循环中的语句。 从while语句中的条件
与while循环顶部测试循环条件的while循环不同, do-while循环do-while循环底部检查其条件。 do-while循环类似于while循环,除了do-while循环保证至少执行一次。 语法 (Syntax) 以下是do-while循环的语法。 do { statement(s); } while( condition ); 请注意,条件表达式出现在循环的末尾,因此循环中的
A Do…While当我们想要重复一组语句时使用Do…While循环,只要条件为真。 可以在循环开始时或循环结束时检查条件。 语法 (Syntax) 以下是VBA中Do…While循环的语法。 Do While condition [statement 1] [statement 2] ... [statement n] [Exit Do] [statement
do…while循环类似于while循环,只是do...while循环不会在第一次循环执行时评估条件。 但是,将对后续迭代评估条件。 换句话说,代码块将在do…while loop中至少执行一次。 流程图 以下是while循环的语法。 do { Statement(s) to be executed; } while (expression); Note - 不要错过do .
与while循环顶部测试循环条件的for和while循环不同, do...while循环do...while循环的底部检查其条件。 do ... while循环类似于while循环,除了do ... while循环保证至少执行一次。 语法 (Syntax) do { code_to_execute } while (Boolean_condition); 流程图 (Flow Diagram) 例