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

    运行结果:

  • 相关阅读:
    游戏活动分析
    移动端页面设计指南
    20条开发AIR Native Extension的建议
    Feathers UI 扩展实例 For Starling Framework
    优化 Flash 性能 Flash 开发中心
    简单的不雅词语过滤类
    简单TSql备份所有数据库
    列出Server上5张最大的表
    Asp.net MVC RTM1.0使用NUnit做测试项目
    使用SingleTagSectionHandler实现简单配置节
  • 原文地址:https://www.cnblogs.com/liulitianxia/p/5966588.html
Copyright © 2011-2022 走看看