简体   繁体   English

Java中的温度转换代码无法运行?

[英]Temperature conversion code in Java won't run?

Ok,so I'm a complete novice at programming and I just started coding in Java. 好的,所以我是编程的新手,我才开始使用Java进行编码。 I tried to write a code for temperature conversion (Celsius to Fahrenheit) and for some reason it simply won't run! 我试图编写温度转换代码(摄氏温度到华氏温度),由于某种原因,它根本无法运行! Please, help me find out errors in this code(however silly it may be). 请帮助我找出此代码中的错误(但是可能很愚蠢)。

Here's the code: 这是代码:

  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);
      }
  }

You forgot to write the main method which is the start point for a program to run. 您忘记编写main方法,它是程序运行的起点。 Let me modify your code. 让我修改您的代码。

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) 公共静态void主对象(String [] args)

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

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

        System.out.println(tempInFarenheit);
    } 
}

I think this is going to work better for you: 我认为这对您会更好:

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);
    }
}

You equation for Farenheit was incorrect. 您的Farenheit方程式不正确。 Integer division isn't for you, either. 整数除法也不适合您。

You need a main method . 您需要一种主要方法 I also suggest using an IDE such as Eclipse, which can generate the skeleton code for you (including the syntax of the main method). 我还建议使用诸如Eclipse之类的IDE,它可以为您生成框架代码(包括main方法的语法)。

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." );  
        }  
    }  

}  

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

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