用JAVA写一个将华氏温度转换成摄氏温度的程序

程序的输入是一个整数,表示华氏温度。输出对应的摄氏温度,也是一个整数。提示,为了把计算结果的浮点数转换成整数,需要使用下面的表达式:(int)x;其中x是要转换的那个浮点数。
2026年09月22日 19:35
有3个网友回答
网友(1):

这样:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner in = new Scanner(System.in);

int C; 

int F = in.nextInt();

C = (F - 32)*5/9;

System.out.println(C);

in.close();

}

}

扩展资料:

注意事项

/*

 * 华氏温度和摄氏温度互相转换,从华氏度变成

摄氏度你只要减去32,乘以5再除以9就行了,将

摄氏度转成华氏度,直接乘以9,除以5,再加上

32即行。

 * */ 

package com.homework; 

import java.util.*; 

public class Demo2 {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println("请输入一个华氏温度:");

Scanner sc = new Scanner (System.in);

float a = sc.nextFloat();

float b;

b = (a-32) * 5 / 9;

System.out.println(b);

}

}

网友(2):

将华氏温度转换成摄氏温度的Java程序如下:

import java.util.Scanner;

public class Fahrenheit {

 public static void main (String []args) {

     Scanner input =new Scanner(System.in);

  System.out.print("Enter a degree in Fahrenehit: ");

  int Fahrenehit= input.nextInt();

     int Celsius= (int)((Fahrenehit-32)*(5.0/9));

     System.out.println(Fahrenehit + " Fahrenehit is "+ Celsius + " Celsius");

 }

}

运行结果:

Enter a degree in Fahrenehit: 50
50 Fahrenehit is 10 Celsius

网友(3):

int f;
int c;
Scanmer in= new Scanner(System.in);
f=in.nextInt();
c=(int)((5/9.0)*(f-32));
System.out.prinln(c);