繁体   English   中英

Java if/else 语句

[英]Java if/else statement

我正在计算机 class 上完成我的最终项目,并尝试在嵌套的 class 中实现基本的 if/else 语句,但它只选择使用 else 情况。 导入 java.util.Scanner;

public class CollegeApplication {

   public static void main(String[] args) {

       Scanner scan = new Scanner(System.in);

       //create object by default constructor
       College c1 = new College();
       //create object by overloaded constructor
       College c2 = new College("Frostburg", "Frostburg", "MD", 5142);
       College c3 = new College("UMBC", "Baltimore", "MD", 14000);
       //set the information of object 1
       c1.setName("Full Sail");
       c1.setCity("Winter Park");
       c1.setState("FL");
       c1.setStudent_Body(19285);

       System.out.println("Enter your states two-letter abbreviation");
       String user_State = scan.nextLine();

       c1.printCollege();
       System.out.println();
       c2.printCollege();
       System.out.println();
       c3.printCollege();
   }
}
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
import java.util.Scanner;

public class College {

   // private data members
   private String name;
   private String city;
   private String state;
   private int student_Body;
   private String tuition;
   private String user_State;

   // default constructor which set the data member to default value
   public College() {

       this.name = "";
       this.city = "";
       this.state = "";
       this.student_Body = 0;
       this.tuition = "";
       this.user_State = "";
   }

   // parameterized constructor
   public College(String name, String city, String state, int student_Body) {
       super();
       this.name = name;
       this.city = city;
       this.state = state;
       this.student_Body = student_Body;
   }

   // getter and setter
   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public String getCity() {
       return city;
   }

   public void setCity(String city) {
       this.city = city;
   }

   public String getState() {
      return state;
   }

   public void setState(String state) {
      this.state = state;  
   }

   public int getStudent_Body() {
       return student_Body;
   }

   public void setStudent_Body(int student_Body) {
       this.student_Body = student_Body;
   }    
   // print college data
   public void printCollege() {

       System.out.println("Name of College: " + name);
       System.out.println("City of Collge: " + city);
       System.out.println("State of Collge: " + state);
       System.out.println("Student Body Count: " + student_Body);

   this.user_State = user_State;
      if (state.equals(user_State)) {
         this.tuition = "Eligible";
      }
      else {
         this.tuition = "Ineligible";
      }

       System.out.println("In-State Tuition: " + tuition); 
   }
}

如果有人可以帮助 id 知道如何更改 if 语句以不仅打印不合格,我将不胜感激

这个问题不包含问题,但我看到了问题区域。

问问自己,为什么你的College既有state又有user_State 为什么这个 class 本身的一个方面是 user_State? 甚至没有 getter 和 setter (因为不应该有)。

public void printCollege() {

   this.user_State = user_State;
      if (state.equals(user_State)) {
         this.tuition = "Eligible";
      }
      else {
         this.tuition = "Ineligible";
      }

       System.out.println("In-State Tuition: " + tuition); 
   }

这个 function 不接受输入,也不给出 output,但有打印某些东西和修改字段的副作用。

College中引用 user_State 的唯一其他时间是当它设置为空字符串时。

this.user_State = "";

仅当使用默认构造函数构造College object 时。 如果使用参数构造函数生成College object,则user_State仍为null

无论如何,此方法首先将这个空字符串(或 null)设置为自身:

this.user_State = user_State;

所以它只是空字符串(或null)。

接下来,它将字符串state与 user_State 中的空字符串或user_State进行比较。

if (state.equals(user_State)) {

在您的任何测试用例中, state不等于空字符串也不等于 null,因此它继续到 else 子句:

else {
    this.tuition = "Ineligible";
}

您可能打算让printCollege()获取您向用户询问的user_State变量。 在这种情况下,它不需要 0 arguments,它需要 1 个字符串参数。

public void printCollege(String userState) {

      if (state.equals(userState)) {
         this.tuition = "Eligible";
      }
      else {
         this.tuition = "Ineligible";
      }

       System.out.println("In-State Tuition: " + tuition); 
   }

并且printCollege(String userState)的调用应该根据您从用户那里收到的输入进行。

以后请遵循 Java 命名约定,像user_State应该只是userState

  1. user_State 实例变量没有 setter 方法
  2. 没有为 user_State 实例变量的初始化提供参数

因此,如果条件失败,因为它将 user_State 变量视为实例变量,在默认构造函数的情况下它将始终为“”,在参数化构造函数的情况下将始终为“null”

任何一个

根据@PatricChen 为printCollege()方法提供参数

或者

printCollege()中删除语句this.user_State = user_State并为 user_State 变量和 user_State 参数提供 setter 方法给 College class 的参数化构造函数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM