2015-08-31 47 views
-3

我正在嘗試創建賽馬博彩遊戲。我無法比較各匹馬的價值,看看哪一匹獲勝。運算符>未定義爲參數類型java.util.Random,java.util.Random

if語句我嘗試比較horse1的值和horse2和horse3的值。但我得到的錯誤: 他操作>未定義的參數類型java.util.Random中,java.util.Random的

import java.util.Scanner; 
import java.util.Random; 
public class RunHorseGame 
{ 

public static void main(String[] args) 
{ 
// TODO Auto-generated method stub 

//variables 
    String name; 
    float bal; 
    float bet; 
    boolean nextRace; 
    int raceChoice; 
    int startRace; 

    Random horse1 = new Random(100); 
    Random horse2 = new Random(100); 
    Random horse3 = new Random(100); 
    Random horse4 = new Random(100); 
    Random horse5 = new Random(100); 
    Random horse6 = new Random(100); 
    Random horse7 = new Random(100); 

    Scanner input = new Scanner(System.in); 

//welcome screen 
    System.out.println("Welcome to Horse Racing!"); 
    System.out.println("Please enter your name:"); 
    name = input.nextLine(); 
    System.out.println("welcome " + name + ", you have a balance of $200!"); 

//create loop to repeat races when one finishes (keep balance) 
    nextRace = true; 
    while (nextRace == true) 
    { 
     bal = 200; 

//give race options 
     System.out.println("Please select which race you would like to  enter:"); 
     System.out.println("Press 1 to enter: 3 horse race"); 
     System.out.println("Press 2 to enter: 5 horse race"); 
     System.out.println("Press 3 to enter: 7 horse race"); 

//create each race 
//each horse has randomizer 
//highest number wins race 

     raceChoice = input.nextInt(); 
     switch(raceChoice) 
     { 
      case 1: 
       System.out.println("You have entered the 3 horse race!"); 
       System.out.println("How much would you like to bet?"); 
       bet = input.nextFloat(); 
       System.out.println("You have bet " + bet + " dollars."); 
       bal =- bet; 
       System.out.println("Press 1 to start."); 
       System.out.println("Press 2 to go back to race selection."); 

       startRace = input.nextInt(); 
       switch(startRace) 
       { 
       case 1: 


        if(horse1 > horse2 && horse1 > horse3) 
        { 

        } 

        break; 

       case 2: 
        nextRace = false; 
        break; 
       } 


       break; 
      case 2: 
       break; 
      case 3: 
       break; 
      default: 
       break; 


     } 

     nextRace = true; 

    } 
} 
} 
+2

隨機馬1 =新隨機(100); ** **不會使馬1成爲一個隨機數達100(這是它看起來像你正在做的事情)。你只需要1個「隨機」對象,那麼馬1,馬2等將是整數或什麼,你分配他們像'horse1 = myRandom.nextInt(100);' – csmckelvey

回答

0

使用以下:

Random randomGenerator = new Random(); 

     int horse1 = randomGenerator.nextInt(100); 
     int horse2 = randomGenerator.nextInt(100); 
//and so on... 

作爲(@ajb)正確地指出:甲Random是不是隨機數。所以,當你分配一個數據類型時,它應該是int而不是Random,然後你應該使用如上所示的方法Random來獲得所需的隨機數。

4

一個Random一個隨機數。 A Random是隨機數生成器。因此,如果您想要獲取數字進行比較,您必須使用Random中的方法之一來獲取生成器以生成號碼

2

Random是對隨機數生成器的引用,而不是其他任何東西。您不需要爲每匹馬創建一個新的隨機數生成器。

使Random一個實例: Random r = new Random()

然後創建的馬隨機機會: int horse = r.nextInt(100);

像現在這樣。

相關問題