所以我需要用java写一个程序,把2个用户输入的温度转换成华氏温度和开尔文。我写了代码,它在eclipse中工作,但我的老师严格地说它必须在cmd中工作。它编译得很好,但是当我运行它时,它会显示为could not find or load main class temperatureTester (带有main的类名)。这是我的第一篇文章,所以如果你需要更多的信息,请询问,我正在寻找任何想法,为什么会发生这种情况。下面是我对这个问题的代码。
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;
}
}发布于 2016-11-12 12:40:13
最好的方法可能是将您的项目导出为可执行的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然后它就会像咒语一样执行!
发布于 2016-11-12 15:44:41
只需在cmd中使用javac编译这两个文件,在成功编译后,只需使用java运行您的主类,即temperatureTester类。它成功地执行了。

https://stackoverflow.com/questions/40559433
复制相似问题