Java 字符串比较不需要';t成功并将总计计算为0

Java 字符串比较不需要';t成功并将总计计算为0,java,inheritance,Java,Inheritance,如果用户从LabCourse(BIO,CIS…)中列出的类中选择一个类,它应该给出总加上实验室费用(50),但我的代码总是执行总为0并打印“这个类没有实验室”消息 有什么问题吗 CollegeCourse.java public class CollegeCourse { String name; int num; int crd; double fee; double total; public CollegeCourse(String n) { name =n; } publ

如果用户从
LabCourse
(BIO,CIS…)中列出的类中选择一个类,它应该给出总加上实验室费用(50),但我的代码总是执行总为0并打印
“这个类没有实验室”
消息

有什么问题吗

CollegeCourse.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;
  // }
}
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");
}
}
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);
   }
 } 
LabCourse.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;
  // }
}
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");
}
}
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);
   }
 } 
UseCourse.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;
  // }
}
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");
}
}
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);
   }
 } 

我不确定,但你到底把实验室的费用定在哪里?是的,比较字符串必须使用String.equals()而不是==


字符串是类。您不应该使用
==
来比较类(除非您想检查它们是否是完全相同的对象(在本例中,不仅仅是相同的文本))

当您尝试对字符串使用
==
时,某些IDE(例如)会向您发出警告

尝试
System.out.println(新字符串(“ABC”)=“ABC”)-这将打印
false

使用
等于

if (name.equals("BIO") || name.equals("CHM") ||
    name.equals("CIS") || name.equals("PHY"))
public class LabCourse extends CollegeCourse
{
  ...
  public void computetotal(String name, double fee)
  ...
}
使用正则表达式的更简单选项:

if (name.matches("BIO|CHM|CIS|PHY"))

编辑:

另一个问题是,您从未设置
LabCourse.fee
,但您在此处使用它:

super.computetotal(fee, crd);
因此,在构造函数中设置它:

public LabCourse(String name, double fee)
{
  super(name);
  this.fee = fee; // or add this as a parameter to CollegeCourse's constructor
}
但是,由于
computetotal
会覆盖费用,对它的多次调用可能不会像您希望的那样有效,因此您可能希望将其作为参数传递给
computetotal

if (name.equals("BIO") || name.equals("CHM") ||
    name.equals("CIS") || name.equals("PHY"))
public class LabCourse extends CollegeCourse
{
  ...
  public void computetotal(String name, double fee)
  ...
}

这就引出了一个问题:
fee
作为类变量有意义吗?或者,将
computetotal
中的代码放在构造函数中,然后去掉
computetotal
或类似的内容,可能会更有意义。同样的
名称

我得到了它。。。感谢您的时间,我仍然有问题,因为费用仍然没有被LabCourse继承。