2020.8.2

学习内容:

1、编写函数,使用函数重载,能求两个整数的最大数、三个整数的最大数、四个整数的最大数

import java.util.Scanner;

public class Study {
    public static void max(int a[], int n) {
        int max = a[0];
        for (int i = 0; i < n; i++) {
            if (a[i] > max) {
                max = a[i];
            }
        }
        System.out.println(max);
    }

    // 求两个整数的最大值
    public static void max(int a, int b) {
        int arr[] = { a, b };
        max(arr, 2);
    }

    // 求三个整数的最大值
    public static void max(int a, int b, int c) {
        int arr[] = { a, b, c };
        max(arr, 3);
    }

    // 求四个整数的最大值
    public static void max(int a, int b, int c, int d) {
        int arr[] = { a, b, c, d };
        max(arr, 4);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a1, a2;
        int b1, b2, b3;
        int c1, c2, c3, c4;
        System.out.print("请输入两个整数:");
        a1 = in.nextInt();
        a2 = in.nextInt();
        System.out.print("最大数:");
        max(a1, a2);
        System.out.print("请输入三个整数:");
        b1 = in.nextInt();
        b2 = in.nextInt();
        b3 = in.nextInt();
        System.out.print("最大数:");
        max(b1, b2, b3);
        System.out.print("请输入四个整数:");
        c1 = in.nextInt();
        c2 = in.nextInt();
        c3 = in.nextInt();
        c4 = in.nextInt();
        System.out.print("最大数:");
        max(c1, c2, c3, c4);
    }
}

 

 2、判断101~200之间有多少个素数,并输出所有素数

package helloworld;
import java.util.Scanner;
public class study{
    public static void main(String[] args){
        int i,j,m,n,x;
        m=0;n=0;x=0;
        for(i=101;i<=200;i++) {
            for(j=1;j<=i;j++) {
                n=i%j;
                if(n==0)
                {m=m+1;}
            }
            if(m==2) {
                System.out.print(i+" ");
                x=x+1;
        }
            m=0;
    }
        System.out.println();
        System.out.println("在101~200之间一共有素数:"+x+"个");
    }
}

 

 

遇到的问题:

第一题中,每次按完回车后再输入,光标都在文字前面,不知道是什么原因。

 

posted @ 2020-08-02 09:05  第厘  阅读(133)  评论(0)    收藏  举报