【问题标题】:Using Switch and JOptionPane使用 Switch 和 JOptionPane
【发布时间】:2023-03-14 07:56:02
【问题描述】:

问题:要求用户输入 0-9999 并使用 switch case 和 JOptionPane 将其转换为单词... 我的问题:我知道如何转换其他数字,但如果我输入 11-19,输出例如我输入 11 - “十一个”我怎样才能得到正确的输出?

【问题讨论】:

  • 请提供更多细节。到目前为止,您尝试过什么?

标签: switch-statement joptionpane


【解决方案1】:
public class NumbertoWords {​​
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {​​​​​​​​​
        // TODO code application logic here
        int num=Integer.parseInt(JOptionPane.showInputDialog("Enter a Number from [0-9999]"));
        
        int thou=num/1000;
        int temp=num%1000;     // 9999
        int hun=temp/100;       //999  -> 9
        int temp1=temp%100;
        int tens=temp1/10;       //99 
        int ones=temp%10;
        
        String display="";
        
        
        switch(thou){​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
            
            case 9: display=" Nine Thousand"; break;
            case 8: display=" Eight Thousand"; break;
                  
        }​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
       
        switch (hun){​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
            
            case 9: display=display+ " Nine Hundred"; break;
            case 8: display=display+ " Eight Hundred"; break;
        }​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
        
        switch (tens){​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
            case 9: display=display+ " Ninety"; break;
        }​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
        
        switch (ones){​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
            case 9:  display=display+ " Nine"; break;
            
        }​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
        
        
        JOptionPane.showMessageDialog(null, display,"Number to Words",JOptionPane.INFORMATION_MESSAGE);
      System.exit(0);
    }​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
    
}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

【讨论】:

    猜你喜欢
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 1970-01-01
    • 2013-12-11
    • 2016-05-26
    • 1970-01-01
    相关资源
    最近更新 更多