Java练习题6.2

Java练习题6.2_java
第一部分

1.1计算1+2+3+4+…+99+100

package com.shangjiti.aoian;
public class N01 {
	public static void main(String[] args) {
		int sum=0;
		int i=0;
		while(i<=100)
		{		
			sum=sum+i;
			i++;
	 	}
		System.out.println("1+2+3+...+100="+sum);
	}
}

1.2.计算100以内能被4整除的数的和

package com.shangjiti.aoian;
public class No2 {
	public static void main(String[] args) {
		int sum=0;
		int n=1;
		while(n<=100) 
		{
			if(n%4==0) 
			{
				sum=sum+n;
			}
			n++;
		}
		System.out.println(sum);
	}
}

1.3.计算1*2*3*4*5*6*7*8*9*10(10!)

package com.shangjiti.aoian;
public class No3 {
	public static void main(String[] args) {
		int i=1,sum=1;
		while(i<=10)
		{
		sum*=i;
		i++;
		}
		System.out.println("10!="+sum);
	}
}

1.4.求1-100之间,6的倍数出现的次数

package com.shangjiti.aoian;
public class No4 {
	public static void main(String[] args) {
		int i=1,n=0;
		while(i<=100)
		{
			if(i%6==0)
			n++;
			i++;
		}
		System.out.println(n);
	}
}

1.5.在400–500之间求一个数,它被2除余1,被5除余3,被9除余1,这个数是多少

package com.shangjiti.aoian;
public class No5 {
	public static void main(String[] args) {
		int i=400;
		while(i<=500)
		{
			if(i%2==1&&i%5==3&&i%9==1)
			{
				System.out.println(i);
			}
		i++;
		}
	}
}

1.6.在四位数中找:百位数字是3,十位数字是6,并且它们既能被2整除,又能被3整除,输出这样的四位数。

package com.shangjiti.aoian;
public class No6 {
	public static void main(String[] args) {
		int shi;
		int bai;
		int i=1000;
		while(i<=10000)
		{
				bai=i/100%10;
				shi=i/10%10;
				if(shi==6&&bai==3&&i%6==0)
				{
					System.out.println(i);
				}
		i++;
		}	
	}
}

第二部分

2.1求1+2+3+4+5+6+7+8+9+10;

package com.shangjiti.aoian;
public class No7 {
	public static void main(String[] args) {
		int i=0,sum=0;
		while(i<=10)
		{
			 sum=sum+i;
			 i++;
	 	}	
		System.out.println("1+2+3+...+10="+sum);
	}
}

2.2求1+1/2+1/3+…+1/9+1/10;

package com.shangjiti.aoian;
public class No8 {
	public static void main(String[] args) {
		int i=1;
		double n=0;
		while(i<=10)
		{
			n=n+1.0/i;
			i++;
		}
		System.out.println(n);
	}
}

2.3求1-1/2+2/3-3/4+…+8/9-9/10;

package com.shangjiti.aoian;
public class No9 {
	public static void main(String[] args) {
		double i=1;
		double j=1;
		double s=0;
		while(i<=10)
		{
			if(i==1) 
			{
				s=s+1/i;
			}
			else 
			{
				s=s+j*(i-1)/i;
			}
		j=j*-1;
		i++;
		}
		System.out.println(s);
	}
}

2.4求1+1/2+2/3+3/5+5/8+…;

package com.shangjiti.aoian;
public class No10 {
	public static void main(String[] args) {
			int	i=1;
			double a=1,b=1,t;
			double sum=0.0;
			while(i<=10)
			{
				sum=sum+a/b;
				t=a;
				a=b;
				b=b+t;
				i++;
			}
			System.out.println("1/2+2/3+3/5+.....前十项和为:"+sum);
		}
	}

2.5输入a和n的值,求s=a+aa+aaa+aaaa+aa…a(n个a)的值并输出。例如当n是5,a是2的时候,s=2+22+222+2222+22222;

package com.shangjiti.aoian;
import java.util.Scanner;
public class No11 {
	public static void main(String[] args) {
		System.out.println("输入a和n的值:");
		Scanner sc=new Scanner(System.in);
		int a=sc.nextInt();
		int n=sc.nextInt();
		int sum=0;
		int i=1;
		int s=0;
		while(i<=n)
		{
			s=s*10+a;
			sum=sum+s;
			i++;
		}
		System.out.println("sum="+sum);
	}
}

能力不足,水平有限,如有错误,感谢指正