三大循环

本文通过一系列Java编程示例介绍了基础算法应用,包括猜数游戏的实现,计算平均数的方法,整数分解,计算阶乘,判断素数,找最大公约数,以及数列求和。每个示例都详细阐述了算法思路和代码实现,旨在帮助初学者理解编程中的循环、条件判断和数值操作等基本概念。
摘要由CSDN通过智能技术生成

1、猜数游戏

让计算机来想一个数,然后让用户来猜,用户每输入一个数,就告诉它是大了还是小了,直到猜中为止。最后还要告诉用户猜了多少次。

因为需要不断重复让用户猜,所以需要用到循环
在实际写出程序之前,可以先用文字描述程序思路
核心重点是循环的条件
考虑好 循环的终止条件

1、计算机随机想一个数,记在变量number里
2、一个变量count负责记猜测次数,初始化为0
3、让用户输入一个数字a
4、count递增(加1)
5、判断a和number的大小关系:如果a大,就输出“大”,如果a小,就输出“小”
6、如果a和number是不相等的(无论大小),程序都转回到第三步
7、否则,程序输出“猜中”和次数,然后结束
注:循环的条件是a和number不相等

import java.util.Scanner;

public class Main 
{
	public static void main(String[] args)
	{
		Scanner in = new Scanner(System.in);
		int number = (int)(Math.random()*100+1);  //[0,1)-->[0,100)-->[1,100]
		int a = in.nextInt();  //输入的猜测数
		int count = 0;  //猜的次数
		
//		do
//		{
//			a=in.nextInt();
//			count = count + 1;
//			if(a > number)
//			{
//				System.out.println("偏大");
//			}
//			else if(a < number)
//			{
//				System.out.println("偏小");
//			}
//		}while(a != number);
		
//		System.out.println("恭喜你猜对了,你猜了"+count+"次。");
//		不建议用do...while循环,看着晕
		
		
		while(a != number)
		{
			count ++;
			if(a > number)
			{
				System.out.println("偏大");
			}
			else if(a < number)
			{
				System.out.println("偏小");
			}
			a = in.nextInt();
		}
		
		System.out.println("恭喜你猜对了,你猜了"+ (count + 1) +"次。");
		
		
//		for():不太适合用,因为不清楚具体循环的次数
	}
}

2、算平均数

让用户输入一系列的正整数,最后输入-1表示输入结束,用程序计算出这些数字的平均数,输出输入数字的个数和平均数。
变量–>算法–>流程图–>程序

变量
一个变量记录累加的结果,一个变量记录读到的个数
算法
1、初始化变量sum和count为0
2、读入number
3、如果number不是-1,则将number加入sum,并将count加1,回到2
4、如果number是-1,则计算和打印出sum/count(要换为浮点类型来计算)
在这里插入图片描述

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		int number;
		int count = 0;
		int sum = 0;
	
		number = in.nextInt();
		while(number != -1)
		{
			sum = sum + number;
			count = count + 1;
			number = in.nextInt();
		}
		
//		do
//		{
//			number = in.nextInt();
//			if(number != -1) 
//			{
//				sum = sum + number;
//			    count = count + 1;
//			}
//		}while(number != -1);
		
		if(count > 0)
		{
			System.out.println("平均数为:"+(double)sum/count);
			System.out.println("有效输入(输入值参与计算)次数为:"+count);
		}
	}
}

3、整数的分解

一个整数:
对其做%10的操作,可得到它的个位数
对其做 / 10的操作,可去掉它的个位数
然后依此类推,就可得到十位数、百位数等等

例:
352%10=2 得到个位
352 / 10=35 去掉个位
35%10=5 得到十位
35 / 10=3 去掉十位
3%10=3 得到百位
3 / 10=0 去掉百位
分解得到每一位(正序逆序输出),0可以做结束控制

import java.util.Scanner;

public class Main 
{
	public static void main(String[] args) 
	{
		Scanner in = new Scanner(System.in);
		int number;
		number = in.nextInt();
		int result = 0;
		while(number > 0)
		{
			int digit = number % 10;
			result = result*10 + digit;
			System.out.print(digit);
			number = number / 10;
		}
		System.out.println();
		System.out.println(result);
	}
}

运行结果:
700
007
7

有一个小bug:(do...while就没有问题)
0

0

4、阶乘

n ! = 1 * 2 * 3 * … * n
输入n,计算输出 n !

import java.util.Scanner;

public class Main 
{
	public static void main(String[] args) 
	{
		Scanner in = new Scanner(System.in);
		int n;
		n = in.nextInt();
//		int i = 1;
		int factor = 1;  //阶乘的结果
//		while(i <= n)
//		{
//			factor = factor * i;
//			i++;
//		}
		
		for(int i = 1 ; i <= n ; i++)
		{
			factor = factor * i;
		}
		
		System.out.println(factor);
	}
}

运行结果:(越界了!!)
20
-2102132736

5、素数

只能被1和自己整除的数

import java.util.Scanner;

