Java中的随机方法 [英] random method in java

查看:104
本文介绍了Java中的随机方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
我在Java中的数组项目
我想在代码中使用随机数
怎么做

hi
my project of array in java
i wanna to use random number in the code
how can do it

 /*
* This is the sorted class
*/


 

import java.util.*;

public class SortedArray
{
    static Scanner key = new Scanner(System.in);
    static int initialSize = 1;
    static int[] array = new int[initialSize];
    static int index = 1;
    static int getElement;
    static int delElement;


    public static void getArrayElement()
    {

        try
        {
            if(initialSize==1)
            {
                //Get the user input
                System.out.print("Enter the element: ");
                getElement = key.nextInt();

                //Assign the user input to the array
                for(int i=0; i<index; i++)
                {
                    array[i] = getElement;
                }

            }

            //If the size of the array is not 1 use this
            else
            {
                //Gets the user input
                System.out.print("Enter the element: ");
                getElement = key.nextInt();

                //Create a new empty array with a new size
                int[] temp = new int[index];

                //Assign the old array into the new array
                for(int j = 0; j < index-1; j++)
                {
                    temp[j] = array[j];
                }

                //Change the size of the old array
                array = new int [index];

                //Assign the temporary array into the new array with its new size
                for(int aSize = 0; aSize< array.length; aSize++)
                {
                    array[aSize] = temp[aSize];
                    int k = array.length;
                    array[k-1] = getElement;
                }

                //Pass the array into sortArray method for sorting
                sortArray(array, index);

             }

                //Increment the index and initialSize
               index++;
               initialSize++;
          }
          catch(InputMismatchException e)
         {
                System.out.println("Invalid Input");
                System.exit(0);
         }

      }

    //This is a bubble sort that sorts the array
    public static void sortArray(int a[], int size)
    {

        for(int sortSize = 0; sortSize < size-1; sortSize++)
        {
            int temp_number;

            for(int secondSize = (sortSize+1); secondSize < size; secondSize++)
            {
                if(a[sortSize] > a[secondSize])
                {
                    temp_number = a[sortSize];
                    a[sortSize] = a[secondSize];
                    a[secondSize] = temp_number;
                }
            }

        }

    }

    //Gets the sorted array
    public static int[] getArray()
    {
        return array;
    }

    public static void printArray()
    {
        int getSize = 0;
        //Outputs the sorted array
        for(int get =0; get < getArray().length; get++)
        {
            int[] tempArray = getArray();
            System.out.println(tempArray[get]);
            getSize++;

        }
            System.out.print("Size: " + getSize + "\n");
    }

    //Empty the array
    public static void clear()
    {

        index = 1;
        initialSize = 1;
        array = new int[initialSize];
        System.out.println("The array is empty now");
    }

    /*
    * This is the find method
    * If the target is found then delete the target in the array
    * else let the user know that target is not found
    */
    public static void search(int dE)
    {
        int target = dE;
        Boolean found = false;

        for(int fValue = 0; fValue < array.length; fValue++)
        {

                if(array[fValue]==target)
                {
                        for(int rElement = fValue; rElement < array.length-1; rElement++ )
                        {
                                array[rElement] = array[rElement+1];
                        }

                        int[] temp_Dele = new int[index-1];

                        for(int tempD = 0; tempD < index-1; tempD++)
                        {
                                temp_Dele[tempD] = array[tempD];
                        }

                        index--;
                        array = new int[temp_Dele.length-1];

                        for(int aDele = 0; aDele < array.length; aDele++)
                        {
                                array[aDele] = temp_Dele[aDele];
                        }

                    found = true;

                }


        }


        if(!found)
        {
                System.out.println("Element Not Found");
         }


    }

    //Delete the elements inside the array
    public static void deleteArray()
    {

            System.out.print("Enter the element to be delete:");
            delElement = key.nextInt();

            search(delElement);

    }

    //Printing the the smallest element of the array
    public static void smallest()
    {
            System.out.println(array[0]);
    }

    //Printing the largest element of the array
    public static void largest()
    {
            int lastIndex = array.length;

            //Printing the last element of the array
            System.out.println(array[lastIndex-1]);
    }

}


import java.util.*;

public class IntDriver extends SortedArray
{
    public static void main(String[] args) 
    {

        // Assigning Scanner to key
        Scanner key = new Scanner(System.in);

        //Holds the user selection
        int getSelection;

        /*
        * Asking the user for an input selection
        */
        try
        {
            do
            {

                  System.out.print("\n1) Insert 2) Delete 3) Clear 4) Smallest ");
                  System.out.print("5) Largest 6) Exit");

                  System.out.print("\nPlease enter your selection: ");
                  getSelection = key.nextInt();

                  if(getSelection == 1)
                 {
                    getArrayElement();
                    printArray();
                }

                if(getSelection == 2)
                {
                    deleteArray();
                    printArray();
                }
                if(getSelection == 3)
               {
                    clear();
                }
                if(getSelection == 4)
                {
                    smallest();
                }
                if(getSelection == 5)
                {
                    largest();
                }

            }while (getSelection != 6);
        }
      catch(InputMismatchException ae)
     {
         System.out.println("Invalid Input");
     }
  }

}


以及如何计算运行时间?


and how can calculate the running time ?

推荐答案

请参见

尝试
Java随机数 [ http://www.cafeaulait.org/course/week4/40.html [ ^ ]
http://leepoint.net/notes-java/algorithms/random/random-api.html [^ ]
Try
Java random numbers[^]
http://www.cafeaulait.org/course/week4/40.html[^]
http://leepoint.net/notes-java/algorithms/random/random-api.html[^]


这篇关于Java中的随机方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