Java实验:(金额大写转换,约瑟夫环,复数类)

金额大写转换

  1. import java.util.Scanner;
  2. public class money_capital
  3. {
  4.     private static String[] NUM = { "零""壹""贰""叁""肆""伍""陆","柒""捌""玖" };
  5.     private static String[] UNIT_EN = { "元""拾""佰""仟""万""拾""佰""仟""亿""拾", "佰", "", "", "", "", "仟" };
  6.     private static String[] UNIT_DE = { "角""分","厘" };
  7.     public static String NUM_UNIT( String En, String De )
  8.     {
  9.         String MONEY = "";
  10.         String temp="";
  11.         int e = En.length();
  12.         int d = De.length();
  13.         forint i = 0; i < e; i ++ )
  14.             temp += NUM[ En.charAt(i) - '0' ] + UNIT_EN[ e - i - 1 ];
  15.         if( d == 0 )
  16.             temp += "整";
  17.         else
  18.             forint i = 0; i < d ; i ++)
  19.                 temp += NUM[ De.charAt(i) - '0' ] + UNIT_DE[ i ];
  20.         int i = 0;
  21.         if( temp.charAt( 0 ) == '壹' && temp.charAt( 1 ) == '拾' )
  22.             i = 1;
  23.         forint j = temp.length(), zero = 1; i < j; i ++)
  24.         {
  25.             if( temp.charAt( i ) != '零' )   
  26.             {
  27.                 MONEY += temp.charAt( i );
  28.                 zero = 1;
  29.             }
  30.             else
  31.             {
  32.                 if( zero == 1 && temp.charAt( i + 2 ) != '零')
  33.                 {
  34.                     zero = 0;
  35.                     if( temp.charAt( i + 1 ) != '元' && temp.charAt( i + 1 ) != '万' && temp.charAt( i + 1 ) != '亿')
  36.                         MONEY += temp.charAt( i );
  37.                 }
  38.                 if( temp.charAt( i + 1 ) != '元' && temp.charAt( i + 1 ) != '万' && temp.charAt( i + 1 ) != '亿' ||  temp.charAt( i + 1 ) == '万' &&  MONEY.charAt( MONEY.length()-1) == '亿' )
  39.                         i ++; 
  40.             }
  41.         }           
  42.         return MONEY;
  43.     }
  44.     public static void main(String[] args)
  45.     {
  46.         Scanner scanner = new Scanner(System.in);
  47.         while(true)
  48.         {
  49.             System.out.println("请输入您要转换的金额(注意没有校正判断请输入正确的金额,暂不支持‘,’号输入,最大转换到仟万亿位,最小到厘,金额为0时退出。)");
  50.             String money = scanner.next();      
  51.             if(money.equals("0"))
  52.             {
  53.                 System.out.println("谢谢您的使用!");
  54.                 break;
  55.             }
  56.             int i = money.indexOf('.');
  57.             if( i == -1 )
  58.                 i = money.length();
  59.             String En = money.substring( 0, i );                        // 整数部分
  60.             if( i == money.length() )
  61.                 i --;
  62.             String De = money.substring( i+1, money.length() );         //小数部分
  63.             System.out.println( "“" + money + "”转换为大写金额为:" + NUM_UNIT( En, De ) + "。/n" );
  64.         }
  65.     }
  66. }

本程序缺点:没有对输入进行判断,所以要规范输入,如1322,不要输入成1,322,又如10.00应输入成10,0.1应输入成.1,即输入最前面与最后面不能为0。

 

 

约瑟夫环

  1. import java.util.Scanner;
  2. public class Josephus
  3. {
  4.     public static void main(String[] args)
  5.     {
  6.         Scanner scanner = new Scanner(System.in);
  7.         System.out.print("请输入犯个数N:");
  8.         int N = Integer.parseInt(scanner.next());
  9.         if( N < 1 )
  10.         {
  11.             System.out.println("犯人个数输入错误!");
  12.             return;
  13.         }
  14.         System.out.print("请输入从哪个人(S)开始数起:");
  15.         int S = Integer.parseInt(scanner.next());
  16.         System.out.print("请输入每次数的个数D:");
  17.         int D = Integer.parseInt(scanner.next());
  18.         int[] J = new int[N];
  19.         {
  20.             int i, j, p;
  21.             for( i = 0; i < N -1 ; i ++ )
  22.                 J[i] = i + 1;
  23.             J[i] = 0;
  24.             p = S > 0 ? S - 1 : N-1 ;
  25.             for( i = 0; i < N ; i ++ )
  26.             {
  27.                 for( j = 1; j < D ; j ++ )
  28.                     p = J[p];
  29.                 J[p] = J[J[p]];             
  30.             }
  31.             System.out.println("豁免的是:"+ p);
  32.         }
  33.         
  34.     }
  35. }

 

复数类

  1. public class complex
  2. {
  3.     public double real, imag;
  4.     public complex( double real , double imag )
  5.     {
  6.         this.real = real;
  7.         this.imag = imag;
  8.     }
  9.     public complex( complex a ,complex b ,char c)
  10.     {
  11.         System.out.print( " (" );
  12.         a.print();
  13.         System.out.print(") " + c + " (");
  14.         b.print();
  15.         System.out.print(") = ");
  16.         if( c == '+' )
  17.         {
  18.             this.real = a.real + b.real;
  19.             this.imag = a.imag + b.imag;
  20.         }
  21.         else
  22.             if( c == '-' )
  23.             {
  24.                 this.real = a.real - b.real;
  25.                 this.imag = a.imag - b.imag;
  26.             }
  27.     }
  28.     public complex( complex a , complex b )
  29.     {
  30.         String temp = "|" + str( a ) + "|";
  31.         if( a.real * a.real + a.imag * a.imag > b.real * b.real + b.imag * b.imag )
  32.             temp += " > ";
  33.         else
  34.             if( a.real * a.real + a.imag * a.imag == b.real * b.real + b.imag * b.imag )
  35.                 temp += " == ";
  36.             else
  37.                 temp += " < ";      
  38.         System.out.println( temp + "|" + str(b) +"|" );
  39.     }
  40.     public String str( complex complex )
  41.     {
  42.         String ss = "";
  43.         if( complex.real != 0  )
  44.             ss += complex.real;
  45.         if( complex.imag > 0)
  46.             ss += " + " + complex.imag + "i" ;
  47.         else
  48.             if( complex.imag < 0 )
  49.                 ss += complex.imag + "i";
  50.         if( complex.real == 0 && complex.imag == 0 )
  51.             ss = " 0 ";
  52.         return ss;
  53.     }
  54.     public void print()
  55.     {
  56.         System.out.print( str( this ) );
  57.     }
  58.     public void println()
  59.     {
  60.         print();
  61.         System.out.println();
  62.     }
  63.     public static void main( String args[] )
  64.     {
  65.         complex a = new complex( -11 , -22 );
  66.         complex b = new complex( 11 , 2 );
  67.         complex c = new complex( 122 );
  68.         complex d = new complex( 1122 );
  69.         new complex( a , d , '+' ).println();
  70.         new complex( a , c , '+' ).println();
  71.         new complex( b , c , '-' ).println();
  72.         new complex( b , d , '-' ).println();
  73.         new complex( b , c );
  74.         new complex( a , d );
  75.     }
  76. }

bug:直接构造正纯虚数时,前面多出一个“+”号,有待改进。

 

谢谢批评指正。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值