0

好的,所以我是一个完全的编程新手,我刚开始用 Java 编码。我试图编写一个温度转换代码(摄氏度到华氏度),但由于某种原因它根本无法运行!请帮我找出这段代码中的错误(不管它多么愚蠢)。

这是代码:

  package tempConvert;

  import java.util.Scanner;

  public class StartCode {

      Scanner in = new Scanner(System. in );

      public double tempInFarenheit;

      public double tempInCelcius;

      {
          System.out.println("enter the temp in celcius");

          tempInCelcius = in .nextDouble();

          tempInFarenheit = (9 / 5) * (tempInCelcius + 32);

          System.out.println(tempInFarenheit);
      }
  }
4

4 回答 4

4

您忘记编写作为程序运行起点的 main 方法。让我修改你的代码。

import java.util.Scanner;

public class StartCode 
{
    Scanner in = new Scanner (System.in); 
    public double tempInFarenheit;
    public double  tempInCelcius;

公共静态无效主要(字符串 [] 参数)

    {
        System.out.println("enter the temp in celcius");

        tempInCelcius = in.nextDouble() ;    
        tempInFarenheit = (9/5)*(tempInCelcius+32);

        System.out.println(tempInFarenheit);
    } 
}
于 2012-04-18T20:09:37.623 回答
1

我认为这对你来说会更好:

import java.util.Scanner;

public class StartCode
{
    public static void main(String[] args) {
        Scanner in = new Scanner (System.in);
        double tempInFarenheit;
        double  tempInCelcius;
        System.out.println("enter the temp in celcius");
        tempInCelcius = in.nextDouble() ;
        tempInFarenheit = 1.8*tempInCelcius+32;
        System.out.println(tempInFarenheit);
    }
}

你的华氏方程不正确。整数除法也不适合你。

于 2012-04-18T20:36:24.893 回答
0

你需要一个main 方法。我还建议使用Eclipse之类的IDE,它可以为您生成框架代码(包括main方法的语法)。

于 2012-04-18T20:12:55.530 回答
0
import java.util.*;
public class DegreeToFahrenheit {


    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);  

        System.out.println("Enter a temperature: ");  
        double temperature = input.nextDouble();  
        System.out.println("Enter the letter of the temperature type. Ex: C or c for celsius, F or f for fahrenheit.: ");  

        String tempType = input.next();  


        String C = tempType;  
        String c = tempType;  

        String F = tempType;  
        String f = tempType;  

        double celsius = temperature;  
        double fahrenheit = temperature;  

        if(tempType.equals(C) || tempType.equals(c)) { 
            celsius = (5*(fahrenheit-32)/9);  
            System.out.print("The fahrenheit degree " + fahrenheit + " is " + celsius + " in celsius." );


        }  


        else if(tempType.equals(F) || tempType.equals(f)) {  

            fahrenheit = (9*(celsius/5)+32);  
            System.out.print("The celsius degree " + celsius + " is " + fahrenheit + " in fahrenheit." ); 
        }  

        else {  
            System.out.print("The temperature type is not recognized." );  
        }  
    }  

}  
于 2013-01-10T18:37:25.523 回答