zoukankan      html  css  js  c++  java
  • 第四讲动手动脑集课后作业

    一、动手动脑

    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));
    }
    }

    运行结果:

  • 相关阅读:
    Delphi XE5 图解为Android应用制作签名
    Delphi事件的广播 转
    Delphi XE6 Android拨号函数
    Firemonkey里触发home按键被按下的事件
    如何处理App的Application的事件
    SQL SERVER存储过程中使用事务与捕获异常
    关于蓝牙打印机的开发
    图标大全网站
    提高Android和iOS调试编译速度
    百万级数据查询优化(数据库)
  • 原文地址:https://www.cnblogs.com/liulitianxia/p/5966588.html
Copyright © 2011-2022 走看看