金额大写转换
- import java.util.Scanner;
- public class money_capital
- {
- private static String[] NUM = { "零", "壹", "贰", "叁", "肆", "伍", "陆","柒", "捌", "玖" };
- private static String[] UNIT_EN = { "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟" };
- private static String[] UNIT_DE = { "角", "分","厘" };
- public static String NUM_UNIT( String En, String De )
- {
- String MONEY = "";
- String temp="";
- int e = En.length();
- int d = De.length();
- for( int i = 0; i < e; i ++ )
- temp += NUM[ En.charAt(i) - '0' ] + UNIT_EN[ e - i - 1 ];
- if( d == 0 )
- temp += "整";
- else
- for( int i = 0; i < d ; i ++)
- temp += NUM[ De.charAt(i) - '0' ] + UNIT_DE[ i ];
- int i = 0;
- if( temp.charAt( 0 ) == '壹' && temp.charAt( 1 ) == '拾' )
- i = 1;
- for( int j = temp.length(), zero = 1; i < j; i ++)
- {
- if( temp.charAt( i ) != '零' )
- {
- MONEY += temp.charAt( i );
- zero = 1;
- }
- else
- {
- if( zero == 1 && temp.charAt( i + 2 ) != '零')
- {
- zero = 0;
- if( temp.charAt( i + 1 ) != '元' && temp.charAt( i + 1 ) != '万' && temp.charAt( i + 1 ) != '亿')
- MONEY += temp.charAt( i );
- }
- if( temp.charAt( i + 1 ) != '元' && temp.charAt( i + 1 ) != '万' && temp.charAt( i + 1 ) != '亿' || temp.charAt( i + 1 ) == '万' && MONEY.charAt( MONEY.length()-1) == '亿' )
- i ++;
- }
- }
- return MONEY;
- }
- public static void main(String[] args)
- {
- Scanner scanner = new Scanner(System.in);
- while(true)
- {
- System.out.println("请输入您要转换的金额(注意没有校正判断请输入正确的金额,暂不支持‘,’号输入,最大转换到仟万亿位,最小到厘,金额为0时退出。)");
- String money = scanner.next();
- if(money.equals("0"))
- {
- System.out.println("谢谢您的使用!");
- break;
- }
- int i = money.indexOf('.');
- if( i == -1 )
- i = money.length();
- String En = money.substring( 0, i ); // 整数部分
- if( i == money.length() )
- i --;
- String De = money.substring( i+1, money.length() ); //小数部分
- System.out.println( "“" + money + "”转换为大写金额为:" + NUM_UNIT( En, De ) + "。/n" );
- }
- }
- }
本程序缺点:没有对输入进行判断,所以要规范输入,如1322,不要输入成1,322,又如10.00应输入成10,0.1应输入成.1,即输入最前面与最后面不能为0。
约瑟夫环
- import java.util.Scanner;
- public class Josephus
- {
- public static void main(String[] args)
- {
- Scanner scanner = new Scanner(System.in);
- System.out.print("请输入犯个数N:");
- int N = Integer.parseInt(scanner.next());
- if( N < 1 )
- {
- System.out.println("犯人个数输入错误!");
- return;
- }
- System.out.print("请输入从哪个人(S)开始数起:");
- int S = Integer.parseInt(scanner.next());
- System.out.print("请输入每次数的个数D:");
- int D = Integer.parseInt(scanner.next());
- int[] J = new int[N];
- {
- int i, j, p;
- for( i = 0; i < N -1 ; i ++ )
- J[i] = i + 1;
- J[i] = 0;
- p = S > 0 ? S - 1 : N-1 ;
- for( i = 0; i < N ; i ++ )
- {
- for( j = 1; j < D ; j ++ )
- p = J[p];
- J[p] = J[J[p]];
- }
- System.out.println("豁免的是:"+ p);
- }
- }
- }
复数类
- public class complex
- {
- public double real, imag;
- public complex( double real , double imag )
- {
- this.real = real;
- this.imag = imag;
- }
- public complex( complex a ,complex b ,char c)
- {
- System.out.print( " (" );
- a.print();
- System.out.print(") " + c + " (");
- b.print();
- System.out.print(") = ");
- if( c == '+' )
- {
- this.real = a.real + b.real;
- this.imag = a.imag + b.imag;
- }
- else
- if( c == '-' )
- {
- this.real = a.real - b.real;
- this.imag = a.imag - b.imag;
- }
- }
- public complex( complex a , complex b )
- {
- String temp = "|" + str( a ) + "|";
- if( a.real * a.real + a.imag * a.imag > b.real * b.real + b.imag * b.imag )
- temp += " > ";
- else
- if( a.real * a.real + a.imag * a.imag == b.real * b.real + b.imag * b.imag )
- temp += " == ";
- else
- temp += " < ";
- System.out.println( temp + "|" + str(b) +"|" );
- }
- public String str( complex complex )
- {
- String ss = "";
- if( complex.real != 0 )
- ss += complex.real;
- if( complex.imag > 0)
- ss += " + " + complex.imag + "i" ;
- else
- if( complex.imag < 0 )
- ss += complex.imag + "i";
- if( complex.real == 0 && complex.imag == 0 )
- ss = " 0 ";
- return ss;
- }
- public void print()
- {
- System.out.print( str( this ) );
- }
- public void println()
- {
- print();
- System.out.println();
- }
- public static void main( String args[] )
- {
- complex a = new complex( -11 , -22 );
- complex b = new complex( 11 , 2 );
- complex c = new complex( 1, 22 );
- complex d = new complex( 11, 22 );
- new complex( a , d , '+' ).println();
- new complex( a , c , '+' ).println();
- new complex( b , c , '-' ).println();
- new complex( b , d , '-' ).println();
- new complex( b , c );
- new complex( a , d );
- }
- }
bug:直接构造正纯虚数时,前面多出一个“+”号,有待改进。
谢谢批评指正。