PTA 1006

换个格式输出整数

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if (n < 0 || n >= 1000) {
            System.out.println("请输入一个0到999之间的正整数。");
            return;
        }
        //不用区分几位数
        int ge = n % 10;          // 个位
        int shi = (n / 10) % 10;  // 十位
        int bai = (n / 100);      // 百位
        //shi/bai = 0时,循环i<=0不成立 不会打印
        BaiPrint(bai);
        ShiPrint(shi);
        GePrint(ge);
    }
    public static void GePrint(int ge) {
        for (int i = 1; i <= ge; i++) {
            System.out.print(i);
        }
    }
    public static void ShiPrint(int shi) {
        for (int i = 1; i <= shi; i++) {
            System.out.print("S");
        }
    }
    public static void BaiPrint(int bai) {
        for (int i = 1; i <= bai; i++) {
            System.out.print("B");
        }
    }
}

最开始的版本 测试显示部分正确 没找到原因

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        //划分区间 重复代码多 没想清楚 shi/bai为0时的情况
        int ge = 0;
        int shi = 0;
        int bai = 0;
        if(n<10){
            ge = n % 10;
            GePrint(ge);

        }else if(n >= 10 && n < 100){
            ge = n % 10;
            shi = n / 10 % 10;
            ShiPrint(shi);
            GePrint(ge);
        }else if(n>=100 && n<1000){
            ge = n % 10;
            shi = n / 10 % 10;
            bai = n / 100 % 10;
            BaiPrint(bai);
            ShiPrint(shi);
            GePrint(ge);
        }

    }
    public static void GePrint(int ge){
        for(int i=1;i<=ge;i++){
            System.out.print(i);
        }
    }
    public static void ShiPrint(int shi){
        for(int i=1;i<=shi;i++){
            System.out.print("S");
        }
    }
    public static void BaiPrint(int bai){
        for(int i=1;i<=bai;i++){
            System.out.print("B");
        }
    }
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值