if-else 不应该有 break 吗?
If-else should not have break?
所以我的教授提到 if/if-else 语句中的中断是 "bad" 代码。
她到底是什么意思?另外,我怎样才能修复我目前编写的代码,因为它确实按照我想要的方式工作,现在我需要摆脱 break 语句。
int sumOne = 1;
int sumTwo = 1;
int sumOneTotal = 0;
int sumTwoTotal = 0;
while(sumOne > 0 || sumTwo > 0){
System.out.print("Enter a number to add to first sum: ");
//The user enters in a value for the first sum.
sumOne = input.nextInt();
/**
* We use an if-else statment to ensure sumOne is never less than or equal to 0.
* If it does it ends the program immediately and totals the sums.
* This is because we only want the user to enter in positive numbers.
*/
if (sumOne <= 0){
break;
}else{
sumOneTotal = sumOneTotal + sumOne;
}
System.out.print("Enter a number to add to second sum: ");
//The user enters in a value for the second sum.
sumTwo = input.nextInt();
/**
* We use an if-else statment to ensure sumTwo is never less than or equal to 0.
* If it does it ends the program immediately and totals the sums.
* This is because we only want the user to enter in positive numbers.
*/
if (sumTwo <= 0){
break;
}else{
sumTwoTotal = sumTwoTotal + sumTwo;
}
}
//We print out the total of sumOneTotal and sumTwoTotal.
System.out.printf("%1s%1d%12s%1s%1d", "First sum: ", sumOneTotal, " ", "Second sum: ", sumTwoTotal);
本质上,我希望用户输入任何正数,然后将该数字添加到第一个或第二个总和中。一旦用户输入任何数字 <= 0 我希望程序立即停止。当我修改代码时,我一直遇到的问题是代码保持 运行 通过。这意味着如果我让用户输入 0 以添加到第一个总和中,代码仍然要求用户为第二个总和输入一个数字。我需要它立即停止而不是继续。任何帮助都会有很大的帮助!我正在使用 Java.
编辑!!!所以假设我想制作一个程序来做我现在正在做的完全相同的事情,只是没有 break 语句。我该怎么做?一些规则。最外面的语句必须是 "while" 循环。它的内部运作可以是任何东西。我还需要机器交替打印出 "Enter a number to add to first sum:" 和 "Enter a number to add to second sum:"。因此,如果我输入 1、2、3、4。第一个和是 4,第二个和是 6。最后的规则是它不能包含任何 break 语句!
有时候避免中断比使用它更糟糕。我会这样写,少一个休息。
int sumOneTotal = 0;
int sumTwoTotal = 0;
while (true) {
System.out.print("Enter a number to add to first sum: ");
//The user enters in a value for the first sum.
int sumOne = input.nextInt();
if (sumOne <= 0)
break;
sumOneTotal += sumOne;
System.out.print("Enter a number to add to second sum: ");
//The user enters in a value for the second sum.
int sumTwo = input.nextInt();
if (sumTwo <= 0)
break;
sumTwoTotal += sumTwo;
}
您可以避免中断,但这不会生成代码 clearer/simpler 恕我直言。
int sumOneTotal = 0;
int sumTwoTotal = 0;
boolean okay = true;
do {
System.out.print("Enter a number to add to first sum: ");
//The user enters in a value for the first sum.
int sumOne = input.nextInt();
if (sumOne <= 0) {
okay = false;
} else {
sumOneTotal += sumOne;
System.out.print("Enter a number to add to second sum: ");
//The user enters in a value for the second sum.
int sumTwo = input.nextInt();
if (sumTwo <= 0) {
okay = false;
} else {
sumTwoTotal += sumTwo;
}
} while (okay);
同样的建议也适用于使用标签。尽可能避免它们,除非避免它们意味着做更糟糕的事情。
我不一定同意在 if
中使用 break
总是不好的做法。然而,这更多的是见仁见智,并不是真正的主题。我将回答您问题中与主题相关的部分,即:如何修复我的代码以不在 if
.
中使用 break
下面的代码将继续循环,要求用户输入,直到他们输入有效数字。这避免了您原来的问题,并且有一个额外的好处,即允许用户在犯错时有机会输入新数字,而不是退出循环并重新开始。
int sumOne = 1;
int sumTwo = 1;
int sumOneTotal = 0;
int sumTwoTotal = 0;
while(sumOne > 0 || sumTwo > 0){
do {
System.out.print("Enter a number to add to first sum: ");
//The user enters in a value for the first sum.
sumOne = input.nextInt();
System.out.print("Enter a number to add to second sum: ");
//The user enters in a value for the second sum.
sumTwo = input.nextInt();
}while(sumTwo <= 0 || sumOne <= 0);
sumOneTotal = sumOneTotal + sumOne;
sumTwoTotal = sumTwoTotal + sumTwo;
}
//We print out the total of sumOneTotal and sumTwoTotal.
System.out.printf("%1s%1d%12s%1s%1d", "First sum: ", sumOneTotal, " ", "Second sum: ", sumTwoTotal);
这是对结构化编程还是新事物的倒退,回到 goto 语句等无处不在的时代。理论上,理想情况下,应该永远不用breaks/continues,只有return的单点。实际上,这样做 可以 通过使程序更难编写、更难阅读并占用更多计算资源,从而使您的工作变得更加困难。多个 returns、continues 和 breaks 是真正结构化编程和意大利面条代码之间的中间人。使用得当,没毛病。
一般来说,我发现如果您已经在使用使代码难以阅读的不良做法(例如,编写大量逻辑块而不分解它,紧密耦合对象),它们只会使您的代码模糊不清等)。
如果您有兴趣,here is a link to an interesting perspective on why NOT to use them. And here 是关于它们为何有益的观点。
许多其他人已经用代码回答了,但这是我的镜头:)
public class Main {
public static void main(String args[]) {
int sumOne = 1;
int sumTwo = 1;
int sumOneTotal = 0;
int sumTwoTotal = 0;
Scanner input = new Scanner(System.in);
while(sumOne > 0 || sumTwo > 0){
System.out.print("Enter a number to add to first sum: ");
sumOne = input.nextInt();
if (is_positive(sumOne)){
sumOneTotal = sum_numbers(sumOneTotal, sumOne);
System.out.print("Enter a number to add to second sum: ");
sumTwo = input.nextInt();
if(is_positive(sumTwo)){
sumTwoTotal = sum_numbers(sumTwoTotal, sumTwo);
}
}
}
System.out.printf("%1s%1d%12s%1s%1d", "First sum: ", sumOneTotal, " ", "Second sum: ", sumTwoTotal);
return;
}
public static int sum_numbers(int x, int y){
int total = x + y;
return total;
}
public static boolean is_positive(int x){
boolean is_pos = true;
if(x < 0){
is_pos = false;
}
return is_pos;
}
}
我会说它现在更难阅读了。我的代码越靠右,我就越为需要维护它的人感到难过。当然,我可以通过在方法中包装(更多)位来删除一两个级别的缩进。然后它变得更容易阅读,但有一点黑盒化每一点逻辑似乎都是多余的......
while (sumOne > 0 && sumTwo > 0) {
System.out.print("Enter a number to add to first sum: ");
sumOne = input.nextInt();
if (sumOne > 0) {
sumOneTotal = sumOneTotal + sumOne;
System.out.print("Enter a number to add to second sum: ");
sumTwo = input.nextInt();
if (sumTwo > 0)
sumTwoTotal = sumTwoTotal + sumTwo;
}
}
但我同意其他人的看法 - 没有任何意义可以避免 "break"
更干净,没有中断。
int candidate = 0;
int [] sums = {0,0};
int index = 1;
System.out.print("Enter a number to add to first sum: ");
while((candidate = input.nextInt()) > 0){
sums[index] = sums[index] + candidate;
index = (index + 1)%2;
System.out.print("Enter a number to add to " + ((index == 0) ? "first":"second" ) + " sum: ");
}
//We print out the totals.
System.out.printf("%1s%1d%12s%1s%1d", "First sum: ", sums[0], " ", "Second sum: ", sums[1]);
这并不是说您应该始终避免中断,但在这种情况下您可以避免中断以使您的代码更短、冗余更少。
如果你把你的代码流程图画出来你可以看到它中间有一个bucle退出,这是不对的,正确的方法是在评估时退出,也有人在评估时退出阅读您的代码,他们应该期望在 while 块内评估为 false 而不是随机的时,bucle 会留下,我获取了您的代码并进行了一些修复以使其按预期工作,但我不确定这是否是您的原因老师期待
int sumOne = 1;
int sumTwo = 1;
int sumOneTotal = 0;
int sumTwoTotal = 0;
while (sumOne > 0 && sumTwo > 0) {
System.out.print("Enter a number to add to first sum: ");
// The user enters in a value for the first sum.
sumOne = input.nextInt();
/**
* We use an if-else statment to ensure sumOne is never less than or
* equal to 0. If it does it ends the program immediately and totals
* the sums. This is because we only want the user to enter in
* positive numbers.
*/
if (sumOne > 0) {
sumOneTotal = sumOneTotal + sumOne;
System.out.print("Enter a number to add to second sum: ");
// The user enters in a value for the second sum.
sumTwo = input.nextInt();
/**
* We use an if-else statment to ensure sumTwo is never less
* than or equal to 0. If it does it ends the program
* immediately and totals the sums. This is because we only want
* the user to enter in positive numbers.
*/
if (sumTwo > 0) {
sumTwoTotal = sumTwoTotal + sumTwo;
}
}
}
所以我的教授提到 if/if-else 语句中的中断是 "bad" 代码。 她到底是什么意思?另外,我怎样才能修复我目前编写的代码,因为它确实按照我想要的方式工作,现在我需要摆脱 break 语句。
int sumOne = 1;
int sumTwo = 1;
int sumOneTotal = 0;
int sumTwoTotal = 0;
while(sumOne > 0 || sumTwo > 0){
System.out.print("Enter a number to add to first sum: ");
//The user enters in a value for the first sum.
sumOne = input.nextInt();
/**
* We use an if-else statment to ensure sumOne is never less than or equal to 0.
* If it does it ends the program immediately and totals the sums.
* This is because we only want the user to enter in positive numbers.
*/
if (sumOne <= 0){
break;
}else{
sumOneTotal = sumOneTotal + sumOne;
}
System.out.print("Enter a number to add to second sum: ");
//The user enters in a value for the second sum.
sumTwo = input.nextInt();
/**
* We use an if-else statment to ensure sumTwo is never less than or equal to 0.
* If it does it ends the program immediately and totals the sums.
* This is because we only want the user to enter in positive numbers.
*/
if (sumTwo <= 0){
break;
}else{
sumTwoTotal = sumTwoTotal + sumTwo;
}
}
//We print out the total of sumOneTotal and sumTwoTotal.
System.out.printf("%1s%1d%12s%1s%1d", "First sum: ", sumOneTotal, " ", "Second sum: ", sumTwoTotal);
本质上,我希望用户输入任何正数,然后将该数字添加到第一个或第二个总和中。一旦用户输入任何数字 <= 0 我希望程序立即停止。当我修改代码时,我一直遇到的问题是代码保持 运行 通过。这意味着如果我让用户输入 0 以添加到第一个总和中,代码仍然要求用户为第二个总和输入一个数字。我需要它立即停止而不是继续。任何帮助都会有很大的帮助!我正在使用 Java.
编辑!!!所以假设我想制作一个程序来做我现在正在做的完全相同的事情,只是没有 break 语句。我该怎么做?一些规则。最外面的语句必须是 "while" 循环。它的内部运作可以是任何东西。我还需要机器交替打印出 "Enter a number to add to first sum:" 和 "Enter a number to add to second sum:"。因此,如果我输入 1、2、3、4。第一个和是 4,第二个和是 6。最后的规则是它不能包含任何 break 语句!
有时候避免中断比使用它更糟糕。我会这样写,少一个休息。
int sumOneTotal = 0;
int sumTwoTotal = 0;
while (true) {
System.out.print("Enter a number to add to first sum: ");
//The user enters in a value for the first sum.
int sumOne = input.nextInt();
if (sumOne <= 0)
break;
sumOneTotal += sumOne;
System.out.print("Enter a number to add to second sum: ");
//The user enters in a value for the second sum.
int sumTwo = input.nextInt();
if (sumTwo <= 0)
break;
sumTwoTotal += sumTwo;
}
您可以避免中断,但这不会生成代码 clearer/simpler 恕我直言。
int sumOneTotal = 0;
int sumTwoTotal = 0;
boolean okay = true;
do {
System.out.print("Enter a number to add to first sum: ");
//The user enters in a value for the first sum.
int sumOne = input.nextInt();
if (sumOne <= 0) {
okay = false;
} else {
sumOneTotal += sumOne;
System.out.print("Enter a number to add to second sum: ");
//The user enters in a value for the second sum.
int sumTwo = input.nextInt();
if (sumTwo <= 0) {
okay = false;
} else {
sumTwoTotal += sumTwo;
}
} while (okay);
同样的建议也适用于使用标签。尽可能避免它们,除非避免它们意味着做更糟糕的事情。
我不一定同意在 if
中使用 break
总是不好的做法。然而,这更多的是见仁见智,并不是真正的主题。我将回答您问题中与主题相关的部分,即:如何修复我的代码以不在 if
.
break
下面的代码将继续循环,要求用户输入,直到他们输入有效数字。这避免了您原来的问题,并且有一个额外的好处,即允许用户在犯错时有机会输入新数字,而不是退出循环并重新开始。
int sumOne = 1;
int sumTwo = 1;
int sumOneTotal = 0;
int sumTwoTotal = 0;
while(sumOne > 0 || sumTwo > 0){
do {
System.out.print("Enter a number to add to first sum: ");
//The user enters in a value for the first sum.
sumOne = input.nextInt();
System.out.print("Enter a number to add to second sum: ");
//The user enters in a value for the second sum.
sumTwo = input.nextInt();
}while(sumTwo <= 0 || sumOne <= 0);
sumOneTotal = sumOneTotal + sumOne;
sumTwoTotal = sumTwoTotal + sumTwo;
}
//We print out the total of sumOneTotal and sumTwoTotal.
System.out.printf("%1s%1d%12s%1s%1d", "First sum: ", sumOneTotal, " ", "Second sum: ", sumTwoTotal);
这是对结构化编程还是新事物的倒退,回到 goto 语句等无处不在的时代。理论上,理想情况下,应该永远不用breaks/continues,只有return的单点。实际上,这样做 可以 通过使程序更难编写、更难阅读并占用更多计算资源,从而使您的工作变得更加困难。多个 returns、continues 和 breaks 是真正结构化编程和意大利面条代码之间的中间人。使用得当,没毛病。
一般来说,我发现如果您已经在使用使代码难以阅读的不良做法(例如,编写大量逻辑块而不分解它,紧密耦合对象),它们只会使您的代码模糊不清等)。
如果您有兴趣,here is a link to an interesting perspective on why NOT to use them. And here 是关于它们为何有益的观点。
许多其他人已经用代码回答了,但这是我的镜头:)
public class Main {
public static void main(String args[]) {
int sumOne = 1;
int sumTwo = 1;
int sumOneTotal = 0;
int sumTwoTotal = 0;
Scanner input = new Scanner(System.in);
while(sumOne > 0 || sumTwo > 0){
System.out.print("Enter a number to add to first sum: ");
sumOne = input.nextInt();
if (is_positive(sumOne)){
sumOneTotal = sum_numbers(sumOneTotal, sumOne);
System.out.print("Enter a number to add to second sum: ");
sumTwo = input.nextInt();
if(is_positive(sumTwo)){
sumTwoTotal = sum_numbers(sumTwoTotal, sumTwo);
}
}
}
System.out.printf("%1s%1d%12s%1s%1d", "First sum: ", sumOneTotal, " ", "Second sum: ", sumTwoTotal);
return;
}
public static int sum_numbers(int x, int y){
int total = x + y;
return total;
}
public static boolean is_positive(int x){
boolean is_pos = true;
if(x < 0){
is_pos = false;
}
return is_pos;
}
}
我会说它现在更难阅读了。我的代码越靠右,我就越为需要维护它的人感到难过。当然,我可以通过在方法中包装(更多)位来删除一两个级别的缩进。然后它变得更容易阅读,但有一点黑盒化每一点逻辑似乎都是多余的......
while (sumOne > 0 && sumTwo > 0) {
System.out.print("Enter a number to add to first sum: ");
sumOne = input.nextInt();
if (sumOne > 0) {
sumOneTotal = sumOneTotal + sumOne;
System.out.print("Enter a number to add to second sum: ");
sumTwo = input.nextInt();
if (sumTwo > 0)
sumTwoTotal = sumTwoTotal + sumTwo;
}
}
但我同意其他人的看法 - 没有任何意义可以避免 "break"
更干净,没有中断。
int candidate = 0;
int [] sums = {0,0};
int index = 1;
System.out.print("Enter a number to add to first sum: ");
while((candidate = input.nextInt()) > 0){
sums[index] = sums[index] + candidate;
index = (index + 1)%2;
System.out.print("Enter a number to add to " + ((index == 0) ? "first":"second" ) + " sum: ");
}
//We print out the totals.
System.out.printf("%1s%1d%12s%1s%1d", "First sum: ", sums[0], " ", "Second sum: ", sums[1]);
这并不是说您应该始终避免中断,但在这种情况下您可以避免中断以使您的代码更短、冗余更少。
如果你把你的代码流程图画出来你可以看到它中间有一个bucle退出,这是不对的,正确的方法是在评估时退出,也有人在评估时退出阅读您的代码,他们应该期望在 while 块内评估为 false 而不是随机的时,bucle 会留下,我获取了您的代码并进行了一些修复以使其按预期工作,但我不确定这是否是您的原因老师期待
int sumOne = 1;
int sumTwo = 1;
int sumOneTotal = 0;
int sumTwoTotal = 0;
while (sumOne > 0 && sumTwo > 0) {
System.out.print("Enter a number to add to first sum: ");
// The user enters in a value for the first sum.
sumOne = input.nextInt();
/**
* We use an if-else statment to ensure sumOne is never less than or
* equal to 0. If it does it ends the program immediately and totals
* the sums. This is because we only want the user to enter in
* positive numbers.
*/
if (sumOne > 0) {
sumOneTotal = sumOneTotal + sumOne;
System.out.print("Enter a number to add to second sum: ");
// The user enters in a value for the second sum.
sumTwo = input.nextInt();
/**
* We use an if-else statment to ensure sumTwo is never less
* than or equal to 0. If it does it ends the program
* immediately and totals the sums. This is because we only want
* the user to enter in positive numbers.
*/
if (sumTwo > 0) {
sumTwoTotal = sumTwoTotal + sumTwo;
}
}
}