简体   繁体   English

Java If Else语句的缩写

[英]Java If Else statement for abbreviations

I've been working on this code, its really basic but I just started working with Java and I don't know whats going on with it. 我一直在研究这段代码,它的确很基础,但是我刚开始使用Java,但我不知道它发生了什么。

import java.util.Scanner;


public class Proj {


public static void main(String[] args) {

    //Create Scanner
    Scanner scanner = new Scanner(System.in);

    //Input Abbreviation
    System.err.print("Input METAR Abbreviation: "); 
    scanner.nextLine( );

    //Abbreviations
    String abb = "";
    if (abb.equals("B"))  {
        System.out.println ("Began At Time ");
    }
    else if(abb.equals("+")) {
        System.out.println ("Heavy intensity");
    }
    else if(abb.equals("-")) {
        System.out.println ("Light intensity");
    }
    else if(abb.equals("DZ")) {
        System.out.println ("Drizzle");
    }
    else if(abb.equals("E")) {
        System.out.println ("Ended At Time");
    }
    else if(abb.equals("HZ")) {
        System.out.println ("Haze");
    }
    else if(abb.equals("RA")) {
        System.out.println ("Rain");
    }
    else if(abb.equals("SN")) {
        System.out.println ("Snow");
    }
    else(abb !=NULL) {
        System.out.println("Unknown Abbreviation");
    }
    //Close scanner
    scanner.close( );
    }
}

So what I'm trying to do is when someone enters one of the abbreviations it prints out what it means, and if the abbreviation doesn't exist it prints out Unknown Abbreviation. 所以我想做的是,当有人输入缩写词时,它会打印出含义,如果缩写词不存在,则会打印出未知缩写词。 So my problem is that when I run the code it prints Input METAR Abbreviation and you can enter a string but after that nothing happens, can someone help me out. 所以我的问题是,当我运行代码时,它会打印出输入METAR缩写,您可以输入一个字符串,但是在此之后什么都没有发生,有人可以帮帮我。 Thanks 谢谢

You call scanner.nextLine() but never actually capture or use its result. 您调用scanner.nextLine()但实际上从未捕获或使用其结果。 Then, you assign String abb= "" . 然后,您分配String abb= "" This means abb will always be "" . 这意味着abb将始终为"" That doesn't match any of your if conditions. 这与您的任何if条件都不匹配。

If you do String abb = scanner.nextLine( ); 如果您执行String abb = scanner.nextLine( ); , you'll get what you want: the scanner will read the next line, and assign that value to abb . ,您将获得所需的内容:扫描程序将读取下一行,并将该值分配给abb

Incidentally, I doubt this code compiles; 顺便说一句,我怀疑这段代码是否可以编译。 NULL isn't a keyword in Java (Java is case-sensitive), and the else needs an if after it. NULL不是Java中的关键字(Java区分大小写), else if在其后需要if But you also don't need to check for null , because scanner.nextLine() never returns it (if there's no next line, it just throws NoSuchElementException ). 但是您也不需要检查null ,因为scanner.nextLine()永远不会返回它(如果没有下一行,它只会抛出NoSuchElementException )。 So the last bit should just be a plain else { . 所以最后一点应该只是一个普通的else {

There are some problems: 有一些问题:

  • You should assign the scanner.nextLine() result to a variable. 您应该将scanner.nextLine()结果分配给一个变量。 In this case, I think you must assign it to abb : 在这种情况下,我认为您必须将其分配给abb

    String abb = scanner.nextLine(); 字符串abb = Scanner.nextLine();

  • Also, you forgot the if in the last condition: 此外,您还忘了最后一个条件中的if

     else if (abb != null) { System.out.println("Unknown Abbreviation"); } 
  • And don't forget that Java is case-sensitive, so it must be null , not NULL . 并且不要忘记Java是区分大小写的,因此它必须为null ,而不是NULL

May I sugest you use a switch statement, switch is much easier in your case instead of nested if, you can use 我可以建议您使用switch语句,在这种情况下,使用switch比嵌套更容易,如果可以的话

switch(condition){
case something:
//do stuff
}

For example 例如

    switch(abb){
case "hello":
System.out.println("hi there")
break;
case "else string":
//do something 
break;
}

Good luck! 祝好运! Happy to help 乐意效劳

您正在从Scanner中读取该行,但没有将其存储在任何地方,请按以下步骤进行更改:

String abb = scanner.nextLine( );

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

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