Jack.Du 2021-12-25 09:01 采纳率: 20%
浏览 133

自己的代码一直在PTA上显示答案错误,如何解决?

img

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();

    for (int i = m; i <= n; i++) {
        String str = "";
        str += i + "=";
        int num = i;
        while(num % 2 == 0){
            str += 2 + "*";
            num /= 2;
        }
        while(num % 3 == 0){
            str += 3 + "*";
            num /= 3;
        }
        while(num % 5 == 0){
            str += 5 + "*";
            num /= 5;
        }
        while(num % 7 == 0){
            str += 7 + "*";
            num /= 7;
        }

        if (num % 2 > 0 && num % 3 > 0 && num % 5 > 0 && num % 7 > 0 && num > 1)
            str += num + " ";

        System.out.println(str.substring(0,str.length()-1));
    }
}

}

  • 写回答

2条回答 默认 最新

  • CSDN专家-sinJack 2021-12-25 09:33
    关注

    输出结尾无换行,你的有换行。

    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int m = sc.nextInt();
            int n = sc.nextInt();
    
            for (int i = m; i <= n; i++) {
                String str = "";
                str += i + "=";
                int num = i;
                while(num % 2 == 0){
                    str += 2 + "*";
                    num /= 2;
                }
                while(num % 3 == 0){
                    str += 3 + "*";
                    num /= 3;
                }
                while(num % 5 == 0){
                    str += 5 + "*";
                    num /= 5;
                }
                while(num % 7 == 0){
                    str += 7 + "*";
                    num /= 7;
                }
    
                if (num % 2 > 0 && num % 3 > 0 && num % 5 > 0 && num % 7 > 0 && num > 1)
                    str += num + " ";
    
                if(i==n){
                    System.out.print(str.substring(0,str.length()-1));
                }else{
                    System.out.println(str.substring(0,str.length()-1));
                }
            }
        }
    }
    

    img

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 12月25日

悬赏问题

  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题