2014-10-13 252 views
-2

所以即时编写一个程序,打印出根据使用参数设置的日期的季节。我的问题是,通过使用公共静态字符串与相应的return语句,程序不执行字符串不返回任何东西

public class Lab06 { 

    public static void main (String [] args) { 
    season(12, 15); 

    } 

    public static String season(int month, int day) { 
    if ((((month == 12) && (day >= 16))) || (((month <= 2) && (day <= 31))) 
     || (((month == 3) && (day <= 15)))) { 
     return "Winter"; 
    } 
    else if ((((month == 3) && (day >= 16)) || ((month == 4 || month == 5) && (day <= 31)) 
     || ((month == 6) && (day <= 15)))) { 
     return "Spring"; 
    } 
    else if ((((month == 6) && (day >= 16)) || ((month == 7 || month == 8) && (day <= 31)) 
     || ((month == 9) && (day <= 15)))) { 
     return "Summer"; 
    } 
    else { 
     return "Fall"; 
    } 
    } 

随着当前设置参数时打印出任何东西,它应该返回“落”,但是当我执行编程它什么都不做。

我也提前,并尝试使用公共静态无效与通讯员system.out.print声明只是为了检查我的问题是否在我的if语句中撒谎。

public class Lab06 { 

    public static void main (String [] args) { 
     season(12, 15); 

    } 

    public static void season(int month, int day) { 
     if ((((month == 12) && (day >= 16))) || (((month <= 2) && (day <= 31))) 
     || (((month == 3) && (day <= 15)))) { 
     return "Winter"; 
     } 
     else if ((((month == 3) && (day >= 16)) || ((month == 4 || month == 5) && (day <= 31)) 
     || ((month == 6) && (day <= 15)))) { 
     return "Spring"; 
     } 
     else if ((((month == 6) && (day >= 16)) || ((month == 7 || month == 8) && (day <= 31)) 
     || ((month == 9) && (day <= 15)))) { 
     return "Summer"; 
     } 
     else { 
     System.out.print("Fall"); 
     } 
    } 

通过这样做,由于预期我的程序确实工作,所以没有什么错我的if语句

对于这个任务,我需要提前

由于使用公共静态字符串!

+0

更改'季节(12,15);'到'System.out.print(季节(12,15));' – Bohemian

+0

哦哇这就是这么简单。谢谢你,希望它不会让我失望 – GPC

回答

1

调用Java中的一个方法,像

season(12, 15); 

原因绝对没有产出的方式。试试这个:

System.out.print(season(12, 15)); 
0

你有一个返回类型不匹配:

static void 
... 
return "Winter"; 

你定义一个static String,而不是返回一个字符串值。

然后,您可以打印出该值作为该功能的结果。

String season = season(12, 15); 
System.out.println(season); 

或只是简单地

System.out.println(season(12, 15)); 

也从第二个例子中,函数打印。这应该被调整为返回以将其传回给调用者。

1

你只返回了值,但没有做任何事情。

使用

System.out.print(season(12,15)); 
1

在Java中,我们调用方法一样 赛季(12,15); 而你的方法返回一个字符串值,但在你的情况下,要打印该值,这样你就可以做到这一点有两种ways-

首先approach-

String returnValue = season(12,15); 

System.out.println("return value" + returnValue); // By this approach you can use your store value later also. 

二approach-

System.out.println(season(12,15));