Java中的温度转换代码不会运行?

时间:2012-04-18 20:08:20

标签: java debugging runtime-error

好的,所以我是一个完整的编程新手,我刚开始用Java编写代码。我试着编写一个温度转换代码(Celsius到Fahrenheit),由于某种原因它只是不会运行!请帮助我找出此代码中的错误(但可能很愚蠢)。

以下是代码:

  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 个答案:

答案 0 :(得分:4)

您忘记编写main方法,该方法是程序运行的起点。我来修改你的代码。

import java.util.Scanner;

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

public static void main(String [] args)

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

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

        System.out.println(tempInFarenheit);
    } 
}

答案 1 :(得分: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);
    }
}

你对Farenheit的等式是不正确的。整数除法也不适合你。

答案 2 :(得分:0)

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

答案 3 :(得分: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." );  
        }  
    }  

}