简体   繁体   English

用java编写一个程序来打印从50到250(包括50和250)的所有3的倍数且不能被9整除的数字之和

[英]Write a program in java to print the sum of all numbers from 50 to 250(inclusive of 50 and 250) that are multiples of 3 and not divisible by 9

/* when I run this code there is no error in fact output generated is also correct but I want to know what is the logical error in this code? /* 当我运行这段代码时没有错误,实际上生成的输出也是正确的,但我想知道这段代码中的逻辑错误是什么? please can any one explain what is the logical error.请任何人解释什么是逻辑错误。 */ */

class abc
    {
        public static void main(String arg[]){
        int sum=0;
        //for-loop for numbers 50-250
        for(int i=50;i<251;i++){
            // condition to check if number should be divided by 3 and not divided by 9 
            if(i%3==0 & i%9!=0){
                //individual number which are selected in loop
                System.out.println(i);
                //adding values of array so that total sum can be calculated
                sum=sum+i;   
            }   
        }
        //final display output for the code 
        System.out.println("the sum of intergers from 50 to 250 that are multiples of 3 and not divisible by 9 \n"+sum);
    }
}

My philosophy is "less code == less bugs":我的理念是“更少的代码 == 更少的错误”:

int sum = IntStream.rangeClosed(50, 250)
    .filter(i -> i % 3 == 0)
    .filter(i -> i % 9 != 0)
    .sum();

One line.一条线。 Easy to read and understand.易于阅读和理解。 No bugs.没有错误。

Change this:改变这个:

if(i%3==0 & i%9!=0){

to this:对此:

if(i%3==0 && i%9!=0){

& = bitwise and operator & = 按位和运算符

&& = logical operator && = 逻辑运算符

Difference between & and && in Java? Java中&和&&的区别?

The only problems I saw were:我看到的唯一问题是:

  • The variable sum was undeclared变量sum未声明
  • Use && in place of &使用&&代替&

int sum = 0;
for (int i = 50; i <= 250; i++) {
    if (i % 3 == 0 && i % 9 != 0) {
        System.out.println(i);
        sum = sum + i;
    }
}
System.out.println("the sum of intergers from 50 to 250 that are multiples of 3 and not divisible by 9 \n" + sum);

Well, instead of touching every single value from 50 to 250 like you would do here for(int i=50;i<251;i++) , you can consider something like this...好吧,与其像在这里for(int i=50;i<251;i++)那样触及从 50 到 250 的每一个值,你可以考虑这样的事情......

int i = 48;
int sum = 0;
while(i < 250) {
    i += 3;
    if(i%9 != 0)
        sum += i;
}

This is somewhat optimized in the sense that I am skipping over values that I know are not possible candidates.这在某种意义上进行了优化,因为我跳过了我知道不可能的值。

But, there is a much bigger issue in your code.但是,您的代码中有一个更大的问题。 The following code block prints true , sure.下面的代码块打印true ,当然。 But, it is a bad idea to depend on the & since that is not its job.但是,依赖&是一个坏主意,因为这不是它的工作。 The & is for bitwise AND whereas the && is for logical AND, which is what you are trying to do. &用于按位与,而&&用于逻辑与,这就是您想要做的。

boolean t = true;
boolean f = false;
System.out.println(f&t);

Why?为什么?

In Java, if it is a && operation, as soon as you find the first false , you are sure that the expression will evaluate to false.在 Java 中,如果是&&操作,只要找到第一个false ,就可以确定表达式的计算结果为 false。 Meanwhile, in your implementation, it would need to evaluate both sides.同时,在您的实施中,需要评估双方。 f&t will evaluate to false, but the JVM would need to look at both the f and t variables. f&t将评估为 false,但 JVM 需要查看ft变量。 Meanwhile, on using && , it wouldn't even need to look at the t .同时,在使用&& ,它甚至不需要查看t

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM