java - 使用 3 个函数将温度从摄氏度转换为华氏度

标签 java function class

我是java新手,我正在尝试编写一个将摄氏度转换为华氏度的程序

import java.util.Scanner;

public class Temps
{

   public static void main(String[] args) 
   {

      System.out.print("Enter temp(70 f or 20 c): ");
      double  temp = keyboard.nextDouble();
      String units = keyboard.next();
      if (units.equal("f"))
         newtemp =  ftoc(temp);
      else if (units.equals("c"))
         newtemp = ctof(temp);
      else System.err.println("units must be c or f");
   }

          public static double ftoc (int c)
          return (( 5.0 / 9.0) * (c - 32));
       }

       public static double ctof (int f)
       {
          return ((9.0/5.0)* f+32);
       }
    }

有人可以向我解释一下我做错了什么吗

最佳答案

问题不少

查看固定代码中的注释

    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter temp(70 f or 20 c): ");
      double  temp = keyboard.nextDouble();
      String units = keyboard.next();
      double newtemp = -1;   // not declared
      if (units.equals("f"))  // should be equals
         newtemp =  ftoc(temp);
      else if (units.equals("c"))
         newtemp = ctof(temp);
      else System.err.println("units must be c or f");

      System.out.println("the new temp is " + newtemp);  // need to print it out
   }

      public static double ftoc (double c) {   // take a double
          return (( 5.0 / 9.0) * (c - 32));
       }

       public static double ctof (double f)  // take a double
       {
          return ((9.0/5.0)* f+32);
       }

关于java - 使用 3 个函数将温度从摄氏度转换为华氏度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40647883/

相关文章:

java - PriorityBlockingQueue 什么时候对元素进行排序?

jquery - 为具有相同类别的 div 随机添加类别

java - 从另一个Java类调用 "non-void"方法

java - 运行包含包的 java 文件(在 Linux 上)

java - 如何从maven项目返回网页

java - 将图像 URL 添加到列表字段中的位图图像

java - 使用多个输出参数调用 Oracle StoredProcedure

javascript - Javascript 中的函数传递函数立即调用第二个函数

sql - 在 where 子句和不同的列条件中使用聚合函数

javascript - 这是 Javascript 中将对象实例设置为函数参数的最佳方式