1.求1-100之间的偶数之和
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <br> <p>求1-100的偶数之和</p> <br> 值为: <% int a=0; for(int i = 1;i<101;i++){//循环数 if(i%2==0){ a+=i; } } out.print(a);//输出值 %> </body> </html>
2.求1-100之间的素数之和
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <p>求1-100之间的素数之和</p> <br> 值为: <% int s=0; for(int a = 2;a<100;a++){//循环数 int font=1; for(double b = 2;b<a;b++){//循环数之前的所有数 if(a%b==0&&a!=b){//判断条件 font=0; } } if(font!=0){ s+=a; } } out.print(s);//输出值 %> </body> </html>