• java 面试每日一题3


    题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Scanner;
    
    public class testThree {
        public static void main(String[] args) throws IOException{
            System.out.println("请输入a的值");
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            int a=Integer.parseInt(br.readLine());
            System.out.println("请输入b个数:");
            BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
            int b=Integer.parseInt(br.readLine());
            /*Scanner sc = new Scanner(System.in);
            int a = sc.nextInt();*/
            System.out.println("请输入b的值");
    //        int b = sc.nextInt();
            int s=count_value(b,a);
            System.out.println(s);
        }
    
    private static int count_value(int i,int a){
        if (i <= 0) {
        return 0;
        } else {
        return a * i + count_value(i - 1,a) * 10;
        }
        }
    }

    知识补充:BufferedReader 中read()和readLine()

    read():
    方法功能:读取单个字符。
    返回:作为一个整数(其范围从0 到65535 (0x00-0xffff))读入的字符,也就是说是char型的ASCII码。是如果已到达流末尾,则返回-1。

    readLine():
    方法功能:读取一个文本行。通过下列字符之一即可认为某行已终止:换行(' ')、回车(' ') 或回车后直接跟着换行。
    返回:包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回null

  • 相关阅读:
    consumer详解
    消费幂等
    死信队列
    消息重试
    负载均衡
    存储层
    producer消息
    消息发送与接收
    TCC
    form表单提交前进行加密
  • 原文地址:https://www.cnblogs.com/tjlgdx/p/5883465.html
Copyright © 2020-2023  润新知