Dyl*_*men 6 java break while-loop
所以我的教授提到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);
Run Code Online (Sandbox Code Playgroud)
基本上我希望用户输入任何正数,并将该数字添加到第一个或第二个总和.一旦用户输入任何数字<= 0,我希望程序立即停止.当我修补代码时,我一直存在的问题是代码一直在运行.意思是如果我让用户输入0以添加到第一个总和,则代码仍然要求用户输入第二个总和的数字.我需要它立即停止而不是继续.任何帮助都将是一个很大的帮助!我正在使用Java.
编辑!!!所以假设我假设我想制作一个程序,它完成了我现在正在做的完全相同的事情,只是没有break语句.我该怎么办?一些规则.最外层的语句必须是"while"循环.它的内部运作可以是任何东西.我还需要机器打印出"输入一个数字以添加到第一个总和:"和"输入一个数字以添加到第二个总和:"交替.所以,如果我进入1,2,3,4.第一个总和是4,第二个总和是6.最后一个规则是它不能包含任何break语句!
当结构化编程是一个新事物时,这是一个回归,当goto语句等到处时.理论上,理想情况下,你永远不应该使用休息/继续,只有一个回报点.实际上,通过使程序更难编写,更难阅读以及占用更多计算资源,这样做可能会使您的工作变得更加困难.真正的结构化编程和意大利面条代码之间的多重回报,继续和休息是中间人.使用得当,它们没有任何问题.
一般来说,我发现如果你已经使用了使你的代码难以阅读的不良做法,它们只会模糊你的代码(例如,编写大量的逻辑块而不会破坏它,紧密耦合对象等).
如果您有兴趣,可以在此处找到有关为何不使用它们的有趣视角的链接.而这里是他们为什么benefical的透视.
许多其他人已经回答了代码,但这是我的镜头:)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我想说现在更难阅读.我的代码开始被吸引到越多,我越觉得对谁需要维护它感到遗憾..当然,我可以通过在方法中包装(更多)位来删除一两级缩进.然后它变得更容易阅读,但有一点,黑色拳击每一小块逻辑似乎多余...
小智 2
如果你绘制代码的流程图,你可以看到它在中间退出一个 bucle,这是不正确的,正确的方法是让它在 while 评估时退出,而且当有人阅读你的代码时他们应该退出期望在 while 上评估为 false 时留下 bucle,而不是随机如果在 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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
183 次 |
| 最近记录: |