2016-11-12 107 views
0

所以我需要用java编写一个程序,用2个用户输入摄氏温度并将其转换为华氏度和开氏度。我写了代码,它在eclipse中工作,但我的老师严格地说它必须在cmd中工作。它编译得很好,但是当我去运行它时,它指出could not find or load main class temperatureTester(我的主类的名字)。这是我的第一篇文章,所以如果你需要更多的信息,请问,我正在寻找任何想法,为什么发生这种情况。下面是我的问题代码。如何在cmd上运行java中的2个连接类?

import java.util.Scanner; 
public class temperatureTester{ 

    public static void main (String[]args){ 

    //create 2 objects connecting to temperatureC 
    temperatureC firstValue = new temperatureC(); 
    temperatureC secondValue = new temperatureC(); 
    // initialize scanner 
    Scanner stdin = new Scanner (System.in); 
    //initialize variables 
    double firstC = 0; 
    double secondC = 0; 
    //prompt user for both values 
    System.out.print("Please enter initial temperatures: "); 
    firstC = stdin.nextDouble(); 
    secondC = stdin.nextDouble(); 
    //call object set methods and pass entered values as arguments 
    firstValue.setC(firstC); 
    secondValue.setC(secondC); 
    //display the values for the values for different temp. units 
    System.out.println("1) The current temperature in Celcius is: " + firstValue.getC()); 
    System.out.println("1) The current temperature in fahreinheit is: " + firstValue.getF()); 
    System.out.println("1) The current temperature in kelvin is: " + firstValue.getK()); 
    System.out.println("---------------------------------------------------------------"); 
    System.out.println("2) The current temperature in Celcius is: " + secondValue.getC()); 
    System.out.println("2) The current temperature in fahreinheit is: " + secondValue.getF()); 
    System.out.println("2) The current temperature in kelvin is: " + secondValue.getK()); 

这是第二类

public class temperatureC{ 

private double C; 

/** 
The setC method stores the value in the C field 

@ param initialC the value stored in C 
*/ 

public void setC(double initialC){ 
    C = initialC; 
} 

/** 
The getC returns the C value and also sets a lower limit, 
if a number below is entered it sets it ti the limit. 

@Return the value of the C 
*/ 
public double getC(){ 

    if(C < -273.15){ 
     C = -273.15; 
    } 

    return C; 
} 

/** 
the getF method calculates and returns a value for C in fahrenheit 

@return the computed for C in fahrenheit 
*/ 
public double getF(){ 

    return C * 1.8 + 32; 
} 

/** 
The getK method computes and returns a value for temperature C in kelvin 

@return the computed Kelvin value 
*/ 
public double getK(){ 

    return C + 273.15; 
} 
} 
+0

你怎么运行它?你能发布你的命令吗? – root545

+0

请注意,Java类应以'CapitalLetters'开头 –

回答

0

,最好的办法可能B中您将项目导出为可执行的JAR文件来实现这一目标有以下官方ecclipse链接http://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-37.htm

后来一看下一部分你需要从命令行运行它,这是一个有趣的部分,只需将目录更改为jar文件所在的路径,然后出现以下命令

java -jar yourJar.jar 
pause 

然后它会像魅力一样执行!

+0

我认为他应该在cmd中编译和运行,而不是将其导出到jar并在cmd中运行它。 – SkrewEverything

0

使用javac 编译成功简单地使用即javatemperatureTester类中运行的主类后只需编译在cmd中的这两个文件。它成功执行。

enter image description here