简体   繁体   English

温度计算代码Java

[英]Temperature Calculation Code Java

I am trying to create a program that will print out the temperature in Fahrenheit. 我正在尝试创建一个程序,以打印出华氏温度。 My goal is to not use any condition like the if statement or any loops. 我的目标是不使用任何条件,例如if语句或任何循环。

public class TemperatureConverter {
    public static void main(String[] args) { 
        double celsius, fahrenheit; 
        Scanner input = new Scanner(System.in); 

        System.out.print("Enter the temperature in degrees Celsius:  "); 
        fahrenheit = input.nextDouble(); 
        celsius = 5.0/9.0 * (fahrenheit - 32); 

        System.out.printf(+celsius, "degrees Celsius is" +fahrenheit "degrees Fahrenheit"); 
    }
}

I am getting an error which is: 我收到一个错误,它是:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
The method printf(String, Object...) in the type PrintStream is not applicable for the arguments (double, String)
Syntax error on token ""degrees Fahrenheit"", delete this token

at TemperatureConverter.main(TemperatureConverter.java:14)

Edit: 编辑:

I am not coming up with what I need to come up with. 我没有提出我需要提出的建议。 When I run this program the output is this: 当我运行该程序时,输出为:

For this example the user inputted 45 for the prompt of Celsius. 在此示例中,用户输入了45来提示摄氏。

Enter the temperature in degrees Celsius: 45 输入温度(摄氏度):45
42.77777777777778 degrees Celsius is 45.0 degrees Fahrenheit 42.77777777777778摄氏度是45.0华氏度

Well just to fix your error: 好吧,只是为了解决您的错误:

System.out.print(celsius + "degrees Celsius is " + fahrenheit + " degrees Fahrenheit");

or you could do this if you want to use printf . 或者如果您想使用printf可以这样做。

System.out.printf("%f degrees Celsius is %f degrees Fahrenheit",celsius,fahrenheit);

The reason I did all this is mainly just simple syntax. 我这样做的原因主要是简单的语法。 Strings are stuff within double quotes. 字符串是双引号内的东西。 "blah" for example is a String containing the word blah . 例如, "blah"是一个包含单词blah的字符串。

If you want to concatenate two strings then you use the + operator in Java. 如果要连接两个字符串,则可以在Java中使用+运算符。 So you want to add "foo" and "bar" you write "foo" + "bar" and you get "foobar" . 因此,要添加"foo""bar"请编写"foo" + "bar"然后得到"foobar"

In your attempt you have made two errors: 在尝试中,您犯了两个错误:

Firstly, (the intention of) string concatenation was done wrong. 首先,字符串连接(意图)做错了。 + celsius isn't a syntax error because celsius is a number but if it were a string then the compiler doesnt have anythign to the left of that + to concatenate the value of celsius with. + celsius不是语法错误,因为celsius是一个数字,但是如果它是一个字符串,则编译器在该+的左侧没有任何东西可以将celsius的值与之连接。 "" + celsius would have been fine if you wanted to prepend nothing. 如果您不准备加"" + celsius会很好。

+ celsius effectively does nothing since it is a number. + celsius实际上是无用的,因为它是一个数字。 Positive or negative wont matter. 积极或消极无关紧要。 -celsius of course flips the sign. -celsius当然会翻转符号。 If you want to display a positive number always (although your output would then be incorrect) you can use Math.abs(celsius). 如果您想始终显示一个正数(尽管输出将不正确),则可以使用Math.abs(celsius)。

Secondly, since you are using System.out.printf there is a specific format that you need to follow. 其次,由于您正在使用System.out.printf因此需要遵循一种特定的格式。 ie the first argument must be a string (possibly with things like %d, %f, %s, etc) which the function will replace with the subsequent arguments that you provide it. 也就是说,第一个参数必须是一个字符串(可能包含%d,%f,%s等),该字符串将被您提供的后续参数替换。

This is what my solution does, it uses %f (for floating point numbers) twice and I give it two things celsius and fahrenheit which are doubles and Java realizes that all is good and just replaces the values in there. 这就是我的解决方案所做的事情celsius它两次使用%f (用于浮点数),并且给了它们celsiusfahrenheit两种东西celsius它们都是双精度的,而Java意识到一切都很好,只是替换了其中的值。 You can use things like %.2f if you wanted two decimal places and such as well, this is the advantage of printf over a normal print or println (print + new line). 如果需要两个小数位,则可以使用%.2f类的东西,例如,这也是printf相对于普通print或println(print +换行)的优势。


To fix the real other problem is that you are taking in temprature in celsius but saving it to the variable fahrenheit. 要解决真正的另一个问题,就是您将温度设为摄氏温度,但将其保存到变量华氏度中。 If you just change that then all will be well. 如果您只是进行更改,那么一切都会很好。

System.out.print("Enter the temperature in degrees Fahrenheit:  "); 

To print output use: 要打印输出,请使用:

System.out.println(celsius + "degrees Celsius is" + fahrenheit + "degrees Fahrenheit");

or 要么

System.out.printf("%f degrees Celsius is %f degrees Fahrenheit",celsius,fahrenheit);

[EDIT] what went wrong. [编辑]出了什么问题。

java.lang.Error: Unresolved compilation problems: 
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
The method printf(String, Object...)
  • As the error says your syntax for printf() is wrong please refer link 由于错误表明您的printf()语法错误,请参考链接

尝试考虑以下打印声明:

 System.out.println(celsius+" is degrees Celsius and " +fahrenheit+ " is degrees Fahrenheit");

To start off, you are asking the user to enter the temperature in celcius and then assigning that value to a variable called fahrenheit. 首先,您要让用户以摄氏度输入温度,然后将该值分配给一个名为华氏温度的变量。 It doesn't seem to be very appropriate. 这似乎不太合适。 As some other have said you aren't constructing the String correctly. 正如其他人所说的那样,您没有正确构造String。

I have tidied up you code and changed the variable names as well as the formula to calculate the degrees in Fahrenheit. 我整理了您的代码,并更改了变量名称以及用于计算华氏度的公式。

    public static void main(String[] args) { 

        double celsius, fahrenheit; 
        Scanner input = new Scanner(System.in); 
        System.out.print("Enter the temperature in degrees Celsius:  "); 
        celsius = input.nextDouble(); 
        fahrenheit = celsius*9.0/5.0 + 32; 

        System.out.print(celsius + " degrees Celsius is " + fahrenheit+  " degrees Fahrenheit"); 
    }
    System.out.printf(+celsius, "degrees Celsius is" +fahrenheit "degrees Fahrenheit"); 

Check your original statement. You have missed adding one +(append) to add strings.

Use this 

   System.out.printf("%f degrees Celsius is %f degrees Fahrenheit",celsius,fahrenheit);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM