• 第四讲动手动脑集课后作业


    一、动手动脑

    1、编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数。

    源代码:

    //huzejie20161016
    //生成指定数目的随机数
    import java.util.Scanner;
    public class Suijishu {
    public static void main(String[] args) {
    System.out.print("请输入要输出多少个随机数:");
    Scanner s = new Scanner(System.in);
    int n = s.nextInt();
    int x = (int)(Math.random() * 1000);
    int a = (int)(Math.random() * 1000);
    int c = (int)(Math.random() * 1000);
    int m = (int)(Math.random() * 1000);
    for(int i = 0;i < n;i++)
    {
    x = (a * x + c) % m;
    System.out.print(x + " ");
    }
    }
    }

    程序截图:

    2、查看以下代码你发现了什么吗?

    方法重载要满足两个条件:1、方法名相同。2、参数类型不同,参数个数不同,或者是参数类型的顺序不同。

    二、课后作业

    1、使用递归方法判断某个字符串是否为回文

    源代码:

    //判断输入字符串是否是回文
    import java.util.Scanner;
    public class Huiwen {
    public static void main(String[] args) {
    System.out.print("请输入字符串:");
    Scanner S = new Scanner(System.in);
    String s = S.nextLine();
    if(Panduan(s) == 1)
    System.out.println( "这个字符串是回文!");
    else
    System.out.println( "这个字符串不是回文!");
    }
    public static int Panduan(String str)
    {
    if(str.length() == 0||str.length() == 1)
    return 1;
    else
    {
    char First = str.charAt(0);
    char End = str.charAt(str.length() - 1);
    if(First != End)
    return 0;
    }
    return Panduan(str.substring(1,str.length() - 1));
    }
    }

    运行结果:

  • 相关阅读:
    A 开场白 快速幂
    矩阵快速幂
    围棋棋盘有多少正方形 19*19
    matlab
    汉诺塔问题
    error C2504 base class undefined
    查看标准的FM以及描述等的一个数据…
    报表整合数据的时候,常见的模式:
    SQL执行顺序
    LIST&nbsp;BOX&nbsp;联动的实现
  • 原文地址:https://www.cnblogs.com/liulitianxia/p/5966588.html
Copyright © 2020-2023  润新知