使用void方法格式化java文本

使用void方法格式化java文本,java,Java,我已经找过了,找不到我要找的信息。我的代码按预期运行,但我还有一点代码需要改进 问题是我不能在print语句中调用void方法,如下所示: System.out.print("Water is a " + printTemp(temperature) + " at" + temperature + " degrees."; printTemp(温度是一种无效方法,因此无法使用,因此我找到了一种解决方法,但并不理想: System.out.print("\nWater is a "); pri

我已经找过了,找不到我要找的信息。我的代码按预期运行,但我还有一点代码需要改进

问题是我不能在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);
}
import java.util.Scanner;
公共类printTemp{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
系统输出打印(“请输入温度:”);
双温=输入。nextDouble();
//从printTemp方法获取水的状态,然后
//将格式化输出返回给用户的温度
System.out.print(“\n水是a”);
打印温度;
系统输出打印(“at”);
系统输出打印F(“%.0f”,温度);
系统输出打印(“度”。\n”);
}
公共静态无效打印温度(双温){
字符串returnMessage=“null”;
如果(温度<32)
returnMessage=“solid”;
否则,如果(温度>212)
returnMessage=“气体”;
其他的
returnMessage=“液体”;
System.out.printf(返回消息);
}
}


这是针对学校的,因此必须保留一些条件,printTemp必须是一个void方法,变量temp必须保持为DOUBLE。

下面的代码片段怎么样

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

...
printTemp(input.nextDouble());

使用类变量保存温度字符串字,并使用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";
    }
}
公共类PrintTemp{
私有静态字符串TempStr=“”;
公共静态void main(字符串[]args){
[...]
打印温度;
系统输出打印(“水在“+温度+”度下为“+温度+”);
}
公共静态无效打印温度(双温){
如果(温度<32)
TempStr=“固体”;
否则,如果(温度>212)
TempStr=“气体”;
其他的
TempStr=“液体”;
}
}

更新

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{
公共静态void main(字符串[]args){
双温=35;
StringBuilder returnMessage=新建StringBuilder();
printTemp(温度、返回信息);
系统输出打印(“水是“+温度+”度下的“+返回信息+”);
}
公共静态无效打印温度(双温,StringBuilder返回消息){
如果(温度<32)
returnMessage.append(“固体”);
否则,如果(温度>212)
returnMessage.append(“气体”);
其他的
returnMessage.append(“液体”);
}
}
这是让printTemp修改消息的最简单方法,而无需实际返回消息,也无需使用其他类/bean。
但这只适用于对象(而不是基本类型),在对象中它不适用于字符串,因为在Java中它们被视为特例(这就是为什么我必须使用StringBuilder,StringBuffer也可以工作)

很抱歉耽搁了大家

这是解决办法

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);
}
import java.util.Scanner;
公共类printTemp{
公共静态void main(字符串[]args){
//从用户处读取温度
扫描仪输入=新扫描仪(System.in);
系统输出打印(“\n请输入温度:”);
//调用并将输入插入printTemp方法。
printTemp(input.nextDouble());
}
公共静态无效打印温度(双温){
//确定水是固态、液态还是气态。
字符串returnMessage=“null”;
如果(温度<32)
returnMessage=“solid”;
否则,如果(温度>212)
returnMessage=“气体”;
其他的
returnMessage=“液体”;
//打印给用户。
System.out.print(“\n水是”+返回消息);
System.out.printf(“温度为%.0f度。%n\n”);
}

}

你对这些限制无能为力。好吧,除了将3条打印语句合并成一条:
printf(“温度%.0f度.%n”);
我真的不喜欢这种问题:“如果不使用推荐的最干净的方式,我怎么能为我的学校作业做X?”一般来说,我不喜欢在论坛上发帖,但我不确定是否有更好的方法。谢谢你帮助我学习!旁注:这不是论坛;)类是小写的,变量是大写的。。。这就是Backwards你真的认为创建一个实例字段来解决该任务是最好的方法吗?这似乎很有趣,但就我的作业而言,我不能使用它。@AndrewTobilko有没有解决这个高度复杂的任务的最好方法^^^只是想添加其他可能与OPs任务一起工作的方法描述。谢谢,我试过这么做,但是语句是在main方法中,因此java不能使用“returnMessage”。只是我必须这样做,所以双精度打印为int:。。。系统输出打印(“水是一个”+返回消息);System.out.printf(“温度为%.0f.%n”);我认为它工作得很好,但我相信它比我们目前的电流略高。请在收到解决方案后发布