寻找每个月天数的程序

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

Finding days in each month program

问题

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int numberOfDays = 0;

        System.out.print("What's the month? ");
        String month = scan.nextLine();
        System.out.print("Is it a leap year? ");
        String leapYear = scan.nextLine();

        boolean isLeapYear = leapYear.equalsIgnoreCase("yes");

        switch (month) {
            case "January":
                numberOfDays = 31;
                break;
            case "February":
                if (isLeapYear) {
                    numberOfDays = 29;
                } else {
                    numberOfDays = 28;
                }
                break;
        }
    }
}
英文:

i'm a beginner at java and i'm trying to create a program that asks a user to enter a month, and prints the number of days in that month, as well as asking if it is a leap year. I've tried converting a String "yes" input from the scanner, into a boolean true or false input, however, i keep receiving the error cannot convert string into boolean. I've tried the parseString method however with no success. Any insight or help into this issue is greatly appreciated. Thanks for your time!

public static void main(String[] args){
	Scanner scan = new Scanner(System.in);
	int numberOfDays = 0;

	System.out.print("What's the month? ");
	String month = scan.nextLine();
	System.out.print("Is it a leap year? ");
	String leapYear = scan.nextLine();
	
	boolean b1 = Boolean.parseBoolean(leapYear);
	if (b1 = "yes") {
		b1 = true;
	}
	else {
		b1 = false;
	}
	
	switch(month) {
		case "January": 				
			numberOfDays = 31;
			break;				
		case "February":
			if (b1 = true) {
				numberOfDays = 29;
			} else {
				numberOfDays = 28;
			}
		break;
	}		
}	

答案1

得分: 2

boolean b1 = false;
if (leapyear.equals("yes")) {
    b1 = true;
}

我相当确定 Boolean.parseBoolean(leapYear) 只能在输入 "true""false" 时才起作用

另外在表达式中使用 = 要小心

    if (b1 = "yes") {

可能会导致错误但是

    if (b1 == "yes") {

几乎更糟因为只有当 b1 指向完全相同的 "yes" 字符串对象时才会返回 true而这几乎是不可能的因此使用 .equals() 方法它会按预期进行比较
英文:
boolean b1 = false;
if (leapyear.equals("yes")) {
    b1 = true;
}

I'm pretty sure Boolean.parseBoolean(leapYear) would only work if you entered "true" or "false".

Also, be careful using = in an expression.

if (b1 = "yes") {

probably will give you an error, but

if (b1 == "yes") {

is almost worse because it will only return true if b1 points to the exact same "yes" string object which it almost never will. Hence the use of .equals() which does what you expect.

答案2

得分: 1

你想要类似这样的代码:

boolean b1 = "yes".equals(leapYear);

用 b1 替代 if else 代码块中的 yes。你不能将字符串与布尔值直接比较,比较时需要使用两个等号。一个等号会尝试将 "yes" 赋值给一个布尔值,这就是你出错的地方。

英文:

You want something like this:

boolean b1 = "yes".equals(leapYear);

Remove the if else block with b1 = yes. You cant compare an String to a boolean, for comparison you need two equal signs, one would try to assign yes to a boolean, that's where your error comes from.

答案3

得分: 1

请尝试以下代码。我已经标注了下面需要进行一些代码修正的部分。

public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int numberOfDays = 0;

    System.out.println("请输入月份:");
    String month = scan.nextLine();
    System.out.print("这是闰年吗?");
    String leapYear = scan.nextLine();

    boolean b1;
    if ("yes".equalsIgnoreCase(leapYear)) {
        b1 = true;
    }
    else {
        b1 = false;
    }

    switch(month) {
        case "一月":                 
            numberOfDays = 31;
            break;              
        case "二月":
            if (b1) {
                numberOfDays = 29;
            } else {
                numberOfDays = 28;
            }
        break;
    }
}
英文:

Try the below code.
some code correction needed I have marked the below.

public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int numberOfDays = 0;

    System.out.println("What's the month? ");
    String month = scan.nextLine();
    System.out.print("Is it a leap year? ");
    String leapYear = scan.nextLine();

    boolean b1;
    if ("yes".equalsIgnoreCase(leapYear)) {
        b1 = true;
    }
    else {
        b1 = false;
    }

    switch(month) {
        case "January":                 
            numberOfDays = 31;
            break;              
        case "February":
            if (b1) {
                numberOfDays = 29;
            } else {
                numberOfDays = 28;
            }
        break;
    }
}

答案4

得分: 1

你的代码无法编译,因为你把一个字符串赋值给了一个布尔变量。这是行不通的。尝试以下代码:

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int numberOfDays = 0;

    System.out.print("月份是什么? ");
    String month = scan.nextLine();
    System.out.print("这是闰年吗? ");
    String leapYear = scan.nextLine();

    boolean b1;
    if (leapYear.equalsIgnoreCase("是")) {
        b1 = true;
    } else {
        b1 = false;
    }

    switch (month) {
        case "一月":
            numberOfDays = 31;
            break;
        case "二月":
            if (b1 == true) {
                numberOfDays = 29;
            } else {
                numberOfDays = 28;
            }
            break;
    }
}

编辑:在Java中,单个=用于赋值,而双===)用于进行相等性测试。因此,在之前的代码中,b = true会将b1的值重置为true,而不考虑之前由你的“这是闰年吗”的问题设置的值,因此else部分永远不会被执行。

英文:

Your code is not compilable as you are assigning a String to boolean variable. That will not work. Try the below code

public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int numberOfDays = 0;

        System.out.print("What's the month? ");
        String month = scan.nextLine();
        System.out.print("Is it a leap year? ");
        String leapYear = scan.nextLine();

        boolean b1;
        if (leapYear.equalsIgnoreCase( "Yes")) {
            b1 = true;
        }
        else {
            b1 = false;
        }

        switch(month) {
            case "January":
                numberOfDays = 31;
                break;
            case "February":
                if (b1 == true) {
                    numberOfDays = 29;
                } else {
                    numberOfDays = 28;
                }
                break;
        }
    }

Edit: In Java single = is used for assignment and double = (==) is used for equality test. So in previous code the b = true reset the value of b1 to true irrespective of previous value set by your question of isLeapYear. and so else part is never executed.

huangapple
  • 本文由 发表于 2020年5月3日 13:37:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/61570057.html
匿名

发表评论

匿名网友

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

确定