2016-04-24 16 views
3

我已查找並找不到要查找的信息。我的代碼正如我所期望的那樣工作,但我有一點我想要改進的代碼。使用void方法格式化java文本

的問題是,我不能像這樣的打印語句中調用一個void方法:

System.out.print("Water is a " + printTemp(temperature) + " at" + temperature + " degrees."; 

printTemp(溫度是一個void方法,以便這不會工作,結果我發現了一個工作-around但它並不理想:

System.out.print("\nWater is a "); 
printTemp(temperature); 
System.out.print(" at"); 
System.out.printf(" %.0f", temperature); 
System.out.print(" degrees.\n"); 

這裏是全碼:

import java.util.Scanner; 
public class printTemp { 

public static void main(String[]args) { 

Scanner input = new Scanner(System.in); 
System.out.print("Please enter the temperature: "); 
Double temperature = input.nextDouble(); 
// takes the state of the water from the printTemp method and 
// the temperature to return a formatted output to the user 
System.out.print("\nWater is a "); 
printTemp(temperature); 
System.out.print(" at"); 
System.out.printf(" %.0f", temperature); 
System.out.print(" degrees.\n"); 

} 

public static void printTemp(double temperature) { 
    String returnMessage = "null" ; 

    if (temperature < 32) 
     returnMessage = "solid"; 
    else if (temperature > 212) 
     returnMessage = "gas"; 
    else 
     returnMessage = "liquid"; 
    System.out.printf(returnMessage); 
} 

}

這是學校因此有條件必須保持,printTemp必須是一個無效的方法和變量溫度必須保持一個DOUBLE。

+0

沒有什麼你可以用這些約束做。那麼,除了將3種打印語句合併爲一個:'printf(「at%.0f degrees。%n」,temperature);' – Andreas

+2

我真的不喜歡這樣的問題:「如果沒有我的學校作業,我該如何做X使用推薦和最乾淨的方式來做到這一點?「 – Dici

+0

我不喜歡在論壇上發帖,但我不確定是否有更好的方法。感謝您幫助我學習! –

回答

2

怎麼樣把下面的代碼片段

... 
System.out.print("Water is a " + returnMessage + " at" + temperature + " degrees."); 

printTemp方法的最後一行?然後在main輸出什麼,你只是寫:

... 
printTemp(input.nextDouble()); 
+1

謝謝我試圖做到這一點,但聲明是在主要的方法,因此Java不能使用'returnMessage'。 –

+0

只有我不得不這樣做,所以雙打印爲int: ... System.out.print(「Water is a」+ returnMessage); System.out.printf(「at%.0f degrees。%n」,temperature); –

0

使用類變量來保存溫度串詞,並使用無效的方法來設置它。

public class PrintTemp { 

    private static String TempStr = ""; 

    public static void main(String[]args) { 
     [...] 
     printTemp(temperature); 
     System.out.print("Water is a " + TempStr + " at" + temperature + " degrees."); 
    } 

    public static void printTemp(double temperature) { 
     if (temperature < 32) 
      TempStr = "solid"; 
     else if (temperature > 212) 
      TempStr = "gas"; 
     else 
      TempStr = "liquid"; 
    } 
} 
+0

該類是小寫字母,變量是uppercased ...這是倒退 –

+2

你真的認爲創建一個實例字段來解決這個任務是最好的方法嗎? – Andrew

+1

這似乎很有趣,但爲了我的任務,我無法使用它。 –

0

UPDATE

public class PrintTemp { 

    public static void main(String[] args) { 
     double temperature = 35; 
     StringBuilder returnMessage = new StringBuilder(); 
     printTemp(temperature, returnMessage); 
     System.out.print("Water is a " + returnMessage + " at " + temperature + " degrees."); 
    } 

    public static void printTemp(double temperature, StringBuilder returnMessage) { 
     if (temperature < 32) 
      returnMessage.append("solid"); 
     else if (temperature > 212) 
      returnMessage.append("gas"); 
     else 
      returnMessage.append("liquid"); 
    } 
} 

這是有printTemp修改消息沒有實際它返回,並且不使用額外的類/豆最簡單的方法。 但是,這隻適用於對象(不是原始類型),對象之間不適用於字符串,因爲在Java中它們被視爲一種特殊情況。 (這就是爲什麼我不得不使用一個StringBuilder,StringBuffer也可以)

+0

我認爲它效果很好,但我相信它略高於我們目前的課程。 –

+0

請在獲得解決方案時發佈解決方案 – eddyce

0

對不起,推遲傢伙!

以下是解決方案。

import java.util.Scanner; 
public class printTemp { 
public static void main(String[]args) { 
//Read in the temperature from the user 
Scanner input = new Scanner(System.in); 
System.out.print("\nPlease enter the temperature: "); 
//Call and insert the input into the printTemp method. 
printTemp(input.nextDouble()); 
} 
public static void printTemp(double temperature) { 
    //Decide whether the water is in a soild, liquid or gaseous sate. 
    String returnMessage = "null"; 
    if (temperature < 32) 
     returnMessage = "solid"; 
    else if (temperature > 212) 
     returnMessage = "gas"; 
    else 
     returnMessage = "liquid"; 
    //Print to the user. 
    System.out.print("\nWater is a " + returnMessage); 
    System.out.printf(" at %.0f degrees.%n\n", temperature); 
} 

}