我有兩種方法
*`public boolean validateMarks() {
return (this.qualifyingMarks >= 65 && this.qualifyingMarks <= 100);
}
public boolean validateCourseId() {
return (this.courseId >= 1001 && this.courseId <= 1005);
}`*
validateMarks():用于驗證合格考試成績 - 合格成績在 65 到 100 之間(含)用于計算折扣后的課程費用。
因此,當小于 65 時列印列印“不合格,您失敗了”,當課程無效時“課程不正確,請使用正確的課程編號重試”
這是我的 calculateCourseFee 方法
***if(this.validateMarks()) {
this.courseFee = fee - (fee * discount);
System.out.println("****Course Allocation Details****" "\n"
"Student Name: " this.getStudentName() "\n"
"Course Id: " this.getCourseId() "\n"
"Qualifying Exam Marks: " this.getQualifyingMarks() "\n"
"Student's Registration Id: " this.getRegistrationId() "\n"
"Total Course Fee: " this.getCourseFee() "\n"
"Hostel Required: " hostel);
}else {
System.out.println("wrong for marks ");
}
if(this.validateCourseId()) {
this.courseFee = fee - (fee * discount);
System.out.println("****Course Allocation Details****" "\n"
"Student Name: " this.getStudentName() "\n"
"Course Id: " this.getCourseId() "\n"
"Qualifying Exam Marks: " this.getQualifyingMarks() "\n"
"Student's Registration Id: " this.getRegistrationId() "\n"
"Total Course Fee: " this.getCourseFee() "\n"
"Hostel Required: " hostel);
}else {
System.out.println("Wroog for course");
}
***
I make two different ifs for the two requirements, but everytime I run it, it prints the else statement to, even if marks is greather than 65... am I missing something?
Reviewing my code and tell me what am I missing or what am I doing wrong
uj5u.com熱心網友回復:
您在此處顯示的代碼部分似乎按預期作業。
public class Driver {
public static void main(String args[]) {
Eligible e1 = new Eligible();
e1.calculateCourseFee();
}
}
class Eligible{
int qualifyingMarks = 66;
int courseId = 1002;
public boolean validateMarks() {
return (this.qualifyingMarks >= 65 && this.qualifyingMarks <= 100);
}
public boolean validateCourseId() {
return (this.courseId >= 1001 && this.courseId <= 1005);
}
public void calculateCourseFee(){
if(this.validateMarks()) {
System.out.println("works for marks");
}else {
System.out.println("wrong for marks ");
}
if(this.validateCourseId()) {
System.out.println("works for course");
}else {
System.out.println("Wroog for course");
}
}
}
輸出:
為分數
作業 為課程作業
也許問題在于您如何設定qualifyingMarks和courseId變數的值?
uj5u.com熱心網友回復:
我希望我能給你一個喜歡或豎起大拇指,我終于做到了,感謝你給我的所有答案,我只是將兩個 if 合二為一。這是代碼:
if(this.validateCourseId() && this.validateMarks()) {
this.courseFee = fee - (fee * discount);
System.out.println("****Course Allocation Details****" "\n"
"Student Name: " this.getStudentName() "\n"
"Course Id: " this.getCourseId() "\n"
"Qualifying Exam Marks: " this.getQualifyingMarks() "\n"
"Student's Registration Id: " this.getRegistrationId() "\n"
"Total Course Fee: " this.getCourseFee() "\n"
"Hostel Required: " hostel);
}else if(!this.validateCourseId()) {
System.out.println("Wrong course");
}
else if(!this.validateMarks()) {
System.out.println("You've failed");
}
感謝大家!!
uj5u.com熱心網友回復:
可能qualifyingMarks 為零或其他值,在方法“validateMarks”中列印qualifyingMarks,你會得到你的問題的原因。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/453412.html
