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

huangapple go评论70阅读模式
英文:

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

问题

以下是您提供的代码的翻译部分:

import java.util.Scanner;

public class DaysOfWeek {
    public static void main(String[] args) {
        String day;
        System.out.print("输入星期几:");
        Scanner sc = new Scanner(System.in);
        day = sc.next();
        if (day.equals("星期一")) {
            System.out.println("听起来有人有一种星期一的感觉!");
        } else if (day.equals("星期三")) {
            System.out.println("已经是周三了!肚脐!");
        } else if (day.equals("星期五")) {
            System.out.println("终于,星期五了!");
        } else {
            System.out.println("这是一周中的另一天。");
        }
    }
}
英文:

So I'm trying to do a simple code with an if-else-if statement, but it isn't working. I have only done if-else before and I am getting confused. Here is the code. Why isn't it accepting my 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

得分: 1

  1. 如果您希望任何比较按照编写的方式起作用,您需要使用 String daysc.nextLine()

  2. ifelse if 行不应使用 ; - 如果您这样编写更容易看清楚

if (condition) {

}
英文:
  1. You want String day and sc.nextLine() if you want any comparison to work as written

  2. if and else if lines should not use ; - Easier to see if you write like

     if (condition) {
    
     }
    

答案2

得分: 0

以下是您要翻译的内容:

感谢大家迅速指出我在 if-else-if 代码行末尾的分号。还要感谢大家注意到字符串与双精度的问题,我知道这一点,但由于我对 if-else-if 行的困惑感到非常沮丧,所以忽略了它。一旦我纠正了这两个问题,它就按照我预期的方式工作了。

为了进行正确比较,将 String day 和 sc.nextLine() 替换了 Double。

if-else-if 行末尾的分号被移除。

我还添加了一点内容,以便可以针对输入的不同日期选项进行处理。

import java.util.Scanner;

public class DaysOfWeek {
    public static void main(String[] args) {
        String day;
        System.out.print("请输入日期: ");
        Scanner sc = new Scanner(System.in);
        day = sc.nextLine();
        if (day.equals("星期一") || day.equals("一") || day.equals("周一") || day.equals("Mon"))
        {
            System.out.println("听起来有人被周一绊倒了!");
        }
        else if (day.equals("星期三") || day.equals("三") || day.equals("周三") || day.equals("Wed"))
        {
            System.out.println("已经是周三了,一周的中点!");
        }
        else if (day.equals("星期五") || day.equals("五") || day.equals("周五") || day.equals("Fri"))
        {
            System.out.println("终于,周五到了!");
        }
        else {
            System.out.println("这是一周中的另一天。");
        }
    }
}
英文:

Thank-you everyone for quickly pointing out my semicolons at the end of my if-else-if lines. 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. 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.

; was removed from the ends of my if-else-if lines.

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.");}
}}

huangapple
  • 本文由 发表于 2020年9月12日 01:18:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63851601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定