while 循环与第三章练习题

while循环与for循环

1.基本框架

① 循环的初始化

while ( ②.循环继续的条件){

③循环体

④循环的步长

}

while(true){

①循环初始化

②循环继续的条件

③循环体

④循环的步长

}

要注意的是:用true写的循环体,在逻辑上是比较顺畅的,但是也是有一定的危险的,主要是因为它本身是一个死循环,需要在一些合适的时候跳出循环。

2、while循环的死循环

我们可以写代码举例如下:

public class Day02091 {

	public static void main(String[] args) {
		while (true) {
			System.out.println("年轻是美好的");
		}

	}

}

我们可以发现这是一个死循环,如果我们不认为的关闭,他会一直运行下去,不会跳出这个循环圈。

3、for循环的死循环

我们可以写代码举例如下:

public class Day02092 {

	public static void main(String[] args) {
		for (; ;) {
			System.out.println("年轻是美好的");
		}

	}

}

我们可以发现这是一个死循环,如果我们不认为的关闭,他会一直运行下去,不会跳出这个循环圈。

需要注意的是:这样的格式只适用于for循环,在while循环中并不适用。

第三章练习题

3.9

代码如下:

import java.util.Scanner;

public class Day39 {

	public static void main(String[] args) {
		//提示用户输入x坐标与y坐标
		System.out.print("Enter a point's x- and y- coordinates :");
		Scanner in = new Scanner(System.in);
		double x = in.nextDouble();
		double y = in.nextDouble();
		if (x > 0 && x <200 && y < -0.5 * x + 100 && y > 0) {
			System.out.println("The point is in the triangle");
		}else {
			System.out.println("The point is not in the triangle");
		}
	}

}

这道题主要就是确定这个三角形的区域 ;if(x > 0 && x <200 && y < -0.5 * x + 100 && y > 0)每一个所指的具体范围如下图所示:

其代码如下:

import java.util.Scanner;

public class Day310 {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		//提示用户输入大矩形的宽度和高度以及大矩形的点坐标
		System.out.print("Enter r1's center x-,y-coordinates,width,and height:");
		
		double x1 = in.nextDouble();
		double y1 = in.nextDouble();
		double w1 = in.nextDouble();
		double h1 = in.nextDouble();
		//提示用户输入小矩形的宽度和高度以及小矩形的点坐标
		System.out.print("Enter r2's center x-,y-coordinates,width,and height:");
		
		double x2 = in.nextDouble();
		double y2 = in.nextDouble();
		double w2 = in.nextDouble();
		double h2 = in.nextDouble();

		double rightmin = (w1 - w2) / 2 + x1 ;
		double leftmin = -(w1 - w2)  / 2-  x1 ;
		double upmin = (h1 - h2) / 2+ y1 ;
		double downmin = -(h1 - h2) / 2- y1 ;
		
		double rightmax = (w1 + w2) / 2 + x1 ;
		double leftmax = -(w1 + w2)  / 2-  x1 ;
		double upmax = (h1 + h2) / 2+ y1 ;
		double downmax = -(h1 + h2) / 2- y1 ;
		
		if (x2 < rightmin && x2 > leftmin && y2 < upmin && y2 > downmin) {
			System.out.print("r2 is inside r1");
		}else if (y2 > rightmax || y2 < leftmax || y2 > upmax || y2 < downmax) {
			System.out.print("r2 is overlaps r1");
		}else {
			System.out.print("r2 don't overlaps r1");
		}
		
	}

}

这个代码输入的数字过多,所以一定要注意输入的顺序。

其中红色的矩形表示的是:小矩形在大矩形内。

蓝色矩形表示的是:小矩形与大矩形不相交。

3.11

代码可以写为:

import java.util.Scanner;

public class Day311 {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.print("请输入一些数字:");//提示用户输入一些数字
		int positives = 0;
		int negatives = 0;
		double sum = 0;
	
		while (true) {
			int num = in.nextInt();//定义输入的数字为num
			if (num != 0) {
				sum += num;
				if (num > 0) {
					positives ++;
				}else {
					negatives ++;
				}
			}else {
				break;
			}
		}
		if (positives + negatives == 0) {
			System.out.print("输入的数字只有零");
		}else {
			System.out.print("整数的个数 :" + positives);
			System.out.print("负数的个数 : " + negatives);
			System.out.print("总和为 : " +sum);
			System.out.print("平均数为 : "+ sum /(positives + negatives));
		}
	}

}

这里需要注意的是:输入的时候必须在while循环了,否则就只能输入一个数字不能继续进行下去了。

因为题中说为0的值不计入运算,因此当这个数字为0时就用break跳出循环区。

其代码如下:

import java.util.Scanner;
public class Day312{
public static void main(String[] args) {
    
		Scanner in = new Scanner(System.in);
		//输入两个数
		System.out.print("请输入两个整数 : ");
		int n1 = in.nextInt();
		int n2 = in.nextInt();
		int gcd = 1;
		for (int k = n1 < n2 ? n1: n2 ;k >= 1;k--) {
			if (n1 % k == 0 && n2 % k == 0) {
				 gcd = k;
				
				break;
			}
		}
		System.out.print(n1 +" 与 "+ n2 + "的最大公约数为 : " + gcd);
	}

}

求两个数的最大公因数时:我们可以先比较出两个数中较小的那个数,将其赋值给min。

因为公因数需要最大的,所以我们可以将它从最大的数字开始循环,直到大于1为止。

