2014-11-30 3 views
0

나는이 질문에 대한 답변을 이미 알고 있으며, 여기를 보았다 : math.random, only generating a 0? 뿐만 아니라 몇 가지 다른 포럼이지만, 지금까지 내 질문에 대답 할 수 없었다.Java Math.random이 0을 반환합니까?

나는 Rock Paper Scissors 게임을 위해 1에서 3 사이의 숫자 (여기에는 1 & 3 포함)를 생성하려고합니다. 불행히도 내 임의 번호의 출력은 항상 0입니다.

Random()을 사용할 수 없으므로주의하십시오. 이 클래스를 구현하려면 Math.random 메서드를 사용해야합니다. 여기에 임의의 숫자에 대한 내 코드입니다 :

public static int randomComputerChoice(int highVal, int lowVal) { 
     do{ 
      highVal = 4; 
      lowVal = 0; 
    computerChoice=0;//resets random number 
    //generates and returns a random number within user's range 
    computerChoice = (int)((Math.random()* highVal)+1); 
    } while((computerChoice>= highVal)||(computerChoice<= lowVal)); 
    return (computerChoice); 
} 

}이 필요하다고하면 내가 확실하지 않았다

,하지만 오류가 내 코드의 나머지 부분에있을 수 있습니다 같은 느낌.

import java.util.Scanner; 
import java.io.IOException; 

public class RockPaperScissors { 

    static int weaponChoice; 
    static int computerChoice; 
    static int lowVal = 1; 
    static int highVal = 3; 

    /** 
    * @param args the command line arguments 
    * @throws java.io.IOException 
    */ 
    public static void main(String[] args) throws IOException { 
     Scanner userInput = new Scanner(System.in); 
     System.out.println(" ========================="); 
     System.out.println("====ROCK=PAPER=SCISSORS===="); 
     System.out.println(" ========================="); 
     int playAgain = 1; //sets a play again function 
     do { 
      System.out.println("Please select your weapon."); 
      System.out.println("1 - Rock"); 
      System.out.println("2 - Paper"); 
      System.out.println("3 - Scissors"); 
      System.out.println("Choose wisely:"); 
      String choice = userInput.nextLine(); 
      weaponChoice = Integer.parseInt(choice); 
      do { 
      if (weaponChoice != 1 && weaponChoice != 2 && weaponChoice != 3) { 
        System.out.println("Please choose again, grasshopper. There are only" 
         + " three choices."); 
       System.out.println("1 - Rock"); 
       System.out.println("2 - Paper"); 
       System.out.println("3 - Scissors"); 
       choice = userInput.nextLine(); 
       weaponChoice = Integer.parseInt(choice); 
      } 
      } while (weaponChoice != 1 && weaponChoice !=2 && weaponChoice != 3); 

      if (weaponChoice == 1) { 
       System.out.println("You have selected Rock as your weapon."); 
      } 
      else if (weaponChoice == 2) { 
       System.out.println("You have selected Paper as your weapon."); 
      } 
      else { 
       System.out.println("You have selected Scissors as your weapon."); 
      } 
      //Computer's Choice 
      System.out.println("The computer's choice is "+computerChoice); 


     } while (playAgain == 1); 
} 

    public static int randomComputerChoice(int highVal, int lowVal) { 
     do{ 
      highVal = 4; 
      lowVal = 0; 
    computerChoice=0;//resets random number 
    //generates and returns a random number within user's range 
    computerChoice = (int)((Math.random()* highVal)+1); 
    } while((computerChoice>= highVal)||(computerChoice<= lowVal)); 
    return (computerChoice); 
} 


} 

어떤 도움을 주셔서 감사합니다! 대단히 감사합니다.

+0

그것은 highVal' '의 값을 무시하고'호출자가 한 것으로 lowVal' 4와 0으로 대체하는 함수의 매우 무례 (그리고 왜 당신이 할 그 이름을 가진 정적 멤버입니까?) – molbdnilo

답변

0

시도 : Min + (int)(Math.random() * ((Max - Min) + 1)). 출력은 1과 3 사이의 숫자 여야합니다. 최소값은 1, 최대 값은 3입니다. 그게 전부 야. 번호 범위는 다음과 같아야합니다 : [1,3] 이는 1과 3이 포함됨을 의미합니다. 여기

또 다른 제안 :

int number = 1 + (int)(Math.random() * ((3 - 1) + 1)); 
    System.out.println(number); 
//alternative way: 
// return number; 
+0

computerChoice는 int 값으로 저장되므로 처음에 (int)를 제거하면 처음부터 double을 저장해야합니다. 새로운 변수를 만들어서 double로 저장하고 int로 변환하는 것이 좋은 생각일까요? –

+0

여기에 문제가 표시되지 않습니다. 죄송합니다. ENTER를 너무 일찍 눌렀습니다. 내가 게시 한이 함수는 int도 반환합니다. 변환 할 필요가 없습니다. –

+0

여전히 0으로 인쇄됩니다. 방법에 문제가 있는지 궁금합니다. –

관련 문제