Java类文件在IntelliJ的project中运行,但不在project之外
我刚刚做了一个测验,我们应该用一些给定的标准创建一个医院类,然后在HospitalTester驱动程序类中实例化一个医院类……我完成了任务,IntelliJ没有太多问题,运行良好 然后我做了我经常做的事情,我将要提交的java文件从项目文件夹中复制出来,Java类文件在IntelliJ的project中运行,但不在project之外,java,Java,我刚刚做了一个测验,我们应该用一些给定的标准创建一个医院类,然后在HospitalTester驱动程序类中实例化一个医院类……我完成了任务,IntelliJ没有太多问题,运行良好 然后我做了我经常做的事情,我将要提交的java文件从项目文件夹中复制出来,HospitalTester.java,然后自己提交(教授不希望我们提交项目,只提交单个java文件) 我以为我做完了,一切都很好,但令我恐惧的是,今天我收到一封电子邮件,说班上只有一个人得了100分。我去检查我提交的内容,当我运行它时,什么都没
HospitalTester.java
,然后自己提交(教授不希望我们提交项目,只提交单个java文件)
我以为我做完了,一切都很好,但令我恐惧的是,今天我收到一封电子邮件,说班上只有一个人得了100分。我去检查我提交的内容,当我运行它时,什么都没有发生
我所有的代码都在下面,当我将其作为一个项目打开并保存为“测验1”时,它工作得很好,但当我将其从项目文件夹的src文件夹中删除时,它不会运行
为什么会这样
import java.text.DecimalFormat;
import java.util.Scanner;
public class HospitalTester {
public static void main(String[] args) {
// Construct new hospital instance
Hospital hospital = new Hospital(
"Grey-Sloan Memorial",
10,
150000.79,
2450896.50);
// Asks for keyboard prompt for hospital information.
hospital.setName();
hospital.setNumberOfDoctors();
hospital.setAvgDoctorSalary();
hospital.setTotalGrossIncome();
//Creates a new String variable to print toString() method out from
String sys;
sys = hospital.toString();
System.out.print(sys);
// Provides percent of doctor salary compared to Total Gross Income
hospital.percentDoctors();
}
}
class Hospital {
String name;
private int number_doctors;
private double avg_salary_doctors;
private double total_gross_income;
// Hospital Constructor
public Hospital(String name, int number_doctors, double avg_salary_doctors, double total_gross_income) {
this.name = name;
this.number_doctors = number_doctors;
this.avg_salary_doctors = avg_salary_doctors;
this.total_gross_income = total_gross_income;
}
// Get methods to return hospital name, number of doctors, avg doctor salary, and total gross income of hospital.
public String getName() {
return name;
}
public int getNumberOfDoctors() {
return number_doctors;
}
public double getAvgDoctorSalary() {
return avg_salary_doctors;
}
public double getTotalGrossIncome() {
return total_gross_income;
}
//Set methods to change hospital name, number of doctors, avg doctor salary, and total income of hospital varaibles.
public void setName() {
System.out.print("Enter a new name for the hospital: \n");
this.name = new Scanner(System.in).nextLine();
}
public void setNumberOfDoctors() {
System.out.print("Enter the number of doctors working at the hospital: \n");
this.number_doctors = new Scanner(System.in).nextInt();
}
public void setAvgDoctorSalary() {
System.out.print("Enter the average doctor salary: \n");
this.avg_salary_doctors = new Scanner(System.in).nextDouble();
}
public void setTotalGrossIncome() {
System.out.print("Enter the total gross income that the hospital makes: \n");
this.total_gross_income = new Scanner(System.in).nextDouble();
}
// Calculates total doctors salary: (Number of doctors * avg salary of doctor) / Total Gross Income
public void percentDoctors() {
double percent, percent_converted;
String percent_converted_rounded;
percent = (number_doctors * avg_salary_doctors) / total_gross_income;
percent_converted = percent * 100;
// Round off to two decimal places.
DecimalFormat df = new DecimalFormat("#,###,##0.00");
percent_converted_rounded = df.format(percent_converted);
System.out.print("Doctor salary as a percent of total gross income = " + percent_converted_rounded + "% \n");
}
public String toString() {
//Provide formatting to Doctor salary and hospital income financial numbers.
DecimalFormat df = new DecimalFormat("#,###,##0.00");
String hospital_income_format, avg_salary_doctors_format;
hospital_income_format = df.format(total_gross_income);
avg_salary_doctors_format = df.format(avg_salary_doctors);
return "Name = " + name
+ "\nNumber of Doctors = " + number_doctors + " doctors " +
"\nAverage Doctor Salary = $" + avg_salary_doctors_format +
"\nHospital Income = $" + hospital_income_format + "\n";
}
}
我只是复制了代码并使用javac命令运行它(将其保存为随机文件夹中的java文件),它对我来说很好。您是否尝试使用javac或IDE本身执行它?我刚刚将您的代码复制到名为
HospitalTester.Java的Java文件中
我运行javac HospitalTester.java
我运行java HospitalTester
扫描仪将启动:
Enter a new name for the hospital:
希望有帮助。您不能运行.java类。您需要先编译它,得到一个.class文件,然后才能运行它。@Atizs我怎么能在项目中运行这样一个.java类呢?什么变化?如果您签入Intellij项目文件夹,您将看到类映射。当您执行“运行”时,Intellij首先编译写入.class文件的.java文件,然后执行它们。Project Folder/out/production这是类文件所在的位置,它没有给我一个在IDE中正常运行的选项(绿色箭头)。我尝试在IDE中使用终端,在IDE外部使用
javac HospitalTester.java
而不使用任何东西happened@EvanKim可能您不在“java项目上下文”中,我刚刚发布了一个屏幕截图。你收到错误信息了吗?如果您在windows上,请检查Java环境变量。哦,我没有意识到在javac HospitalTester.Java
之后您必须输入第二个命令Java HospitalTester
lololoh,您需要第二个命令来运行HospitalTester类lol。好的,谢谢,这很有意义。我希望我的老师知道这个lol