• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
戈瑾
博客园    首页    新随笔    联系   管理    订阅  订阅
Java入门——day30
练习1

一、今日练习

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之间有多少个素数,并输出所有素数

 1 package helloworld;
 2 import java.util.Scanner;
 3 public class study{
 4     public static void main(String[] args){
 5         int i,j,m,n,x;
 6         m=0;n=0;x=0;
 7         for(i=101;i<=200;i++) {
 8             for(j=1;j<=i;j++) {
 9                 n=i%j;
10                 if(n==0)
11                 {m=m+1;}
12             }
13             if(m==2) {
14                 System.out.print(i+" ");
15                 x=x+1;
16         }
17             m=0;
18     }
19         System.out.println();
20         System.out.println("在101~200之间一共有素数:"+x+"个");
21     }
22 }

 

 二、遇到问题

第一题编写的函数重载运行时报错。

posted on 2020-08-04 21:37  戈瑾  阅读(111)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3