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

时间:2019-02-27 15:50:34

标签: java

我刚刚进行了一个测验,假设我们应该按照给定的条件进行医院课程的学习,然后在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";
    }

}

2 个答案:

答案 0 :(得分:1)

我刚刚将您的代码复制到了一个名为HospitalTester.java的Java文件中

我运行javac HospitalTester.java

我运行java HospitalTester

扫描仪启动:

Enter a new name for the hospital:

希望有帮助。

答案 1 :(得分:0)

enter image description here

我只是复制了代码并使用javac命令(将其另存为Java文件在随机文件夹中)运行了它,对我来说很好。您是否尝试使用javac或IDE本身执行它?