在我的代码下面工作正常,直到我的最后一个if-else。看来我的布尔变量canGraduate和onProbation做错了。也许我在先前的if-else语句中错误地重新分配它们。 deadbranch发生在我最后一个if-else的另一半。
package lab5;
import java.util.Scanner;
public class Lab5 {
public static void main(String[] args) {
//creates scanner object
Scanner scanner = new Scanner(System.in);
//PART II
//creating variables
double gpa;
int totalCreditsTaken;
int mathScienceCredits;
int liberalArtsCredits;
int electiveCredits;
boolean canGraduate = true;
boolean onProbation = false;
//prompts user for imput
System.out.println("What is your GPA?");
gpa = scanner.nextDouble();
System.out.println("What's the total amount of credits you've taken?");
totalCreditsTaken = scanner.nextInt();
System.out.println("How many math and science credits have you taken?");
mathScienceCredits = scanner.nextInt();
System.out.println("How many liberal arts credits have you taken?");
liberalArtsCredits = scanner.nextInt();
System.out.println("How many elective credits have you taken?");
electiveCredits = scanner.nextInt();
//creates first "if" statment to determine if GPA is high enough to be on track or on probation
if (gpa < 2.0){
System.out.println("You're on academic probation.");
onProbation = true;
}
//PART III
//creates a conditional to see if there's enough credits to graduate
if (totalCreditsTaken < 40 ){
System.out.println("You need more credit(s) to graduate.");
canGraduate = false;
}
else{
System.out.println("Examining credit breakdown...");
canGraduate = true;
}
//PART VI
//Nested if-else if-else to determine if the student qualifies for BA or BS
if ((mathScienceCredits >= 9) && (electiveCredits >= 10)){
System.out.println("You qualify for a BS degree.");
canGraduate = true;
}
else if ((liberalArtsCredits >= 9) && (electiveCredits >= 10)){
System.out.println("You qualify for a BA degree.");
canGraduate = true;
}
else{
System.out.println("You currently don't meet the degree requirments.");
canGraduate = false;
}
//PART V
//Uses an if statement to either congradulate the student or tell the student to take more classes
if ((onProbation = true) || (canGraduate = false)){
System.out.println("You don't qualify to graduate.");
}
else{
System.out.println("Congradualations you qualify to graduate.");
}
}
}
答案 0 :(得分:4)
您在此处分配值:
gulpfile
您需要使用if ((onProbation = true) || (canGraduate = false)){
代替
更新(评论后)
更好的是,不要比较布尔值。相反,由于==
和onProbation
都是布尔类型,因此您可以使用:
canGraduate
归功于@RealSkeptic和@FredK(在他们的评论中)
答案 1 :(得分:2)
关于这里发生的事情的更多解释。
在Java中,=
运算符是赋值,而不是比较(比较运算符为==
)。因此,如果a
是int
,则a = 3
表示&#34;将值3放在变量a
&#34;。
但是作业也是一种表达方式。除了将值放在该变量中之外,表达式还会计算为已分配的值。
因此表达式a = 3
的值为3.您可以执行以下操作:
System.out.println( a = 3 );
这将在a
中放置3,并在控制台上打印3
。
通常,Java不允许您在=
和==
之间混淆。如果变量是int
或float
或String
,请编写如下语句:
if ( a = 3 ) ... // Compilation error
将无效,因为表达式的值为3,int
值,而if
需要类型为boolean
的表达式。所以它会告诉你表达是错误的,你会注意到:&#34;哦,我的意思是==
&#34;。
但如果a
的类型是 boolean
,那么编写a = false
或a = true
是一项赋值,它也返回值已分配 - 这是 boolean
。因此,你可以写
if ( a = false ) ... // Compiles correctly
并且编译器不会抱怨,因为表达式的值是boolean
,而if
期望的是boolean
。编译器并不知道你实际上要进行比较。它只知道它有一个适当类型的表达式。
出于这个原因,建议永远不要比较if ( a == true )
个变量。而不是
if ( a )
写
是完全正确的if
因为a
在true
为a
时会成功,而在false
为canGraduate
时失败。无需比较!为变量提供一个好名字非常重要 - if ( canGraduate )
是一个好名字和一个像
false
具有良好的可读性&#34;如果[用户]可以毕业......&#34;。
对于if ( ! canGraduate )
,您可以使用
if ( canGraduate == false )
它的英语听起来不那么好听,但它比=
更清晰,更清晰,还有额外的奖励,你不会错过if ( canGraduate = false )
和错误地写/**
* Item Filter
*
*/
itemFilter: function(isoContainer, $isoSelector) {
var itemGroup;
var viewBySelect;
var nameFilter;
var buttonFilter;
var usedNames = {};
var $isoContainer = $(isoContainer);
var $win = $(window);
// Lazy load w/ sorting/filtering enabled
var $imgs = $('.lazy');
// init Isotope
var $container = $isoContainer.isotope({
itemSelector: $isoSelector,
layoutMode: 'fitRows',
animationEngine: 'best-available',
getSortData: {
name: '.name'
},
filter: function() {
var $this = $(this);
var itemGroupResult = itemGroup ? $this.is(itemGroup) : true;
var viewBySelectResult = viewBySelect ? $this.is(viewBySelect) : true;
var nameFilterResult = nameFilter ? $this.is(nameFilter) : true;
var buttonResult = buttonFilter ? $this.is(buttonFilter) : true;
return itemGroupResult && viewBySelectResult && nameFilterResult && buttonResult;
}
});
// Fixes layout issues when images are still loading.
$container.imagesLoaded().progress(function() {
$container.isotope('layoutItems');
});
// Load images when filtering is initiated and
// we have a new HTML layout to lazyload on.
$container.isotope('on', 'layoutComplete', function () {
FEATURES.loadVisible($imgs, 'lazylazy');
});
// Load images on scroll with event lazylazy.
$win.on('scroll', function () {
FEATURES.loadVisible($imgs, 'lazylazy');
});
// Initialize lazyload with fadeIn effect and failure_limit.
// Listening on event 'lazylazy'
$imgs.lazyload({
effect: "fadeIn",
failure_limit: Math.max($imgs.length - 1, 0),
event: 'lazylazy'
});
// select filtering - by Role
$('.view-by-select').on('change', function() {
viewBySelect = $('option:selected', this).data('filter');
$container.isotope();
});
// filtering - by Profile Name
$('.filter-title').on('click', 'span', function() {
nameFilter = $(this).hasClass('name');
$container.isotope({ sortBy: nameFilter });
});
// reset filtering
$('.filter-reset').on('click', function() {
$('.view-by-select').val('default').trigger('change');
buttonFilter = $(this).data('filter');
$container.isotope();
});
// Remove duplicate roles from select list
var $selectList = $('select.view-by-select > option');
$selectList.each(function () {
if(usedNames[this.text]) {
$(this).remove();
} else {
usedNames[this.text] = this.value;
}
});
}
。