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

    运行结果:

  • 相关阅读:
    Linux五种IO模型
    怎样理解阻塞非阻塞与同步异步的区别?
    .NET 框架 (转载)
    数组 反转
    排序 归并排序&逆序对
    快速寻找满足条件的2个数
    数组 寻找最大的第k个数
    字符串 删除字符串开始以及末尾的空白符,并把数组中间的多个空格(如果有)符转换为1个
    排序 快速排序
    java8常用api
  • 原文地址:https://www.cnblogs.com/liulitianxia/p/5966588.html
Copyright © 2011-2022 走看看