2012-10-20 3 views
4

나는 나 자신에게 자바를 가르치려고 노력하고 내가 가진 책에 두 장 약간의 딸꾹질 실행 해요 : P를이 운동 중 하나입니다 :자바 환율 교단 문제

는 "그 계산하는 클래스를 작성 입력 된 달러의 통화 단위를 20, 10, 5, 1로 변환하여 표시합니다. "

지금까지 코딩에 대한 지식이 4 시간 이상 이었으므로이 질문에 대답하는 것이 너무 단순하지 않다고 생각합니다. 나는이 모든 것을 작성하는 훨씬 더 효율적인 방법이있을 것이라고 확신하지만, 내 질문은 사용자가 "예"라고 응답하면 전체를 종료 할 수 있고 "아니오"라고 대답하면 수정 된 버전으로 계속 진행할 수있다.

또한 자바를 배우는 데 도움이되는 조언이나 안내는 많은 도움이됩니다. 모든이

import javax.swing.JOptionPane; 
public class Dollars 
{ 
    public static void main(String[] args) 
    { 
     String totalDollarsString; 
     int totalDollars; 
     totalDollarsString = JOptionPane.showInputDialog(null, "Enter amount to be  converted", "Denomination Conversion", JOptionPane.INFORMATION_MESSAGE); 
    totalDollars = Integer.parseInt(totalDollarsString); 
    int twenties = totalDollars/20; 
    int remainderTwenty = (totalDollars % 20); 
    int tens = remainderTwenty/10; 
    int remainderTen = (totalDollars % 10); 
    int fives = remainderTen/5; 
    int remainderFive = (totalDollars % 5); 
    int ones = remainderFive/1; 
    JOptionPane.showMessageDialog(null, "Total Entered is $" + totalDollarsString + "\n" + "\nTwenty Dollar Bills: " + twenties + "\nTen Dollar Bills: " + tens + "\nFive Dollar Bills: " + fives + "\nOne Dollar Bills: " + ones); 
    int selection; 
    boolean isYes, isNo; 
    selection = JOptionPane.showConfirmDialog(null, 
     "Is this how you wanted the total broken down?", "Select an Option", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); 
    isYes = (selection == JOptionPane.YES_OPTION); 
     JOptionPane.showMessageDialog(null, "You responded " + isYes + "\nThanks for your response!"); 
     isNo = (selection == JOptionPane.NO_OPTION); 
     int twenties2 = totalDollars/20; 
     int tens2 = totalDollars/10; 
     int fives2 = totalDollars/5; 
     int ones2 = totalDollars/1; 
     JOptionPane.showMessageDialog(null, "Total Entered is $" + totalDollarsString + "\n" + "\nTwenty Dollar Bills: " + twenties2 + "\nTen Dollar Bills: " + tens2 + "\nFive Dollar Bills: " + fives2 + "\nOne Dollar Bills: " + ones2); 
} 
} 

답변

0

먼저 읽어 주셔서 덕분에, 당신은 정말 isYes 및 ISNO에 대한 두 개의 논리 값을 필요로하지 않는 것. 기본적으로 사용자에게 다른 솔루션이 필요한지 여부를 묻습니다.이 값은 하나의 참/거짓 값입니다 (또는 오히려 isNo는! isYes와 같습니다. 옵션 창은 YES_OPTION 및 NO_OPTION 값 중 하나만 반환하기 때문입니다). 사용자가 '예'를 선택

int selection = JOptionPane.showConfirmDialog(null, 
     "Is this how you wanted the total broken down?", "Select an Option", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); 
if (selection == JOptionPane.NO_OPTION) {    
    int twenties2 = totalDollars/20; 
    int tens2 = totalDollars/10; 
    int fives2 = totalDollars/5; 
    int ones2 = totalDollars/1; 
    JOptionPane.showMessageDialog(null, "Total Entered is $" + totalDollarsString + "\n" + "\nTwenty Dollar Bills: " + twenties2 + "\nTen Dollar Bills: " + tens2 + "\nFive Dollar Bills: " + fives2 + "\nOne Dollar Bills: " + ones2); 
} 

경우 : 당신이 당신의 '세련된'버전 로 이동되어 다음에 수행 할 수있는 것들

경우 사용자는 첫번째 출력은 그가 원하는 것을 없음을 나타냅니다 어쨌든 주 방법이 완료되므로이 경우 아무 것도 할 필요가 없습니다.

+0

답변 해 주셔서 감사합니다. 그것은 내가 이것을 완벽하게 끝내는 것을 도왔습니다 : D – user1761914