所以我的教授提到if / if-else语句的中断是“坏”代码。 她到底是什么意思?我怎么能修复我目前编写的代码,因为它确实按照我想要的方式工作,现在我需要获取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);
基本上我希望用户输入任意正数,并将该数字添加到第一个或第二个总和。一旦用户输入任何数字&lt; = 0,我希望程序立即停止。当我修补代码时,我一直存在的问题是代码一直在运行。意思是如果我让用户输入0以添加到第一个总和,则代码仍然要求用户输入第二个总和的数字。我需要它立即停止而不是继续。任何帮助都将是一个很大的帮助!我正在使用Java。
EDIT !!!所以假设我假设我想制作一个程序,它完成了我现在正在做的完全相同的事情,只是没有break语句。我该怎么办?一些规则。最外层的语句必须是“while”循环。它的内部运作可以是任何东西。我还需要机器打印出“输入一个数字以添加到第一个总和:”和“输入一个数字以添加到第二个总和:”交替。所以,如果我进入1,2,3,4。第一个总和是4,第二个总和是6.最后一个规则是它不能包含任何break语句!
答案 0 :(得分:6)
这是对结构化编程是一件新事物的回归,当goto语句等无处不在时。理论上,理想情况,你永远不必使用休息/继续,只有一个回报点。实际上,这样做可以通过使程序更难编写,更难阅读和占用更多计算资源来使您的工作更加困难。真正的结构化编程和意大利面条代码之间的多重回报,继续和休息是中间人。使用得当,它们没有任何问题。
通常,我发现如果你已经使用了使你的代码难以阅读的不良做法,他们只会模糊你的代码(例如,编写大量的逻辑块而不会破坏它,紧密耦合对象等)
如果您感兴趣,here是一个关于为什么不使用它们的有趣视角的链接。 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;
}
}
我想说现在更难阅读。我的代码开始被吸引到越多,我越觉得对谁需要维护它感到遗憾..当然,我可以通过在方法中包装(更多)位来删除一两级缩进。然后它变得更容易阅读,但有一点,黑色拳击每一小部分逻辑似乎多余......
答案 1 :(得分:2)
如果你绘制代码的流程图,你可以看到它在它的中间退出了一个bucle,这是不正确的,正确的方法是当它被评估时退出,当有人正在读你的时候代码他们应该期望bucle在评估为false时保留,而不是随机,如果在while块内,我接受了你的代码并做了一些修复使其按预期工作但我不确定这是否是你老师所期望的
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;
}
}
}
答案 2 :(得分:1)
更清洁,没有休息。
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]);
并不是说你应该总是避免休息,但在这种情况下,你可以避免它让你的代码更短,更少冗余。
答案 3 :(得分:0)
有时避免休息比使用它更糟糕。我会这样写一下,少休息一下。
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;
}
你可以避免休息,但这并不能使代码更清晰/更简单恕我直言。
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);
同样的建议适用于使用标签。尽可能避免使用它们,除非避免它们意味着做更糟糕的事情。
答案 4 :(得分:0)
我并非同意在break
内使用if
一直是不好的做法。然而,这更多的是意见问题而不是任何事情,并不是真正的话题。我将回答您关于主题的问题部分,即:如何修复我的代码,以便在break
内不使用if
。
下面的代码将继续循环,询问用户输入,直到他们输入有效数字。这可以避免您的原始问题,并且还有一个额外的好处,即如果用户犯了错误就可以输入新号码,而不是退出循环并重新开始。
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);
答案 5 :(得分:0)
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;
}
}
但我同意其他人的意见 - 没有任何意义可以避免&#34;打破&#34;