回 帖 发 新 帖 刷新版面

主题:本人菜鸟,对这个有点不解,请好心人教教

import java.util.Scanner;
public class Sum {
    public Sum(int m)
    {
        for(int i=1;i<m/2;i++)
        {
            int sum=0;
            for(int j=i;j<m/2;j++)
                {
                    sum+=j;
                    if(sum==m)
                    {
                        System.out.print(m+"=");
                        for(int k=i;k<=j;k++)
                            {
                                
                                System.out.print(k+"+");
                                System.out.println();
                            }
                        break;
                    }
                    if(sum>m)
                        break;
                }
            
            
        }
        
    }
    public static void main(String[] args)
    {
        Scanner in=new Scanner(System.in);
        System.out.println("请输入整数");
        int b=in.nextInt();
        new Sum(b);
        
        
    }

}
运行不起来,不知道为什么,请大家帮帮忙啊

回复列表 (共2个回复)

沙发

你这个程序没错误 ,不过是输出的格式不正确,你看看我改的
import java.util.Scanner;
public class Sum {
    int k;
    public Sum(int m)
    {
        for(int i=1;i<m/2;i++)
        {
            int sum=0;
            for(int j=i;j<m/2;j++)
                {
                    sum+=j;
                    if(sum==m)
                    {
                        System.out.print(m+"=");
                        for( k=i;k<j;k++)//修改的就是这个循环
                            {
                                
                                System.out.print(k+"+");
                                
                            }
                        System.out.print(k);
                        break;
                    }
                    if(sum>m)
                        break;
                }
            System.out.println();
            
        }
        
    }
    public static void main(String[] args)
    {
        Scanner in=new Scanner(System.in);
        System.out.println("请输入整数");
        int b=in.nextInt();
        new Sum(b);
        
        
    }

}

板凳

这个程序  功能不全吧。

我来回复

您尚未登录,请登录后再回复。点此登录或注册