zoukankan      html  css  js  c++  java
  • 动手动脑

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

    源代码:

    package lianxi;

    import java.util.*;

    public class Suijishu {

       public static int n;

       public static void main(String[] args) {

          // TODO 自动生成的方法存根

          System.out.println("请输入要生成的随机数个数:");

          Scanner sc=new Scanner(System.in);

          n=sc.nextInt();

          Suijishu A=new Suijishu();

          A.creat();

       }

       public void  creat()

       {

          Random rand=new Random();

          int[] a=new int[n];

          for(int i=0;i<n;i++)

          {

             int x=rand.nextInt(100);//随机产生1——100的 一个种子

             int seed=(7^5*x+0)%(2147483647-1);

             System.out.print(seed+" ");

          }

       }

    }

     截图:

    2.请看以下代码,发现了什么特殊之处?

    答:都运用了函数调用,传递形参的方式,两个函数名相同,但是函数传递的参数类型不同。

    3.写n的阶乘的程序

    源代码:

    package lianxi;

    import java.util.*;

    public class jiecheng {

       public static int n,result;

       public static void main(String[] args) {

          System.out.println("输入正整数n");

          Scanner sc=new Scanner(System.in);

          n=sc.nextInt();

            jiecheng A=new jiecheng();

            result=A.jisuan(n);

            System.out.println("n!="+result);

       }

       public int jisuan(int x)

       {

          if(x==0||x==1)

          return 1;

          else

             return x*jisuan(x-1);

       }

    }

     截图:

  • 相关阅读:
    【Thinking in Java, 4e】初始化与清理
    【Thinking in Java, 4e】控制流程执行
    【Beginning Python】抽象(未完)
    【Python】装饰器 & 偏函数
    【c++ primer, 5e】函数声明 & 分离式编译
    【Python】闭包 & 匿名函数
    【c++ primer, 5e】【函数基础】
    【Python】高阶函数
    变相的取消Datagridview控件的选中状态
    NotifyICon控件使用
  • 原文地址:https://www.cnblogs.com/zzh2019979439/p/7660581.html
Copyright © 2011-2022 走看看