java - Java if/else 语句
问题描述
我正在计算机类中完成我的最终项目,并尝试在嵌套类中实现基本的 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
同时拥有 astate
和 a user_State
。为什么这个类本身的一个方面是 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);
}
此函数不接受输入也不提供输出,但具有打印某些内容和修改字段的副作用。
唯一一次引用 user_State 是College
当它设置为空字符串时。
this.user_State = "";
仅当College
使用默认构造函数构造对象时。如果College
使用参数构造函数创建对象,则user_State
保留null
.
无论如何,此方法首先将这个空字符串(或 null)设置为自身:
this.user_State = user_State;
所以它只是空字符串(或null)。
接下来,它将字符串state
与空字符串或 null in进行比较user_State
。
if (state.equals(user_State)) {
state
在您的任何测试用例中都不等于空字符串或 null,因此它继续到 else 子句:
else {
this.tuition = "Ineligible";
}
您可能打算printCollege()
采用user_State
您向用户询问的变量。在这种情况下,它不需要 0 个参数,它需要 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
.
推荐阅读
- optimization - 如何在 CVXOPT 中运行 MOSEK 求解器
- asp.net-core - Asp.net 核心运行状况检查随机失败并出现 TaskCanceledException 或 OperationCanceledException
- node.js - Sequlize 获取关联表数据
- tensorflow - Tensorflow - ImportError:DLL加载失败:动态链接库(DLL)初始化例程失败
- c# - 错误 CS0246 找不到类型或命名空间名称“IUnityCompiledFacade”(您是否缺少 using 指令或程序集引用?)
- go - 为什么看不到函数 f 的输出?
- javascript - 将 Props 参数的值设为未定义(ReactJS)
- android - 无法解析 FirebaseInstanceIDService 类中的方法“GetApplicationContext()”
- python - “TypeError: G must be a 'd' matrix”在“cvxpy”中是什么意思?
- angular - Angular 8 Angular Element(自定义 Web 组件)样式