gpt4 book ai didi

Java 类文件在 IntelliJ 项目中运行,但不在项目之外运行

转载 作者:行者123 更新时间:2023-12-02 10:12:22 24 4
gpt4 key购买 nike

我刚刚进行了一个测验,我们应该根据一些给定的标准创建一个医院类,然后在 HospitalTester 驱动程序类中实例化一个医院类...我在 IntelliJ 中顺利完成了作业,并且运行良好。

然后我做了我一直做的事情,我将要提交的java文件HospitalTester.java从项目文件夹中复制出来,然后单独提交(教授没有希望我们提交项目,只是单独的 java 文件)。

我以为我已经完成了,一切都很好,但令我恐惧的是,我今天收到一封电子邮件,说类只有 1 人获得了 100/100。我去检查我提交的内容,当我运行它时,没有任何反应......

我的所有代码都在下面,当我将其作为保存为“Quiz 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";
}

}

最佳答案

我刚刚将您的代码复制到一个名为 HospitalTester.java

的 Java 文件中

我运行javac HospitalTester.java

我运行java HospitalTester

扫描仪启动:

Enter a new name for the hospital:

希望有帮助。

关于Java 类文件在 IntelliJ 项目中运行,但不在项目之外运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54909360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com