这里我试图用链表实现一个简单的队列。我在这里使用了Bufferreader和readline。我将“choice”声明为字符串。但是我无法传递一个字符串变量来切换语句。如果我将它声明为Integer变量,那么readline方法将不会接受它。谁能帮忙?如何在switch语句中使用字符串
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;
}
}
}
或者改变您切换情况下'“1”','案“2”'等等,因为选择的是String类型 – 2011-12-21 16:00:02
这是否编译呢? – home 2011-12-21 16:00:06
自从几个月前发布的Java 7以来,您可以在'switch'语句中使用'String'。如果您使用Java 6或更早版本,则不能在'switch'语句中使用'String'。 – Jesper 2011-12-22 12:04:08