如果用户从(BIO,CIS ...)中列出的类中选择一个类,LabCourse
它应该给出总加实验室费用(50),但我的代码总是执行总为 0 并打印"This class don't have lab"
消息
问题可能是什么?
学院课程.java
public class CollegeCourse
{
String name;
int num;
int crd;
double fee;
double total;
public CollegeCourse(String n)
{
name =n;
}
public double computetotal(double fee, int crd)
{
fee = fee * crd;
System.out.println("Total is $" + fee);
this.fee=fee;
return fee;
}
public String getname()
{
return name;
}
public int getnum()
{
return num;
}
public int getcrd()
{
return crd;
}
// public double getfee()
// {
// return fee;
// }
}
实验室课程.java
public class LabCourse extends CollegeCourse
{
double total=0;
public LabCourse(String name, double fee)
{
super(name);
}
public void computetotal(String name)
{
super.computetotal(fee, crd);
if((name == "BIO") || (name == "CHM") || (name == "CIS") || (name =="PHY"))
{
total = fee+ 50;
System.out.println("Total with lab is: " + total);
}
else
System.out.println("This class don't have Lab");
}
}
使用课程.java
import javax.swing.*;
import java.util.*;
import java.util.Scanner;
public class UseCourse
{
public static void main(String args[]) throws Exception
{
String name;
int num;
int crd;
double fee;
Scanner inputDevice = new Scanner(System.in);
System.out.print("Enter Department name: ");
name = inputDevice.next();
System.out.print("Enter Course number: ");
num= inputDevice.nextInt();
System.out.print("Enter Credit hours: ");
crd = inputDevice.nextInt();
System.out.print("Enter fee: ");
fee = inputDevice.nextDouble();
System.out.print("\n\n");
CollegeCourse course = new CollegeCourse(name);
course.computetotal(fee, crd);
LabCourse full = new LabCourse(name, fee);
full.computetotal(name);
}
}