【问题标题】:How to add random number generated to previous location in array?如何将生成的随机数添加到数组中的先前位置?
【发布时间】:2015-04-04 18:56:56
【问题描述】:

作业说明:

您将编写一个 JAVA 程序来模拟 10 匹马的赛马。您必须使用数组。马 1 将在位置 1,马 2 在位置 2,依此类推。所有的马将从位置 0 开始(在数组的该位置具有零值)。用户将按下 enter 让所有马匹前进。每次按 enter 时,您将为十匹马中的每一匹生成一个介于 1 和 3 之间的随机数,这将是马将“移动”向前的空间量。该数字将被添加到马的前一个数字上。第一个达到 15 的马将是获胜者。你的程序应该在马获胜后停止。在每次迭代中,您将显示每匹马的位置。

//WHEN THE USER HITS ENTER NUMBERS ARE ADDED TO PREVIOUSLY GENERATED NUMBERS
//FIRST HORSE TO FIFTEEN WINS
//IM LOST?? :(        
import java.util.*;
import java.text.*;
import java.util.Random;
   public class HorseRace{
      public static void main(String[ ] arg){
         final int ONE = 10;
         final int TWO = 10;
         int count = 0;
         int i = 0; //position
         String[ ] sWords = new String[ONE];
         String sONEWord="";
         Scanner sc = new Scanner(System.in);
         System.out.println("Hit enter to begin race");
         sc.nextLine();
      Random ran = new Random( ); //generates random number
      int[ ] arrRan1 = new int[ONE]; //populating the array one number at the time
         for(int a = 0; a<arrRan1.length; a++){
         arrRan1[a] = ran.nextInt(3) + 1; //number from 1 to 3, then add 1
         }
         boolean exit = false;
      while(count < ONE){ for(int a = 0; a<arrRan1.length; a++){
            System.out.println("Horse "+ (i+1) + ": " + (arrRan1[a]));
            i = i +1;
            }


      System.out.print("Press Enter key to continue");
         sc.nextLine();
      Random ass = new Random( ); //generates random number
      for(int b = 0; b<arrRan1.length; b++){
         int a = 0;
         arrRan1[b] = ran.nextInt(3) + 1; //number from 1 to 3, then add 1
         }
         boolean exit2 = false;
      while(count < TWO){ for(int b = 0; b<arrRan1.length; b++){
            System.out.println("Horse "+ (count+1) + ": " + (arrRan1[b]));
            count = count +1;
            }
            }                               
       }
    }//main
  } //class

【问题讨论】:

  • 要让这个问题被接受,你必须解释你在代码中看到了什么问题,以及你期望它做错了什么。
  • 看来我正在创建两组单独的整数数组(1 个字符串数组),有 10 个位置。我无法连接这些点。如何将第二个 int 数组中的数字添加到第一个中的值?
  • 示例 ...[5,7,9].=[1,2,3] + [4,5,6]
  • 使用循环。提示:您实际上不需要创建一个生成数字的数组。只需在第一个数组上循环,并在每次迭代中生成一个数字并将其添加到该数组项。
  • 这就是我的想法。我还没有在网上看到这个代码之王的例子。这是这个谜题中缺少的部分。一旦我弄清楚了,我只需要编写一个条件,当一个元素的值达到 15...Nested Loop??

标签: java arrays random


【解决方案1】:

这里是必需的步骤

  1. 创建用零初始化的空数组
  2. 检查是否有中奖者
  3. 如果(没有赢家)

    - ask the user to press enter
    - for each horse add random number between 1 and 3
    - print the distance in ran by each horse
    
  4. 其他

    - print the winner horse number
    

我为你提供了代码,但请尝试自己实现它

import java.util.*;
import java.util.Random;
   public class HorseRace{

       // function that iterate over the distance array and check every element if the element is greater than 15 it will return 
       // its index , if there is no element greater than 15 , the function will return -1 
       // the function returns the first element greater than 15 
       public static int winnerExist(int [] distance)
       {
           for(int i = 0;i<distance.length;i++)
               if(distance[i] >= 15)
                   return i;
           return -1;
       }

      public static void main(String[ ] arg){
          Scanner in = new Scanner(System.in);
          // distance array that will hold the distance ran by each horse 
          int [] distance = new int [10];
          // min and max are used to generate random number between 1 and 3 
         int min =1,max=3;
          Random randomNumber = new Random();         
          // winner variable will hold the number of the winner horse 
          int winner;
          // check if the winner value = -1 , which means that there is no winner 
          // if the winner value != -1 , this means that we have a winner horse
          while ((winner = winnerExist(distance) )==-1) {
              System.out.println("Press enter ..");
              // you can check for the string read from the user if it is an empty string or not ( not necessary ) 
              in.nextLine();
              // in this for loop we generate 10 random numbers to be added to each horse 
              for(int i=0;i<10;i++)
                  distance[i]+=randomNumber.nextInt(max-min +1) +min;
              // print the distance of each horse
              for(int i =0 ;i <10;i++)
                  System.out.printf("Distance ran by Horse Number %d is %d \n" , i+1 ,distance[i]);

        }
          // print the winner horse number after adding ( 1 ) to the variable winner because horse number 1 exist in distance [0]
          System.out.printf("The winner horse is horse number %d\n" , (int)(winner+1));



       }

  } //class

【讨论】:

    猜你喜欢
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    相关资源
    最近更新 更多