作业说明:
您将编写一个JAVA程序,用于模拟10匹马的赛马。您必须使用数组。马1位于位置1,马2位于位置2,依此类推。所有的马将从位置0开始(在阵列的该位置具有零值)。用户将按输入以使所有匹马前进。每按一次 enter ,你就会为这十匹马中的每一匹产生一个1到3之间的随机数,这将是马将会移动的空间数量#34;向前。这个数字将被添加到马的前一个数字。第一匹到达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
答案 0 :(得分:0)
这是必需的步骤
if(No winner)
- ask the user to press enter
- for each horse add random number between 1 and 3
- print the distance in ran by each horse
否则
- 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