Use the while statement to find the sum of odd numbers and even numbers from 1 to 100.
Specific examples are as follows:
(Free learning video tutorial sharing: java video tutorial)
public class TestWhile{ public static void main(String[] args){ int i = 1; int sum = 0; while(i<100){ sum+=i; i+=2; } System.out.println("1-100的奇数和为sum = "+sum); System.out.println("--------------------------------------- "); int j = 1; sum = 0; while(j <= 100){ if(j%2 == 0){ sum+=j; } j++; } System.out.print("1-100的偶数数和为sum = "+sum); } }
Related article tutorial sharing: Getting Started with Java Tutorial
The above is the detailed content of Use the while statement to find the sum of odd numbers from 1 to 100 in java. For more information, please follow other related articles on the PHP Chinese website!