如何在 switch 语句中使用字符串

发布于 2024-12-22 16:49:29 字数 1038 浏览 5 评论 0原文

这里我尝试使用链表实现一个简单的队列。我这里使用了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

谁与争疯 2024-12-29 16:49:29

使用 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":.

梦纸 2024-12-29 16:49:29

好吧,另一个答案保留字符串:

if (choice.equals("1")) {
    ...
} else if (choice.equals("2")) {

Okay another answer keep the String:

if (choice.equals("1")) {
    ...
} else if (choice.equals("2")) {
娇纵 2024-12-29 16:49:29

如果它始终是一个字符输入,您可以将其转换为 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...

帅的被狗咬 2024-12-29 16:49:29

在将字符串放入 switch 语句之前将其解析为整数:

int choice;
choice = Integer.parseInt(bf.readLine());

Parse the String to an integer before putting it into ta switch statement:

int choice;
choice = Integer.parseInt(bf.readLine());
耳钉梦 2024-12-29 16:49:29

如果它是一个 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.

简单爱 2024-12-29 16:49:29

试试这个

choice = Integer.parseInt(bf.readLine());

try this

choice = Integer.parseInt(bf.readLine());

赏烟花じ飞满天 2024-12-29 16:49:29

我相信要使用 LinkedList,您需要在声明时指定数据类型。

LinkedList<String> l1 = new LinkedList<String>();

这将创建数据类型字符串的链接列表。然后你可以使用parse函数将String转换为int。

int choice;
choice = Integer.parseInt(bf.readLine());

I believe that to use a LinkedList you need to specify the data type while declaring.

LinkedList<String> l1 = new LinkedList<String>();

This will create a linked list of data type Strings. Then you can use parse function to convert String into int.

int choice;
choice = Integer.parseInt(bf.readLine());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文