如何在 switch 语句中使用字符串
这里我尝试使用链表实现一个简单的队列。我这里使用了Bufferreader和readline。我将“选择”声明为字符串。但我无法将字符串变量传递给 switch 语句。如果我将其声明为整数变量,则 readline 方法将不会接受它。有人可以帮忙吗?
import java.lang.*;
import java.util.*;
import java.io.*;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
LinkedList l1=new LinkedList();
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the no of elements to be inserted: ");
String str;
str=bf.readLine();
System.out.println("Enter Your Choice: ");
System.out.println("1->Insert 2->Delete 3->Display 4->Exit");
String choice;
choice=bf.readLine();
for(;;){
switch(choice) {
case 1:l1.addLast(bf);
break;
case 2:l1.removeFirst();
break;
case 3:
System.out.println("The contents of Queue are :" +l1);
break;
default:break;
}
}
}
Here I'm trying to implement a simple queue using linked list. I used Bufferreader and readline here. I declared "choice" as string. But I can't pass a string variable to switch statement. If I declared it as Integer variable then readline method won't accept it. Can anyone help??
import java.lang.*;
import java.util.*;
import java.io.*;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
LinkedList l1=new LinkedList();
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the no of elements to be inserted: ");
String str;
str=bf.readLine();
System.out.println("Enter Your Choice: ");
System.out.println("1->Insert 2->Delete 3->Display 4->Exit");
String choice;
choice=bf.readLine();
for(;;){
switch(choice) {
case 1:l1.addLast(bf);
break;
case 2:l1.removeFirst();
break;
case 3:
System.out.println("The contents of Queue are :" +l1);
break;
default:break;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用 int choiceNum = Integer.parseInt(choice); 并打开它。
请注意,在 Java 7 中,您实际上可以打开字符串,但您需要
case "1":
。Use
int choiceNum = Integer.parseInt(choice);
and switch on that.Note that in Java 7 you actually can switch on Strings, but you would need to
case "1":
.好吧,另一个答案保留字符串:
Okay another answer keep the String:
如果它始终是一个字符输入,您可以将其转换为
char
并使用单引号打开它...If it's always going to be a one character input, you can convert it to
char
and switch on that using single quotes...在将字符串放入 switch 语句之前将其解析为整数:
Parse the String to an integer before putting it into ta switch statement:
如果它是一个 int,您可能需要使用 Integer.parseInt(),或使用 扫描仪 与 nextInt() 而不是 BufferedReader直接从文件中检索 int 。
If it is an int, you might want to parse your String to int with Integer.parseInt(), or use Scanner with nextInt() instead of BufferedReader to retrieve an int directly from your file.
试试这个
try this
我相信要使用 LinkedList,您需要在声明时指定数据类型。
这将创建数据类型字符串的链接列表。然后你可以使用parse函数将String转换为int。
I believe that to use a LinkedList you need to specify the data type while declaring.
This will create a linked list of data type Strings. Then you can use parse function to convert String into int.