随机数和阵列搜索 [英] Random number and array search

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

问题描述

我试图产生附加单个随机数,并创建将有三个参数=整数数组,该数组的大小,并且将在阵列中被搜索的值的另一方法。它会遍历数组搜索和计算值多少倍当时found.It将打印出的价值有多少次发现或价值没有被发现。我有点失落,这是我迄今为止产生的随机序列并打印/询问用户是否要重新启动。在此先感谢

我的code:

 进口java.util.Scanner中;
进口了java.util.Random;
类主要{
公共静态最后随机RND_GEN =新的随机();
公共无效createNum(INT [] randomNumbers){的for(int i = 0; I< randomNumbers.length;我++){
    randomNumbers [I] = RND_GEN.nextInt(10)+ 1;
}
}公共无效printNum(INT [] randomNumbers){
的for(int i = 0; I< randomNumbers.length;我++){
    的System.out.println(号码+ I +:+ randomNumbers [I]);
}
}公共无效的run(){
扫描仪inputReader =新的扫描仪(System.in);
INT X = 1;
做{
    INT []号= INT新[20];
    createNum(数);
    printNum(数);
    System.out.print(重新启动程序?对NO输入1选择YES,2:);
    X = inputReader.nextInt();
}而(X == 1);
}公共静态无效的主要(字串[] args){
主要走=新的Main();
去跑步();
}
}


解决方案

我不应该告诉你整个code,因为这看起来像一个家庭作业的问题。

所以这里的提示:


  • 创建一个额外的随机数 - 不那么难,你有 RND_GEN

      INT RN = RND_GEN.nextInt(10)+1; //完全一样,你已经做了。


  • 创建了三个参数....下面是方法头的方法:

     无效搜索(INT []数组,诠释大小,INT VAL)


  • 方法的身体?你有各种各样的搜索算法在那里。一些最流行的是:


所以只是去,研究 学习自己做

I am attempting to generate an ADDITIONAL single random number and create another method that will have three parameters = the integer array, the size of the array, and the value it will be searching for in the array. It will search through the array and count how many times the value was found.It will then either print out how many times the value was found or that the value was not found. I am a bit lost and this is what I have so far which generates the random array and prints it/ asking the user if they want to restart. Thanks in advance

My code:

import java.util.Scanner;
import java.util.Random;
class Main{
public static final Random RND_GEN = new Random();
public void createNum(int[] randomNumbers) {

for (int i = 0; i < randomNumbers.length; i++) {
    randomNumbers[i] = RND_GEN.nextInt(10) + 1;
}
}

public void printNum(int[] randomNumbers){
for (int i = 0; i < randomNumbers.length; i++) {
    System.out.println("Number " + i + " : " + randomNumbers[i]);
}
}

public void run() {
Scanner inputReader = new Scanner(System.in);
int x = 1;
do {
    int[] number = new int[20];
    createNum(number);
    printNum(number);
    System.out.print("Restart Program?, Enter 1 for YES, 2 for NO: ");            
    x = inputReader.nextInt();
} while (x == 1);
}

public static void main(String[] args) {
Main go = new Main();
go.run();
}
}

解决方案

I should not be telling you the whole code, since this looks like a homework question.

So here are the hints:

  • Creating an additional random number - Not so difficult, as you have RND_GEN

    int rn = RND_GEN.nextInt(10)+1; //exactly as you have been doing it.
    

  • Creating a method that takes three parameters.... Here is the method header:

    void search(int[] array, int size, int val)
    

  • Body of the method? You have various search algorithms out there. Some of the most popular ones are:

So just go on, research, learn and do it yourself.

这篇关于随机数和阵列搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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