使用循环的简单程序(Java 入门)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19802402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple program using loops (Beginning Java)
提问by user2958597
I have to write a program using loops that calculates the sum of all odd numbers between a and b (inclusive), where a and b are inputs.
我必须使用循环编写一个程序来计算 a 和 b(含)之间所有奇数的总和,其中 a 和 b 是输入。
I made this (below) and it works fine, but I noticed one problem with it: when i enter a larger number followed by a smaller number for the inputs, it returns 0, but when i enter the smaller number first it works perfectly. Any quick fixes for this? :)
我做了这个(如下)并且它工作正常,但我注意到它有一个问题:当我输入一个更大的数字然后输入一个较小的数字时,它返回0,但是当我首先输入较小的数字时,它完美地工作。有什么快速解决办法吗?:)
import java.util.Scanner;
public class ComputeSumAAndB
{
public static void main (String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Please enter 2 integers: "); //prompts user for ints
int a = in.nextInt();
int b = in.nextInt();
int sum = 0;
for (int j = a; j <= b; j++)
{
if (j % 2 == 1)
sum += j;
}
System.out.println("The sum of all odd numbers (inclusive) between " + a + " and "+ b + " is " + sum);
}
}
回答by nhgrif
int temp;
if(a > b) {
temp = a;
a = b;
b = temp;
}
Put this right before your for
loop starts.
把它放在你的for
循环开始之前。
The if
checks whether a
(the first number entered) is larger than b
. If it is, it swaps a
and b
. Now your for
loop will always start with the smallest number and iterate up to the larger number (because after this if
, a
will always be the smaller number).
该if
是否检查a
(输入的第一数目)大于b
。如果是,则交换a
和b
。现在您的for
循环将始终从最小的数字开始并迭代到更大的数字(因为在此之后if
,a
将始终是较小的数字)。
Using this method has the added side effect of making your output make sense. Your output will now always say: "between [smaller number] and [larger number]"
.
使用这种方法有额外的副作用,使您的输出有意义。您的输出现在总是会说:"between [smaller number] and [larger number]"
。
rolfl's answer is more elegant and works perfectly fine, but when the user enters the larger number first, your output may look kind of weird: "between [larger number] and [smaller number]"
, etc.
rolfl 的答案更优雅,工作得很好,但是当用户首先输入较大的数字时,您的输出可能看起来有点奇怪:"between [larger number] and [smaller number]"
等。
回答by Logan Cunningham
It's not working because Ais larger than Bin the forloop, you have it iterate while Ais less than or equal to B.
它不起作用,因为在for循环中A大于B,当A小于或等于B时,你让它迭代。
You could do what nhgrif says but it's changing your data.. But he is correct.
你可以按照 nhgrif 所说的去做,但它正在改变你的数据......但他是对的。
回答by rolfl
You can get the smaller and larger inputs by using the Math.min()and Math.maxfunctions....
您可以通过使用Math.min()和Math.max函数来获得更小和更大的输入......
for (int j = Math.min(a,b); j <= Math.max(a,b); j++) {
if (j % 2 == 1) {
sum += j;
}
}
回答by Christian
That's because you are first expecting for the a
input (inferior limit) and then the b
(superior). When your program reaches the for
, j = a
so the condition a <= b
is False, if the first input is larger. In other words it never enters the for
loop.
那是因为您首先期望a
输入(下限),然后是b
(上级)。当你的程序到达for
,j = a
所以条件a <= b
为False,如果第一输入较大。换句话说,它永远不会进入for
循环。
回答by MouseLearnJava
Actually you should do the following 2 things:
其实你应该做以下两件事:
1 It is just just like rolflmentioned above. You need to put the min and max in the right place in loop.
1 就像上面提到的rolfl一样。您需要将最小值和最大值放在循环中的正确位置。
for (int j = Math.min(a,b); j <= Math.max(a,b); j++) {
for (int j = Math.min(a,b); j <= Math.max(a,b); j++) {
if (j % 2 == 1) {
sum += j;
}
}
}
2 Use if (j % 2 == 1)it is not enough to check whether the num is odd.
2 使用if (j % 2 == 1)不足以检查 num 是否为奇数。
e.g.
例如
a = -5, b =0;What will be the result?
a = -5, b =0; 结果会怎样?
int sum = 0;
for(int j=-5;j<0;j++)
{
if(j%2 == 1)
{
sum+=j;
}
}
The value for sum will be 0.
sum 的值为 0。
We need to change the condition to if(!(j%2 == 0))Then you will get the expected result.
我们需要把条件改成if(!(j%2 == 0))那么就会得到预期的结果。
回答by Jeffery Yuan
That's because you are first expecting for the a input (inferior limit) and then the b (superior). When your program reaches the for, j = a so the condition a <= b is False, if the first input is larger. In other words it never enters the for loop.
那是因为您首先期望输入 a(下限),然后是 b(上限)。当您的程序到达 for, j = a 时,如果第一个输入较大,则条件 a <= b 为 False。换句话说,它永远不会进入 for 循环。