2015-01-20 2 views
-2
private static void algorithm2() { 
    String name = ""; 
    int high = (char)90; 
    int low =(char) 66; 
    int letter; 
    int direction; 
    do { 
     letter = (high + low)/2; 
     String[] choices = { "Before", "This is the letter", "After", "Done" }; 

     direction = JOptionPane.showOptionDialog(null, 
       "Is the next letter in your name a(n) \'" +(char) letter 
         + "\' or is it after/before it? ", "Option 2", 
       JOptionPane.DEFAULT_OPTION, 
       JOptionPane.PLAIN_MESSAGE, 
       null, choices, "Option 2"); 

     if (direction == 0) 
      low= letter - 1; // 

     if (direction == 1){ 
      name+=(char)letter; 
     } 

     if(direction==2){ 
      high = letter + 1; // last guess was too high 
     } 
     if (direction == 3) 
      break; 
    } while (direction != 1); 

    JOptionPane.showMessageDialog(null, "Thanks for playing " + name + "!"); 
} 

가능한 문자의 각 시퀀스에서 중간 문자를 추측합니다. 처음에는 m 또는 n입니다. 추측이있을 때마다 a) 추측이 맞는지, b) 정확한 답이 추측보다 알파벳 순서대로 일찍 나오는지, c) 정확한 문자가 나중에 추측보다 알파벳 순서대로 있는지 사용자에게 물어보십시오. 그에 따라 추측하는 추측을 조정하여 남은 문자 세트의 가운데 문자를 항상 추측합니다. 가능성의 수가 짝수 일 때, 첫 번째 또는 두 번째 "중간"문자를 추측하든 상관 없습니다. 단 하나의 가능성이있을 때, 그 중 하나는 하나의 가치 목록에있는 중간 선택입니다.내 방법을 제대로 작동시키는 방법?

나는 창을 표시 할 때 이전에 클릭하고 잘못된 문자를 표시 할 때 작동하지 않습니다. 사람들로

+1

확실하게'low = letter-1'은'high = letter-1'이어야하고'high = letter + 1'은'low = letter + 1'이어야합니까? – ruakh

+0

어떤 진단 검사를 수행 했습니까? 디버깅을 시도 했습니까? 그렇다면, 무슨 일이 일어 났습니까? 잘못된 문자가 무엇입니까? 방향이 올바르게 해석되고 있습니까? –

+0

실제로 "지난 추측이 너무 높았습니다"라는 주석은 "다음 글자가 [글자 표시 후]"옵션과 일치하지 않습니다. –

답변

0

는 의견에서 지적한 :

if(direction==2){ 
    high = letter + 1; // last guess was too high 
} 

될해야

if (direction == 0) 
    low= letter - 1; // 

이 유사

high = letter - 1; 

이어야한다

low = letter + 1; 

더욱 더, 당신은 루프를 종료 할 때 direction == 1 : 당신이 조심 while(true); 또는 while(direction != 3)

public static void main(String[] args) { 


    String name = ""; 
    int direction; 
    do { 
     int high = (char) 90; 
     int low = (char) 66; 
     int letter; 

     do { 
      letter = (high + low)/2; 
      String[] choices = { "Before", "This is the letter", "After", 
        "Done" }; 

      direction = JOptionPane.showOptionDialog(null, 
        "Is the next letter in your name a(n) \'" 
          + (char) letter 
          + "\' or is it after/before it? ", "Option 2", 
        JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, 
        null, choices, "Option 2"); 
      System.out.println(direction); 
      if (direction == 0) 
       high = letter - 1; // 

      if (direction == 1 || direction == 3) { 
       name += (char) letter; 
      } 

      if (direction == 2) { 
       low = letter + 1; // last guess was too high 
      } 

     } while (direction != 1 && direction != 3); 

    } while (direction != 3); 

    JOptionPane.showMessageDialog(null, "Thanks for playing " + name + "!"); 
} 

으로 변경해야하므로

while (direction != 1); 

그래서 사용자가 1 개 이상의 문자를 입력 할 수 없습니다를 할 때 high == low 사용자가이 문자를 허용하지 않으면 오류입니다.

+0

각각을 지워야합니까? – user3269660

+0

그것은 전 단지 약 4 글자와 이후에 대한 편지를 제공합니다 – user3269660

+0

@ user3269660 내 대답을 업데이 트 기본적으로, 당신은 두 개의 루프가 필요합니다. –

관련 문제