使用void方法格式化java文本

时间:2016-04-24 14:54:27

标签: java

我看起来无法找到我要找的信息。我的代码正在按照我的预期运行,但我想要改进一些代码。

问题是我不能在这样的print语句中调用void方法:

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

printTemp(温度是一种无效的方法,所以这不会起作用,因此我找到了解决办法,但这并不理想:

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必须是一个void方法,变量temp必须保持双倍。

4 个答案:

答案 0 :(得分:2)

如何放置以下代码段

resv

作为最后一行的... System.out.print("Water is a " + returnMessage + " at" + temperature + " degrees."); 方法?然后在printTemp输出中没有任何内容,你只需写:

main

答案 1 :(得分:0)

使用类变量来保存Temperature String字,并使用void方法设置它。

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";
    }
}

答案 2 :(得分:0)

<强>更新

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修改消息而不实际返回消息而不使用其他类/ bean的最简单方法。 但这只适用于对象(非原始类型)和对象之间它不适用于字符串,因为在Java中它们被视为一种特殊情况。 (这就是为什么我必须使用StringBuilder,StringBuffer也会工作)

答案 3 :(得分: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);
}

}