简体   繁体   English

在 switch 语句中使用扫描仪,但我的程序只保留打印默认值

[英]Using scanner in a switch statement but my program only keeps printing the default

I am a college freshman who's studying programming, so apparently, I'm new to this, but I'm already at the part of switch statements.我是一名正在学习编程的大学新生,显然,我对此并不陌生,但我已经参与了 switch 语句。

The goal of my program is to be like a dictionary.我的程序的目标是像一本字典。 When user enters the term, the program should enter its meaning.当用户输入术语时,程序应输入其含义。 But my program keeps printing the default .但我的程序一直打印default What do you think am I missing?你觉得我错过了什么? Here's my code:这是我的代码:

        Scanner scn = new Scanner(System.in);
            
        System.out.print("Enter the term:");
        String term = scn.nextLine();
            
        switch(term) {
            case "Programming":
                System.out.println("It is the process of creating a set of instructions to tell the computer how to do a task.");
            break;
            case "Java":
            System.out.println("A popular high-level and object-oriented programming language developed by Sun Microsystems in 1995 and now, owned by Oracle.");
            break;
            case "print":
                System.out.println("A method in Java that is used to display a text on the console.");
            break;
            case "scanner":
            System.out.println("It is used to get an input data from the user, and found in the java.util package.");
            break;
            case "packages":
                System.out.println("It organizes Java classes into namespaces, providing a unique namespace for each type it contains.");
            break;
            case "IDE":
                System.out.println("also known as Integrated Development Environment, a software app that enables us to write & debug programs more easily.");
            break;
            case "switch":
                System.out.println("Unlike conditionals, these statements only checks equality and only works strings, char, int and enums.");
            break;
            case "function":
                System.out.println("A block of organized, reusable code that is used to perform a single, related action.");
            break;
            case "Conditional Statements":
                System.out.println("It allows a program to take action based on the given condition.");
            break;
            case "IF-statements":
                System.out.println("It is one of the conditional statement that handles one conditional expression. It either does SOMETHING or NOTHING.");
            break;
            default: 
                System.out.println("Sorry, we don't have any information about that.");
                 }
                 
        scn.close();

Sorry, if it's a lengthy program, I want to make it ten items as possible.对不起,如果这是一个冗长的程序,我想尽可能地把它做成十个项目。 I also forgot to tell you that I tried once to remove the Scanner and only declare the variables, the program worked (just for you to know that my program also works and print strings).我也忘了告诉你,我曾尝试删除Scanner并只声明变量,程序工作(只是为了让你知道我的程序也工作并打印字符串)。

Anyway, for those who will respond, thank you very much in advance!无论如何,对于那些会回应的人,非常感谢您!

Posted is working correctly, at least for input Java see IDEone . Posted 工作正常,至少对于输入Java参见IDEone
Most probably the input is not correct - switch needs exact same text: case is important, spaces are important, ...很可能输入不正确 - switch需要完全相同的文本:大小写很重要,空格很重要,...

Suggestion: include the input in the error message:建议:在错误消息中包含输入:

default: 
    System.out.println("Sorry, we don't have any information about '" 
                       + term + "'");

so the message can help user (and developer) see what went wrong (even/specially true in production code).因此该消息可以帮助用户(和开发人员)查看出了什么问题(甚至/特别是在生产代码中)。

As commented, for this user case, it is better to ignore case like in:正如评论的那样,对于这个用户案例,最好忽略以下情况:

switch (term.toLowerCase()) {
    case "programming":   \\ the labels must all be lowercase too
        ...
    case "java":
        ...

Incase there is space at the end of your input, you can trim that term.trim()如果输入末尾有空格,则可以修剪该term.trim()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM