Java训练二

一、斐波那契数列

1、1、2、3、5、8、13、21、34、...是一组典型的斐波那契数列,前两个数相加等于第三个数。那么请问这组数中的第n个数的值是多少?

package haha;
import java.util.Scanner;
public class helloworld{
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		System.out.print("第几个数:");
		int num=sc.nextInt();
		if(num<1) {
			System.out.println("无效数字!");
		}else if(num==1 || num==2) {
			System.out.println("1");
		}else {
			int a=1,b=1,c=0;
			for(int i=0;i<(num-2);i++) {
				c=a+b;
				a=b;
				b=c;
			}
			System.out.println(c);
		}
	}
}

二、判断奇偶数

编写Java程序,实现判断变量x是奇数还是偶数。

package haha;
import java.util.Scanner;
public class helloworld{
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		System.out.print("请输入一个数:");
		int a=sc.nextInt();
		if(a%2==0) {
			System.out.println(a+"是偶数");
		}else {
			System.out.println(a+"是奇数");
		}
	}
}

三、输出菱形

编写Java程序,使用for循环打印出菱形。

package haha;
public class helloworld{
	public static void main(String[] args) {
		for(int i=1;i<=9;i++) 
		{
			int a=2*i-1;
			int b=9-i;
			for(int j=0;j<b;j++)
			{
				System.out.print(" ");
			}
			for(int k=1;k<=a;k++)
			{
				System.out.print("*");
			}
			System.out.print("\n");
		}
		for(int i=1;i<=8;i++)
		{
			int a=17-2*i;
			int b=i;
			for(int j=1;j<=b;j++)
			{
				System.out.print(" ");
			}
			for(int k=1;k<=a;k++)
			{
				System.out.print("*");
			}
			System.out.print("\n");
		}
	}
}

四、计算1~20的阶乘的倒数之和

编写Java程序,使用while循环语句计算1+1/2!+1/3!+...+1/20!之和。

package haha;
public class helloworld{
	public static void main(String[] args) {
		int a=1;
		double b=0;
		while(a<=20)
		{
			double c=1;
			for(int i=1;i<=a;i++)
			{
				c=c*i;
			}
			b=b+1/c;
			a=a+1;
		}
		System.out.println(b);
	}
}

五、查找素数

使用for循环,判断1~100有多少个素数,并在控制台上输出所有的素数。

package haha;
public class helloworld{
	public static void main(String[] args) {
		int n=0;
		for(int i=1;i<=100;i++)
		{
			if(i==1)
			{
				n=n+1;
				System.out.print(i+" ");
			}
			else
			{
				int a=0;
				for(int j=1;j<=i;j++)
				{
					if(i%j==0 && j!=1 && j!=i)
					{
						a=a+1;
					}
				}
				if(a==0)
				{
					n=n+1;
					System.out.print(i+" ");
				}
			}
		}
		System.out.println("\n"+n);
	}
}

 六、摄氏度转华氏度

使用do...while循环,在控制台输出摄氏温度和华氏温度的对照表,对照表从摄氏温度-30℃~50℃,每行间隔10℃。

package haha;
public class helloworld{
	public static void main(String[] args) {
		int a=-30;
		do
		{
			double b=a*1.8+32;
			System.out.println("摄氏温度:"+a+"℃    华氏温度:"+b+"℉");
			a=a+10;
		}while(a<=50);
	}
}

七、百钱买百鸡

5文钱可以买一只公鸡,3文钱可以买1只母鸡,1文钱可以买3只雏鸡,现在用100文钱买100只鸡。那么公鸡、母鸡、雏鸡各有多少只?

package haha;
public class helloworld{
	public static void main(String[] args) {
		int a=100/5;
		int b=100/3;
		int c=3*100;
		double money=0;
		Loop:for(int i=0;i<=a;i++)
		{
			for(int j=0;j<=b;j++)
			{
				for (int k=0;k<=c;k=k+3)
				{
					money=i*5+j*3+k/3;
					if(money==100 && i+j+k==100)
					{
						System.out.println("公鸡:"+i+"只");
						System.out.println("母鸡:"+j+"只");
						System.out.println("雏鸡:"+k+"只");
						break Loop;
					}
				}
			}
		}
	}
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天下弈星~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值