Home > Back-end >  JAVA programming problem
JAVA programming problem

Time:10-12

JAVA programming calculation can be divided exactly by 3 or within 100 can be divided exactly by 7 and the number of those,

CodePudding user response:

The problem is the simplest one by one try, qualified, put together, but I feel more simple way,

CodePudding user response:

Simple method is 3 and 7 is the least common multiple of 21, so start from 21st to 21 increases, the number of 100 conforms to this condition within can
int sum=0;
For (int I=21; i<101; I +=21)
The sum +=I;
System.out.println(sum);

CodePudding user response:

System. Out.println (IntStream rangeClosed (1, 100). The filter (value - & gt; The value % 3==0 | | value % 3==0). The sum ());

CodePudding user response:

One line of code,,,,

CodePudding user response:

One of the 3 to 7

CodePudding user response:

java8 flow in the form of
Public class Test {
Public static void main (String [] args) {
Int sum=IntStream. RangeClosed (1, 100). The filter (value - & gt; The value % 3==0 | | value % 7==0). The sum ();
System. The out. Println (" sum="+ sum);//output and

Int [] ints.=IntStream rangeClosed (1, 100). The filter (value - & gt; The value % 3==0 | | value % 7==0). The toArray ();
System. The out. Println (Arrays. ToString (ints));//output all conform to the values
}
}
  • Related