JAVA Review(1)

题目(1):金额转换:将阿拉伯数字的金额转换成中国传统形式

例如:105201314 ----> 壹亿零仟伍佰贰拾万壹仟叁佰壹拾肆圆整

(本题最大单位到亿哈!!!)

package com.my.exercises;

import java.util.*;


public class exercise_1 {
    // 建立一个数组将单位存储起来
    private static final String units[] = new String[] { "圆", "拾", "佰", "仟", "万","拾", "佰", "仟", "亿" };

    // 建立一个数组将数值存储起来

    private static final String datas[] = new String[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };

    public static void main(String[] args) {
        // 从键盘接收数
        Scanner s = new Scanner(System.in);
        System.out.println("请输入金额");
        // 将接收的字符串转换成int数值
        int money = s.nextInt();

        System.out.println(convert(money));

    }

    public static String convert(int money) {
        StringBuffer sb = new StringBuffer("整");
        int init = 0;
        while (money != 0) {
            sb.insert(0, units[init]);
            int number = money % 10;
            sb.insert(0, datas[number]);
            money /= 10;// 把刚才处理过那数位去掉
             init++;
        }
        return sb.toString();

    }

}

input:

请输入金额
102452088

output

壹亿零仟贰佰肆拾伍万贰仟零佰捌拾捌圆整

顺便复习以下知识点。

(本人比较懒,感觉Google比较好用,就不看API文档了,本来想翻译一下,
但是觉的别人写得简单易懂,还是看英文文档原汁原味,就给整理过来了)

1.review: Scanner Class in java

Scanner is a class in java.util package used for obtaining the input of the primitive types
like int, double etc. and strings. It is the easiest way to read input in a Java program.

2.how to use:

To create an object of Scanner class, we usually pass the predefined object System.in, which represents the standard input stream.

We may pass an object of class File if we want to read input from a file.
To read numerical values of a certain data type XYZ, the function to use is nextXYZ().

For example, to read a value of type short, we can use nextShort()

To read strings, we use nextLine().

To read a single character, we use next().charAt(0). next() function returns the next token/word in the input as a string and charAt(0) funtion returns the first character in that string.

3.Let us look at the code snippet to read data of various data types.

  package com.my.review;
  import java.util.*;
  public class Scanner_review1 {

        public static void main(String[] args) {

        //首先我们定义并初始化一个scanner对象,等待键盘输入,只需定义并初始化一次就可以多次使用
        Scanner sc = new Scanner(System.in);

        // 接下读入一个字符串
        String name = sc.nextLine();
        //读入一个字符
        char gender = sc.next().charAt(0);

        //接下来是一些数值型的读入
        int age = sc.nextInt();
        long phoneNo = sc.nextLong();
        double price = sc.nextDouble();

        // 把这些值打印出来看看是否正确
        System.out.println("Name: " + name);
        System.out.println("Gender: " + gender);
        System.out.println("Age: " + age);
        System.out.println("Phone Number: " + phoneNo);
        System.out.println("price: " + price);

    }

input:

keen
F
23
1888888888
520.00 

output:

520.00
Name: keen
Gender: F
Age: 23
Phone Number: 1888888888
price: 520.0

4.有时我们也会常用到以下的用法

Sometimes, we have to check if the next value we read is of a certain type or if the input has ended (EOF marker encountered).

Then, we check if the scanner’s input is of the type we want with the help of hasNextXYZ() functions where XYZ is the type we are interested in. The function returns true if the scanner has a token of that type, otherwise false.

For example, in the above code, we have used hasNextInt().To check for a string, we use hasNextLine(). Similarly, to check for a single character , we use hasNext().charAt(0).

Let us look at the code snippet to read some numbers from console and print their mean.

package com.my.review;

import java.util.Scanner;

public class Scaner_review2 {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int sum =0;
    int count = 0;
    //check if an int values is avaliable
    while(sc.hasNextInt()) {
        //read an int value
        int num = sc.nextInt();
        sum+=num;
        count++;
    }
        int avg = sum /count;
        System.out.println("avg:"+avg);
    
  }
}

input

1
2
3
4
5
q //这里我使用一个 q 字母,代表退出,当你想停止并输出结果时, 在这里你只需输入非int型就可以了,否则键盘一直等待你的输入。

output

avg:3  

本文Scanner的巨大篇幅来自于Scanner 尊重原著,点击链接查看吧!!!

你可能感兴趣的:(JAVA Review(1))