简体   繁体   English

Java 中的 Else-if 语句不起作用,我做错了什么?

[英]Else-if statement in Java is not working, what am I doing wrong?

So I'm trying to do a simple code with an if-else-if statement, but it isn't working.所以我试图用 if-else-if 语句做一个简单的代码,但它不起作用。 I have only done if-else before and I am getting confused.我之前只做过 if-else 并且我很困惑。 Here is the code.这是代码。 Why isn't it accepting my else-if?为什么它不接受我的 else-if?

import java.util.Scanner;

public class DaysOfWeek {
public static void main (String[]args) {
    Double day;
    System.out.print("Enter the day: ");
    Scanner sc = new Scanner(System.in);
    day = sc.nextDouble();
    if (day.equals("Monday"));
    {
        System.out.println("Sounds like someone has a case of the Mondays!");
    }
    else if (day.equals("Wednesday"));
    {
        System.out.println("It's hump day! El ombligo!");
    }
    else if (day.equals("Friday"));
    {
        System.out.println("Finally. It's Friday!");
    }
    else {System.out.println("It's another day of the week.");}
}}
  1. You want String day and sc.nextLine() if you want any comparison to work as written如果您希望任何比较按书面方式工作,您需要String daysc.nextLine()

  2. if and else if lines should not use ; if 和 else if 行不应该使用; - Easier to see if you write like - 更容易查看您是否喜欢写

     if (condition) { }

Thank-you everyone for quickly pointing out my semicolons at the end of my if-else-if lines.谢谢大家快速指出我的 if-else-if 行末尾的分号。 Also, thank-you for the catch on the string as opposed to double, I knew this part but had overlooked it because I was too frustrated with my confusion of the if-else-if lines.另外,感谢您抓住字符串而不是双重,我知道这部分但忽略了它,因为我对 if-else-if 行的混淆感到太沮丧。 Once I corrected the two issues it worked exactly how I'd intended.一旦我纠正了这两个问题,它就完全符合我的预期。

String day and sc.nextLine() replaced Double in order to make a proper comparison. String day 和 sc.nextLine() 替换了 Double 以进行适当的比较。

; ; was removed from the ends of my if-else-if lines.已从我的 if-else-if 行的末尾删除。

I also added a little bit so that it would take various options for days entered我还添加了一点,以便输入的天数需要各种选项

    import java.util.Scanner;

    public class DaysOfWeek{
    public static void main (String[]args) {
    String day;
    System.out.print("Enter the day: ");
    Scanner sc = new Scanner(System.in);
    day = sc.nextLine();
    if (day.equals("Monday")||day.equals("M")||day.equals("Mon.")||day.equals("Mon"))

    {
        System.out.println("Sounds like someone has a case of the Mondays!");
    }
    else if (day.equals("Wednesday")||day.equals("W")||day.equals("Wed.")||day.equals("Wed"))
    {
        System.out.println("It's hump day! El ombligo!");
    }
    else if (day.equals("Friday")||day.equals("F")||day.equals("Fri.")||day.equals("Fri"))
    {
        System.out.println("Finally. It's Friday!");
    }
    else {System.out.println("It's another day of the week.");}
}}

It has nothing to do with the else-if.它与 else-if 无关。 You obtain a double input ( sc.nextDouble() ) and compare it with String afterwards.您获得一个双输入( sc.nextDouble() ),然后将其与 String 进行比较。 This will never be true, hence the else will always be called这永远不会是真的,因此else将始终被调用

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

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