2015-01-21 3 views
-1

모든 것이 System.out.println 대신 JOptionPane을 사용하고 싶습니다. 내 프로그램이 어떻게 코드를 건너 뛰는지를 설명하는 방법을 모르겠습니다. 당신이 이것을 볼 수 있고 그것을 높이 평가 될 순서대로 가도록 도와주세요.JOptionPane 학습에 문제가 있습니까?

if (holeChoice.equals("9")) { ... } 

을 또는 중 하나를 사용하여 문자열을 정수로 구문 분석 :

//Step 1: Import Java APIs 
import java.util.InputMismatchException; 
import java.util.Scanner; 
import javax.swing.*; 



//Step 2: Name File and Class 
public class GolfScores { 




//Step 3: Declare All Variables 

public static void main(String[] args) { 
    int hole9 = 0; 
    int hole18 = 0; 
    String holeChoice; 

    Scanner input = new Scanner(System.in); 


//Step 4: The program accepts INPUT from the user 

    for (;;) { 
    try { 
holeChoice = JOptionPane.showInputDialog("For hole 9, please enter 9. If you are on hole 18, please enter 18 "); 

    if (holeChoice.equals(9)) { 
     System.out.println("The par for this hole is 3. Please enter if you this hole took you: " 
        + "1, 2, 3, 4, 5, 6+ shots");    
     hole9 = input.nextInt(); 

    } else if (holeChoice.equals(18)) { 
     System.out.println("The par for this hole is 5. Please enter if you this hole took you: " 
        + "1, 2, 3, 4, 5, 6, 7 or 8+ shots"); 
     hole18 = input.nextInt(); 

    } else if (holeChoice.equals(18)) { 
     System.out.println("Please enter a valid number"); 
     continue; 
    } 
    break; 
} catch (InputMismatchException inputMismatchException) { 
    System.err.printf("\nException: %s\n", inputMismatchException); 
    System.out.println("Please enter a valid number."); 
    input.next(); 
} 
} 


//Step 5 & 6: The user input is PROCESSED by Java and uses math to calcualte an answer to output. The user's score is then output for them to see. 
if (hole18 == 1) { 
    System.out.println("Your score for this hole was a hole in one!"); 

} else if (hole18 == 2) { 
    System.out.println("Your score for this hole was albetross."); 

} else if (hole18 == 3) { 
    System.out.println("Your score for this hole was eagle."); 

} else if (hole18 == 4) { 
    System.out.println("Your score for this hole was birdie."); 

} else if (hole18 == 5) { 
    System.out.println("Your score for this hole was par."); 

} else if (hole18 == 6) { 
    System.out.println("Your score for this hole was boogie."); 

} else if (hole18 == 7) { 
    System.out.println("Your score for this hole was double boogie."); 

} else if (hole18 >= 8) { 
    System.out.println("You need some more practice at golf."); 

} else if (hole18 <= 0) { 
    System.out.println("Please input a valid number."); 
    input.next(); 

} 


    if (hole9 == 1) { 
    System.out.println("Your score for this hole was a hole in one!"); 

} else if (hole9 == 2) { 
    System.out.println("Your score for this hole was birdie."); 

} else if (hole9 == 3) { 
    System.out.println("Your score for this hole was par."); 

} else if (hole9 == 4) { 
    System.out.println("Your score for this hole was boogie."); 

} else if (hole9 == 5) { 
    System.out.println("Your score for this hole was double boogie."); 

} else if (hole9 >= 6) { 
    System.out.println("Your need some more practice at golf."); 

    } 

try { 
    Thread.sleep(3000);     
}  catch(InterruptedException ex) { 
     Thread.currentThread().interrupt(); 
} 

    System.out.println("Thank you for playing Java Golf. Please come  again"); 

    } 

}

+0

무엇을하고 싶습니까? 어떤 단추? – roeygol

+0

실제로 실행되는 순서를 설명하십시오. 예상되는 단계가 예상 한 것으로 가정합니다. – SuckerForMayhem

+0

좀 더 구체적으로 설명 할 수 있습니까? – geekybedouin

답변

2

JOptionPane.showInputDialog(..)에서 반환되는 값은 문자열이 그렇게 사용하는

Integer.parseInt(holeChoice); 
2

JOptionPane.showInputDialogString 반환 결과를 마치인 것처럼 비교하고 있습니다..

holeChoice = JOptionPane.showInputDialog("For hole 9, please enter 9. If you are on hole 18, please enter 18 "); 
//... 
if (holeChoice.equals(9)) // this will always be false 

사용자가, 당신은 당신이 확인 된 값에 따옴표를 추가 할 필요가 작동하지 않습니다 int9과 비교, 9 입력합니다.

예 : 마찬가지로

if (holeChoice.equals("9")) 

, 당신은 또한 입력을 구문 분석하고 int로 설정을 시도 할 수 있지만, 어떤 당신은 확인해야 할 것 (유효하지 않은 사용자 입력에 대한 문을 열 수 있습니다)

try { 
    Integer choice = Integer.valueOf(holeChoice); 
    //... 
    if (choice.equals(9)) { // works now... 
} 
catch (NumberFormatException ex) { 
    // user didn't enter a number! 
}