public class Mainn 
{
	public static void main(String[] args) 
	{
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int isPrime = 1;
		
		for(int i = 2 ; i < n ; i++)
		{
			if(n % i == 0)
			{
				isPrime = 0;
				System.out.println(n + "不是素数,i=" + i);
				break;
			}
		}
		
		if(isPrime == 1)
		{
			System.out.println(n + "是素数");
		}
		else
		{
			System.out.println(n + "不是素数");
		}
	}
}
import java.util.Scanner;

public class Mainn 
{
	public static void main(String[] args) 
	{
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		boolean isPrime = true;
		
		for(int i = 2 ; i < n ; i++)
		{
			if(n % i == 0)
			{
				isPrime = false;
				System.out.println(n + "不是素数,i=" + i);
				break;
			}
		}
		
		if(isPrime)
		{
			System.out.println(n + "是素数");
		}
		else
		{
			System.out.println(n + "不是素数");
		}
	}
}

输出100以内的素数

//逻辑运算!!
import java.util.Scanner;

public class Main
{
	public static void main(String[] args) 
	{
		Scanner in = new Scanner(System.in);
		
		for(int x = 2 ; x < 100 ; x++)
		{
			int isPrime = 1;
			for(int i = 2 ; i < x ; i++)
			{
				if(x % i == 0)
				{
					isPrime = 0;
					break;
				}
			}
			
			if(isPrime == 1)
			{
				System.out.print(x + " ");
			}
			else
			{
				
			}
		}
	}
}

输出结果:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 

输出前50个素数

在这里插入代码片

6、凑硬币

如何用1角、2角、5角硬币凑出10元以下的金额呢?

import java.util.Scanner;

public class Mainn 
{
	public static void main(String[] args) 
	{
		Scanner in = new Scanner(System.in);
		int amount = in.nextInt();
		
		OUT:  //标号(跳出多重循环)
		for(int one = 0 ; one <= amount ; one++)
		{
			for(int five = 0 ; five <= amount/5 ; five++)
			{
				for(int ten = 0 ; ten <= amount/10 ; ten++)
				{
					for(int twenty = 0 ; twenty <= amount/20 ; twenty++)
					{
						if(one + five*5 + ten*10 + twenty*20 == amount)
						{
							System.out.println(one+"张1元,"+five+"张5元,"+ten+"张10元,"+twenty+"张20元-->"+amount);
						    break OUT;
						}
					}
				}
			}
		}
	}
}

输出结果:
33
31元,05元,110元,120-->33

7、求和

f(n) = 1 + 1/2 + 1/3 + … + 1/n

import java.util.Scanner;

public class Mainn 
{
	public static void main(String[] args) 
	{
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		double sum = 0.0;
		for(int i=1 ; i<=n ; i++)
		{
			sum += 1.0/i;
		}
		System.out.println(sum);
		System.out.printf("%.2f",sum);  //四舍五入
	}
}

结果:
100
5.187377517639621
5.19

f(n) = 1 - 1/2 + 1/3 - 1/4 + … + 1/n

import java.util.Scanner;

public class Mainn 
{
	public static void main(String[] args) 
	{
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		double sum = 0.0;
		int sign = 1;
		
		for(int i=1 ; i<=n ; i++)
		{
//			sum += sign*1.0/i;
//			sign = -sign;
			if(i%2 == 1)
			{
				sum += 1.0/i;
			}
			else
			{
				sum -= 1.0/i;
			}
		}
		
//		for(int i=1 ; i<=n ; i++,sign = -sign)
//		{
//			sum += sign*1.0/i;
//		}
		
		System.out.println(sum);
		System.out.printf("%.2f",sum);  //四舍五入
	}
}

结果:
100
0.688172179310195
0.69

8、最大公约数

输入:12 18
输出:6

算法:
1、设i为2
2、如果两个数都能被i整除,则记下当前的i
3、i加1后重复第二步,直到i等于两个数中的一个
4、至此,记下的最后一个数即为能同时整除两数的最大公约数

//法一:枚举(计算的次数多,慢)
import java.util.Scanner;

public class Mainn 
{
	public static void main(String[] args) 
	{
		Scanner in = new Scanner(System.in);
		int num1 = in.nextInt();
		int num2 = in.nextInt();
		int result = 1;
		for(int i =2 ; i <= num1 && i<= num2 ; i++ )
		{
			if(num1 % i == 0 && num2 % i == 0)
			{
				result = i;
			}
		}
		System.out.println(num1 +"和"+ num2 + "的最大公约数是:" + result); 
	}
}

算法:
1、如果num2等于0,计算结束,num1就是最大公约数
2、否则,计算num1除以num2的余数,让两数相等,而num2等于那个余数
3、回到第一步
在这里插入图片描述

//法二:辗转相除法
import java.util.Scanner;

public class Mainn 
{
	public static void main(String[] args) 
	{
		Scanner in = new Scanner(System.in);
		int num1 = in.nextInt();
		int num2 = in.nextInt();
		int result = 1;
		int o1 = num1;
		int o2 = num2;
		while(num2 != 0)
		{
			int r = num1 % num2;
			System.out.println(num1 + "," + num2 + "," + r);
			num1 = num2;
			num2 = r;
		}
		System.out.println(o1 +"和"+ o2 + "的最大公约数是:" + num1); 
	}
}

结果:
12 18
12,18,12
18,12,6
12,6,0
1218的最大公约数是:6
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值