2013-10-03 53 views
0

這是一個相當基本的問題,但作爲一個新的Java學生它難倒我。這是我正在研究的一個實踐問題,無法弄清楚如何將hotOrColdOutside方法調用到main中。截至目前,它不是編譯和要求返回,但我的指令指定在這種情況下沒有參數或返回。我覺得這是我知道的簡單事情,但此時此刻正在探索我的頭腦,任何幫助我糾正這一情況都將值得讚賞。調用方法,其中包含if/else

import java.util.Scanner; 

class TempExp 
{ 
    public static void main(String[] args) 
    { 
     Scanner input = new Scanner(System.in); 
     String answer = outsideTemp(); 
     System.out.print(answer); 
    }//end main 
    public static String outsideTemp() 
    { 
     Scanner input = new Scanner(System.in); 
     System.out.print("What is the temperature outside: "); 
     int userIn = input.nextInt(); 
     if(userIn >= 80) 
     { 
      System.out.print("It is very hot outside."); 
     } 
     else if(userIn >= 60) 
     { 
      System.out.print("It is very nice outside."); 
     } 
     else 
     { 
      System.out.print("It is very cold outside."); 
     }//end if/else   
    }//end method 
} 

回答

0

功能

public static String outsideTemp() 

返回String。但主函數不返回任何內容。

你會想是這樣的:

import java.util.Scanner; 

class TempExp 
{ 
    public static void main(String[] args) 
    { 
     Scanner input = new Scanner(System.in); 
     String answer = outsideTemp(); 
     System.out.print(answer); 
    }//end main 

    public static String outsideTemp() 
    { 
     Scanner input = new Scanner(System.in); 
     System.out.print("What is the temperature outside: "); 
     int userIn = input.nextInt(); 
     if(userIn >= 80) 
     { 
      return "It is very hot outside."; 
     } 
     else if(userIn >= 60) 
     { 
      return "It is very nice outside."; 
     } 
     else 
     { 
      return "It is very cold outside."; 
     }//end if/else   
    }//end method 
} 
0

因爲你說你的指令(我猜你指的是分配?)說沒有參數或返回,您需要更換String部分的功能定義outsideTemp()void。由於它不會返回任何內容,因此您的main函數也需要進行更改,以免期望返回任何內容。

public static void main(String[] args) 
{ 
    outsideTemp(); 
}//end main 

public static void outsideTemp() 
{ 
    ... // this all stays exactly how it is now 
} 

但是,如果你的意思是返回一個字符串,用約翰的回答

相關問題