在 java 中输入退出选择之前,我如何继续在 switch case 中进行选择
how do i keep taking choices in switch case until exit choice is entered in java
- 我想一直接受用户的选择,直到他给出退出声明。
- 如果在这段代码中使用while循环,如何退出?
- 除了 switch case 之外,是否还有其他方式让用户选择:
- 如果我尝试为此使用 while 循环,那么它将进入无限循环:
代码:
import java.util.Scanner;
class lib
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
String c;
int j=5,a=5,s=5,cg=5;
int d=0;
System.out.println("Available Copies :");
System.out.println("java=5,ada=5,sp=5,cg=5");
System.out.println("enter book title");
//System.out.println("enter exit for exit if don't want to search");
c=in.nextLine();
while(d==0)
{
switch(c)
{
case "java":
System.out.println("author is herbert");
System.out.println("price :500");
j--;
break;
case "ada":
System.out.println("author is corman");
System.out.println("price :600");
a--;
break;
case "sp":
System.out.println("author is dhamdhire");
System.out.println("price :550");
s--;
break;
case "cg":
System.out.println("author is pearson");
System.out.println("price :700");
cg--;
break;
case "exit":
d++;
break;
default:
System.out.println("book not available");
}
}
if(j!=0)
System.out.println("number of available copies is"+j);
}
我认为您正在寻找的是将 nextLine
移动到 while 循环中。
这部分代码:
//System.out.println("enter exit for exit if don't want to search");
c=in.nextLine();
while(d==0)
{
.....
为此:
//System.out.println("enter exit for exit if don't want to search");
while(d==0)
{
c=in.nextLine();
.....
如果您希望在用户发出退出命令之前一直接受用户的输入,那么您需要在 while 循环中不断接受输入。将 c=in.nextLine()
移动到 switch 语句之前的 while 循环中。
如果您还想提示用户,则在 switch 语句结束后立即在循环末尾添加 print 语句,而不是将 c=in.nextLine()
移动到 while 循环的末尾打印语句。类似于:
System.out.print("Enter the title of another book: ");
c=in.nextLine();
- 我想一直接受用户的选择,直到他给出退出声明。
- 如果在这段代码中使用while循环,如何退出?
- 除了 switch case 之外,是否还有其他方式让用户选择:
- 如果我尝试为此使用 while 循环,那么它将进入无限循环:
代码:
import java.util.Scanner;
class lib
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
String c;
int j=5,a=5,s=5,cg=5;
int d=0;
System.out.println("Available Copies :");
System.out.println("java=5,ada=5,sp=5,cg=5");
System.out.println("enter book title");
//System.out.println("enter exit for exit if don't want to search");
c=in.nextLine();
while(d==0)
{
switch(c)
{
case "java":
System.out.println("author is herbert");
System.out.println("price :500");
j--;
break;
case "ada":
System.out.println("author is corman");
System.out.println("price :600");
a--;
break;
case "sp":
System.out.println("author is dhamdhire");
System.out.println("price :550");
s--;
break;
case "cg":
System.out.println("author is pearson");
System.out.println("price :700");
cg--;
break;
case "exit":
d++;
break;
default:
System.out.println("book not available");
}
}
if(j!=0)
System.out.println("number of available copies is"+j);
}
我认为您正在寻找的是将 nextLine
移动到 while 循环中。
这部分代码:
//System.out.println("enter exit for exit if don't want to search");
c=in.nextLine();
while(d==0)
{
.....
为此:
//System.out.println("enter exit for exit if don't want to search");
while(d==0)
{
c=in.nextLine();
.....
如果您希望在用户发出退出命令之前一直接受用户的输入,那么您需要在 while 循环中不断接受输入。将 c=in.nextLine()
移动到 switch 语句之前的 while 循环中。
如果您还想提示用户,则在 switch 语句结束后立即在循环末尾添加 print 语句,而不是将 c=in.nextLine()
移动到 while 循环的末尾打印语句。类似于:
System.out.print("Enter the title of another book: ");
c=in.nextLine();