2013-03-27 2 views
-3

메인 메서드에 4 개의 무작위로 구분 된 숫자 (반복 없음)를 사용하여 만든 int를 반환해야 나머지 작업에도 사용할 수 있습니다. 문자열 메서드도 사용할 수 없습니다. 나는이 내가 무엇을 가지고는 임의의 숫자메소드에서 Int 호출?

를 인쇄 할 : 코드에서

public static void main(String[] args) { 
     int generateSecretNumber; 
     System.out.print("Random number is: "+generateSecretNumber); 

} 

public static int generateSecretNumber(int w, int x, int y, int z, int secretNumber) { 
    Random ran = new Random(); 

    w = ran.nextInt(); 
    x = ran.nextInt(); 
    y = ran.nextInt(); 
    z = ran.nextInt(); 


    while (w == x || w == y || w == z){ 
    w = ran.nextInt(); 
    } 
    while (x == w || x == y || x==z){ 
    x = ran.nextInt(); 
    } 
    while (y == w || y == x || y == z){ 
    y = ran.nextInt(); 
    } 
    while (z == w|| z== x || z == y){ 
    z = ran.nextInt(); 
    } 

    if (w != x && x != y && y!=z){ 

    secretNumber = w + x + y + z; 
    } 

    return secretNumber; 
} 

} 
+0

현재 문제는 무엇입니까? – Gawdl3y

+0

실제로 메소드를 먼저 호출하십시오. 로컬에 저장하지 않아도됩니다. –

+0

어떻게해야합니까 ?? main 메서드에서 secretNumber를 인쇄하고 싶습니다. – user2121773

답변

1

, 당신은 결코 실제로 you'r 대신 변수를 선언, 당신의 generateSecretNumber() 메소드를 호출하지 않습니다. 주 신고를 취소하고 인쇄 라인을 System.out.print("Random number is: " + generateSecretNumber());으로 변경하십시오.

다음으로 generateSecretNumber() 메서드는 자체적으로 전체 작업을 결정하고 외부 데이터를 필요로하지 않으므로 인수가 없어야합니다. 인수가 없어지기 때문에 int w, x, y, z;도 선언해야합니다.

둘째, 상한이없는 임의의 정수를 생성합니다. 이것은 원하는 바가 아니며 대신 rand.nextInt() 호출의 상한선이 10이어야하므로 rand.nextInt(10)이됩니다. 0 ~ 9의 임의의 정수를 선택합니다.

마지막으로 실제 4 자리 숫자 대신 합계를 반환합니다. 대신 각 자릿수의 합계를 그 자리에 반환하십시오. 예를 들어, 네 번째 숫자는 w * 1000이어야합니다.

결과 코드 예제 :

public class RandUniqueFourDigits { 
    public static void main(String[] args) { 
     System.out.println("Random number is: " + generateSecretNumber()); 
    } 

    public static int generateSecretNumber() { 
     Random rand = new Random(); 
     int w = 0, x = 0, y = 0, z = 0; 

     // Generate each digit until they're all unique 
     while(w == x || w == y || w == z || x == y || x == z || y == z) { 
      w = rand.nextInt(10); 
      x = rand.nextInt(10); 
      y = rand.nextInt(10); 
      z = rand.nextInt(10); 
     } 

     // Generate each digit until they're all unique 
     w = rand.nextInt(10); 
     do x = rand.nextInt(10); while(x == w) 

     // Combine them into one integer and return 
     return w * 1000 + x * 100 + y * 10 + z; 
    } 
} 

그리고보다 효율적인 루프 (각 숫자 만이 초기 코드처럼 될 필요가있을 때 생성 재)

는이 완전히 while 루프를 대체 :

결과
w = rand.nextInt(10); 
do x = rand.nextInt(10); while(x == w); 
do y = rand.nextInt(10); while(y == w || y == x); 
do z = rand.nextInt(10); while(z == w || z == x || z == y); 

: 고려

public class RandUniqueFourDigits { 
    public static void main(String[] args) { 
     System.out.println(generateSecretNumber()); 
    } 

    public static int generateSecretNumber() { 
     Random rand = new Random(); 
     int w = 0, x = 0, y = 0, z = 0; 

     // Generate each digit until they're all unique 
     w = rand.nextInt(10); 
     do x = rand.nextInt(10); while(x == w); 
     do y = rand.nextInt(10); while(y == w || y == x); 
     do z = rand.nextInt(10); while(z == w || z == x || z == y); 

     // Combine them into one integer and return 
     return w * 1000 + x * 100 + y * 10 + z; 
    } 
} 
0

하지 많은 숫자 Y가 ou는 모든 것을 메모리에서 계산하고 그로부터 임의의 값을 되돌릴 수 있습니다.

public class RandomGenerator { 
    private static final List<Integer> numbers; 
    private static final Random random = new Random(); 

    static { 
     for(int i = 1000; i < 10000; i++) { 
      int x = i%10; 
      int y = (i/10)%10; 
      int z = (i/100)%100; 
      int u = (i/1000); 

      // Check for uniqueness 
      if(unique) { numbers.add(i); } 
     } 
    } 

    public static Integer getNumber() { 
     return numbers.get(random.nextInt(numbers.size())); 
    } 
} 
관련 문제