求出最大的公约数之后就不用计算其它数值,则直接跳出循环,用break。

代码运行的结果为:

请输入两个整数 : 5 15
5 与 15的最大公约数为 : 5

编写代码如下:

import java.util.Scanner;
class Day313{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        System.out.print("请输入一个数字:");
        int num=scanner.nextInt();
        while(true){
            for(int i=2;i<=num;i++){
                if(num%i==0){
                    System.out.print(i+" ");
                    num=num/i;
                    break;
                }
            }
            if(num==1){
                break;
            }
        }
    }
}

因为输入的数字可能为1,但1只有1 * 1 因此不满足条件。

需要在前面运用while循环让1也包含在这个循环内

其代码如下:

import java.util.Scanner;
class Day314{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        System.out.print("请输入行数:");
        int line=scanner.nextInt();
        for(int i=1;i<=line;i++){
            for(int k=1;k<=(line-i);k++){
                if(line<10){
                    System.out.print("  ");
                }else{
                    System.out.print("   ");
                }
            }
            for(int x=-(i-1);x<=i-1;x++){
                if(line<10){
                    System.out.printf("%-2d",Math.abs(x)+1);
                }else{
                    System.out.printf("%-3d",Math.abs(x)+1);
                }
            }
            System.out.println();
        }
    }
}

其代码如下:

class Day315{
    public static void main(String[] args){
        for(int i=1;i<=6;i++){
            for(int j=1;j<=i;j++){
                System.out.print(j+" ");
            }
            System.out.println();
        }
        System.out.println("==========");
        for(int i=1;i<=6;i++){
            for(int j=1;j<=7-i;j++){
                System.out.print(j+" ");
            }
            System.out.println();
        }
        System.out.println("==========");
        for(int i=1;i<=6;i++){
            for(int k=1;k<=6-i;k++){
                System.out.print("  ");
            }
            for(int j=i;j>=1;j--){
                System.out.print(j+" ");
            }
            System.out.println();
        }
        System.out.println("==========");
        for(int i=1;i<=6;i++){
            for(int k=1;k<=i-1;k++){
                System.out.print("  ");
            }
            for(int j=1;j<=7-i;j++){
                System.out.print(j+" ");
            }
            System.out.println();
        }
    }
}

3.16

其代码如下:

class Day316{
    public static void main(String[] args){
        for(int i=1;i<=8;i++){
            for(int k=1;k<=8-i;k++){
                System.out.print("    ");
            }
            for(int x=-(i-1);x<=i-1;x++){
                System.out.printf("%4d",(int)Math.pow(2,i-1-Math.abs(x)));
            }
            System.out.println();
        }
    }
}

先定义boolean 来确定是否是素数

让i来确定2~1000这些数字,j来做i的因子,如果满足i%j==0就不是素数

最后,别忘记让a的值重新定义为true。

其代码如下:

public class Day317 {

	public static void main(String[] args) {
		boolean a = true;
		int count = 0;
		for (int i = 2 ;i <= 1000;i++) {  	//确定2~1000之间的每个数
			for (int j = 2;j < i;j++) {//2~1000之间每次除的数字,是判断是否是素数
				if (i % j == 0) {
					a = false;
				}
			}
			if (true) {
				count++;
				System.out.print(i+" ");
			}
			a = true;
			if (count % 8 == 0) {
				System.out.println();
			}
			
		}

	}

}

这道题需要先输入i ,然后需要定义一个取反的变量,再把它代入循环即可。

其代码为:

import java.util.Scanner;

public class Day318 {

	public static void main(String[] args) {
		Scanner in  = new Scanner(System.in);
		System.out.print("请输入一个数字 : ");
		int i = in.nextInt();
		double sum = 0.0;
		int k = 1;//取反
		double a = 0.0;
		for (int j = 1;j <= i;j += 2) {
			sum = 1.0 / j * k;
			a += sum;
			k = -k;
		}
		System.out.print(4 * a);
		
	}

}

十进制转换2进制的方法:对输入的这个数一直除以2,直到这个数变成零为止,每次除的余数自下向上的读取就是这个数转换为2进制的结果。

这里运用了一个binStr表示的是直接加上前面的数值

代码如下:

import java.util.Scanner;
class Day323{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        System.out.print("请输入一个数字:");
        int num=scanner.nextInt();
        String binStr="";
        while(true){
            binStr=num%2+binStr;//"1100"
            num/=2;
            if(num==0){
                break;
            }
        }
        System.out.println(binStr);
    }
}

3.24

因为要输入一些数字,切要对这些数字判断最大值。所以将num = in.nextInt();写道循环里面

如果为0,则直接break 跳出这个循环

如果这个数出现第一次,则将count = 1,第二次甚至多次出现,就对这个数出现的次数加一。

其代码如下:

import java.util.Scanner;

public class Day324 {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.print("请输入一些数字");
		int max = 0;//将最大的值赋给max
		int count = 0;//计算最大的数值循环多少次
		
		int num ;
		while (true) {
			num = in.nextInt();
			if (num == 0) {
				break;
			}else if (num > max) {
				max = num;
				count = 1;
			}else if (num == max) {
				count ++;
			}
		}
		System.out.print(max + "有" + count + "个" );

	}

}
发布了11 篇原创文章 · 获赞 0 · 访问量 255

猜你喜欢

转载自blog.csdn.net/yihjh/article/details/104238805