[英]Java conditional statement/ initializing variable
Currently this is my code, and I need to display " Please enter a valid choice" when the user don't pick A,B,C,D,E or F as their choices. 当前这是我的代码,当用户未选择A,B,C,D,E或F作为选择时,我需要显示“请输入有效的选择”。 The problem is if I put the statement " Please enter a valid...." on the "else" conditional, Java would ask me to initialized the variable ActivityFactor as there will not be one if the user don't select the correct choice.
问题是如果我在“ else”条件上加上“请输入一个有效的...”语句,Java会要求我初始化变量ActivityFactor,因为如果用户未选择正确的选择就不会有变量。 Anyone know how I can fix this?
有人知道我该如何解决吗? Or any idea how I should code a program to do such?
还是有任何想法我应该如何编写程序来做到这一点?
if((inGender.equalsIgnoreCase("M") ||(inGender.equalsIgnoreCase ("F"))) && inActivity.equalsIgnoreCase("A"))
ActivityFactor = 1.0;
else if ((inGender.equalsIgnoreCase("M") ||(inGender.equalsIgnoreCase ("F"))) && inActivity.equalsIgnoreCase("B"))
ActivityFactor = 1.3;
else if (inGender.equalsIgnoreCase("M") && inActivity.equalsIgnoreCase("C"))
ActivityFactor = 1.6;
else if (inGender.equalsIgnoreCase("F") && inActivity.equalsIgnoreCase("C"))
ActivityFactor = 1.5;
else if (inGender.equalsIgnoreCase("M") && inActivity.equalsIgnoreCase("D"))
ActivityFactor = 1.7;
else if (inGender.equalsIgnoreCase("F") && inActivity.equalsIgnoreCase("D"))
ActivityFactor = 1.6;
else if (inGender.equalsIgnoreCase("M") && inActivity.equalsIgnoreCase("E"))
ActivityFactor = 2.1;
else if (inGender.equalsIgnoreCase("F") && inActivity.equalsIgnoreCase("E"))
ActivityFactor = 1.9;
else if (inGender.equalsIgnoreCase("M") && inActivity.equalsIgnoreCase("F"))
ActivityFactor = 2.4;
else if (inGender.equalsIgnoreCase("F") && inActivity.equalsIgnoreCase("F"))
ActivityFactor = 2.2;
else
{
ActivityFactor = -1;
}
//After
if(ActivityFactor != -1){
tdee = (nBMR * ActivityFactor);
System.out.println(tdee);}
else
{ System.out.println("Please enter a valid choice");
}
If non of the conditions in the if statements is true, then you don't assign anything to ActivityFactor
, and it is not initialized when used in the line double TDEE = (nBMR * ActivityFactor);
如果if语句中的所有条件都不为true,则您不会为
ActivityFactor
分配任何内容,并且在double TDEE = (nBMR * ActivityFactor);
行中使用时不会对其进行初始化double TDEE = (nBMR * ActivityFactor);
. 。
Either initialize it before the code you've shown here, give it a default value in the last case, or loop until you get a valid value. 您可以在此处显示的代码之前对其进行初始化,在最后一种情况下为其提供默认值,或者循环直到获得有效值。
Either initialize your variable before the loop, or place the whole loop inside a function and then do something like: 可以在循环之前初始化变量,或者将整个循环放入函数中,然后执行以下操作:
double TDEE = (nBMR * getActivityFactor());
Also, have a look at this : http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html 另外,看看这个: http : //docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
happy coding! 祝您编码愉快! ;)
;)
initialise ActivityFactor to an usual value before your conditional. 在有条件之前将ActivityFactor初始化为通常的值。
For example you may do this: 例如,您可以这样做:
// knowing that it can never be -1
// so if that value remains, you know that user entered wrong letter
ActivityFactor = -1
// then the conditional begins
if((inGender.equalsIgnoreCase("M") ||(inGender.equalsIgnoreCase ("F"))) && inActivity.equalsIgnoreCase("A"))
...
// after conditional...
if(activityFactor != -1){
double TDEE = (nBMR * ActivityFactor);
}
By the way, I suggest you use 'activityFactor' instead of ActivityFactor. 顺便说一句,我建议您使用“ activityFactor”而不是ActivityFactor。
You should do two things: 您应该做两件事:
You can solve this problem throwing an exception that you will catch. 您可以解决此问题并抛出将捕获的异常。
private double getTDEE (String inGender, String inActivity) {
//logic
else {
throw new IllegalArgumentException("Please enter a valid choice");
}
return (nBMR * ActivityFactor);
}
As you're already aware, the problem with your code is that execution continues regardless of whether the user has entered valid input. 如您所知,代码的问题在于无论用户是否输入有效输入,执行都会继续。 Due to this, much refactoring is due - I would also attempt to make the conditional statements a little bit prettier;
因此,需要进行大量的重构-我还将尝试使条件语句更漂亮。 but that's personal preference.
但这是个人喜好。
Possible solutions: 可能的解决方案:
a) Use a loop - Then break out of the loop when the user has entered satisfactory input.. a)使用循环 -当用户输入令人满意的输入时,退出循环。
while( true ){
/* get input from the user */
/* run through validation checks...
and -break- out of the loop when they're satisfied */
}
/* do calculations here */
b) Use a function to abstract all this logic away.. (as Psyclops suggests) b)使用一个函数将所有这些逻辑抽象化..(如Psyclops建议)
Personally, I would use a combination of these approaches - have all of this logic extracted away in to a function which returns false when no valid input is entered, and then use a construct like while(! yourFunction() )
to simply loop through it until it's completed. 就我个人而言,我将使用这些方法的组合-将所有这些逻辑提取到一个函数中,当未输入有效输入时该函数将返回false,然后使用
while(! yourFunction() )
来简单地遍历它直到完成 You could using passing by reference to avoid having to use the return type for anything other than a boolean value. 您可以使用按引用传递来避免必须将返回类型用于布尔值以外的任何值。
I wouldnt initialise the variable before the loop! 我不会在循环之前初始化变量! This just means the programme will continue to execute;
这只是意味着程序将继续执行; however it wont have any appropriate data in there - which is potentially worse than the application just crashing.
但是它里面没有任何合适的数据-这可能比刚崩溃的应用程序还要糟糕。
I haven't exactly gifted you the answer in code - but hopefully it's a starting point to allow you to think about how to conceptually compose/design such a solution. 我还没有完全以代码的形式给您答案-但希望它是一个起点,让您考虑如何从概念上构成/设计这样的解决方案。 That's generally the hardest part.. ;) Good luck.
通常,这是最困难的部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.