2017-03-17 86 views
0

我試圖對第二個返回方法進行兩個調用,該方法需要一個參數(以英寸爲單位),並以釐米爲單位打印該值,但不工作!我做了什麼錯`Java方法代碼錯誤

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

    // prompt the user and get the value 
    System.out.print("Exactly how many inches are you? "); 
    int uHeight = in.nextInt(); 

    // converting and outputing the result 
    System.out.print("How many inches is your mother? "); 
    int mHeight = in.nextInt(); 
    InchesToCm(mHeight,uHeight); 
} 

public static double InchesToCm() { 


Scanner in = new Scanner(System.in); 
System.out.print("Exactly how many inches are you? "); 
int inHeight = in.nextInt(); 
double cmHeight = inHeight * 2.54; 
System.out.println("He's " + uHeight + " inches or " + mHeight + " cm."); 
return 
+1

請更準確地說,是如何它不工作? – Berger

+1

什麼問題,例外,錯誤?請用這些信息編輯問題。見[問]。那麼,我沒有看到'InchesToCm'方法中的返回值,但沒有右括號。那麼它是完成方法? – AxelH

回答

3

您應該通過您的mHeight和uHeight,在你的方法,因爲你叫你這樣InchesToCm(mHeight,uHeight);主要方法,所以你的方法應該是這樣的:?

public static double InchesToCm(int mHeight, int uHeight) {//pass your values in method 

其次,你必須在最後返回結果:

return cmHeight;//return the result 

注意

對於好的做法你的方法應該像@Timothy腳輪小寫字母開頭的評論說,你的類名稱應與上字母


編輯

啓動對於好的做法您必須使用:

public static void main(String[] args) { 
    Scanner in = new Scanner(System.in); 
    System.out.print("Exactly how many inches are you? "); 
    int uHeight = in.nextInt(); 

    System.out.println("He's " + uHeight + " inches or " + inchesToCm(uHeight) + " cm."); 
    System.out.print("How many inches is your mother? "); 

    int mHeight = in.nextInt(); 
    System.out.println("He's " + mHeight + " inches or " + inchesToCm(mHeight) + " cm."); 
    //------------------------------^^-------------------------------^^------------------ 
    in.close();//close your Scanner 
} 

//Your function should take inches and return cm 
public static double inchesToCm(int height) { 
    return height * 2.54; 
} 
+0

和'InchesToCm'應該以小寫字母和動詞開始:'convertInchesToCm' –

+0

我需要進行兩次調用第二個返回方法,該方法接受一個參數(我的值以英寸爲單位)並以釐米爲單位輸出值 –

+0

謝謝@TimothyTruckle我已經編輯它 –