在 do-while 循环中输入不适用于 Scanner class
Input not working with Scanner class in the do-while loop
在 do-while 循环中,代码在第一种情况后不接受输入。它在获取输入后被终止并且不执行 do-while 循环 properly.what is wrong with z=sc.nextLine(){in the while loop}
或扫描仪 class ?
PS:我也尝试使用 Scanner sc=new Scanner("System.in");但代码仍然无效。
除了 parseInt() 还有其他选择吗?
即将输出:
堆栈的大小?
5个
1.PUSH
2.POP
3.Display
1个
推号?
19
继续?
继续后,代码终止
请帮忙。
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
扫描仪有问题 class。因为当你使用 call z=sc.nextLine();
时,这个调用首先从你输入数字的行中获取输入以压入堆栈,因为 int p=sc.nextInt();
这一行只取一个整数但不会完成整行。
因此您可以在输入是或否之前通过添加 sc.nextLine()
手动完成该行。通过添加额外的 nextLine() 将在您插入整数时完成当前行。
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
来解决这个问题
在 do-while 循环中,代码在第一种情况后不接受输入。它在获取输入后被终止并且不执行 do-while 循环 properly.what is wrong with z=sc.nextLine(){in the while loop} 或扫描仪 class ? PS:我也尝试使用 Scanner sc=new Scanner("System.in");但代码仍然无效。 除了 parseInt() 还有其他选择吗?
即将输出: 堆栈的大小? 5个 1.PUSH 2.POP 3.Display 1个 推号? 19 继续? 继续后,代码终止
请帮忙。
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
扫描仪有问题 class。因为当你使用 call z=sc.nextLine();
时,这个调用首先从你输入数字的行中获取输入以压入堆栈,因为 int p=sc.nextInt();
这一行只取一个整数但不会完成整行。
因此您可以在输入是或否之前通过添加 sc.nextLine()
手动完成该行。通过添加额外的 nextLine() 将在您插入整数时完成当前行。
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
来解决这个问题