保全头发 2023-05-09 20:34 采纳率: 42.9%
浏览 18
已结题

关于#java输入输出#的问题,如何解决?


package 实验文件;

import java.util.ArrayList;
import java.util.Scanner;

public class BoxMain {

    public static void swap(int a, int b){
        int m;
        m=a;a=b;b=m;
    }
    public static void sort(int[] a, int n)
    {
        int temp;
        for(int i=0;i<n-1;i++)                   //比较n-1次(第一次循环表示趟数)
        {
            for(int j=0;j<n-i-1;j++)              // 最后一次比较a[n-i-1]与a[n-i-2]   (第二次循环表示比较次数 )
            {
                if(a[j]<a[j+1])
                {
                    swap( a[j+1],a[j]);
                }
            }
        }
    }


    public static void main(String[]args){

        Scanner sa=new Scanner(System.in);

        int Boxvolume;
        System.out.println("输入箱子大小");
        Boxvolume= sa.nextInt();    //输入箱子大小
        int goods;
        System.out.println("输入货物多少");
        goods= sa.nextInt();        //输入货物多少

        int []goodvolume=new int[1000];
        int []resivolume=new int[1000];

        System.out.println("输入每一个货物大小,一共需要"+goods+"个");
        for (int i=0;i<goods;i++){
            goodvolume[i]= sa.nextInt();    //输入每一个货物大小
        }

        sort(goodvolume,goods);

        int resivolumle[][]=new int[1000][1000];    //定义每个箱子承装的货物代码
        int box_count=0;            //定义箱子个数
        resivolume[box_count]=Boxvolume;            //定义第一个箱体的剩余体积

        for(int i=0;i<goods;i++){
            int shouru=1;
            for (int j=0;j<=box_count;j++){
                if (goodvolume[i]<=resivolume[j]){
                    resivolume[j]=resivolume[j]-goodvolume[i];
                    shouru=0;
                    int num=0;
                    ArrayList mak=new ArrayList();
                    for (int m=0;m<resivolumle[i].length;m++){
                        if (resivolumle[i][m]!=0){
                            num++;
                        }
                    }

                    resivolumle[i][num]=i+1;

                    break;
                }
            }
            if (shouru==1){
                box_count+=1;
                resivolume[box_count]=Boxvolume-goodvolume[i];
                resivolumle[box_count][0]=i+1;
            }
        }

        int num=box_count+1;
        System.out.println("一共需要"+num+"个箱子");


        for(int i=0;i<=box_count;i++){
            int j=i+1;
            System.out.print("第"+j+"个箱子装有下面货物:");
            int numx=0;
            /*ArrayList mak=new ArrayList();*/
            for (int m=0;m<resivolumle[i].length;m++){
                if (resivolumle[i][m]!=0){
                    numx++;
                }
            }


            for (int m=0;m<=numx;m++){
                System.out.print(resivolumle[i][m]+" ");
            }
            System.out.println("");
        }
    }
}

为什么我的输入输出是这样的,代码哪里有问题

输入箱子大小
100
输入货物多少
6
输入每一个货物大小,一共需要6个
60 45 35 20 20 20
一共需要3个箱子
第1个箱子装有下面货物:1 0
第2个箱子装有下面货物:2 0
第3个箱子装有下面货物:6 0

  • 写回答

2条回答 默认 最新

  • Leodong. 2023-05-09 21:03
    关注

    该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:
    根据您提供的代码,问题可能出在以下几个地方:

    1. 输入货物大小时,您使用了 sa.nextInt() 方法。这个方法会读取一个整数,但是它不会读取行末的换行符。因此,在下一次读取输入时,程序会读取到上一次输入的换行符,而不是您期望的输入。为了解决这个问题,您可以在每次读取整数之后,使用 sa.nextLine() 方法读取换行符。

    2. swap 方法中,您使用了值传递的方式来交换两个数值,这是不正确的。在 Java 中,方法参数都是值传递的,不能直接通过方法交换两个变量的值。如果您想要实现交换两个变量的值,可以使用一个临时变量来完成。

    3. sort 方法中,您的冒泡排序算法的实现有一些问题。具体来说,您在交换两个数值时,使用了错误的参数。应该使用数组中的下标来交换两个数值,而不是数值本身。另外,您的排序算法是倒序排列的,如果想要正序排列,可以把 if(a[j]<a[j+1]) 改成 if(a[j]>a[j+1])

    下面是修改后的代码:

    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class BoxMain {
    
        public static void swap(int[] a, int i, int j) {
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    
        public static void sort(int[] a, int n) {
            for (int i = 0; i < n - 1; i++) { // 比较n-1次(第一次循环表示趟数)
                for (int j = 0; j < n - i - 1; j++) { // 最后一次比较a[n-i-1]与a[n-i-2]
                                                        // (第二次循环表示比较次数)
                    if (a[j] > a[j + 1]) {
                        swap(a, j, j + 1);
                    }
                }
            }
        }
    
        public static void main(String[] args) {
    
            Scanner sa = new Scanner(System.in);
    
            int Boxvolume;
            System.out.println("输入箱子大小");
            Boxvolume = sa.nextInt(); // 输入箱子大小
            sa.nextLine(); // 读取换行符
    
            int goods;
            System.out.println("输入货物多少");
            goods = sa.nextInt(); // 输入货物多少
            sa.nextLine(); // 读取换行符
    
            int[] goodvolume = new int[1000];
            int[][] resivolumle = new int[1000][1000];
            int[] resivolume = new int[1000];
            int box_count = 0;
    
            System.out.println("输入每一个货物大小,一共需要" + goods + "个");
            for (int i = 0; i < goods; i++) {
                goodvolume[i] = sa.nextInt(); // 输入每一个货物大小
                sa.nextLine(); // 读取换行符
            }
    
            sort(goodvolume, goods);
    
            resivolume[box_count] = Boxvolume;
    
            for (int i = 0; i < goods; i++) {
                int shouru = 1;
                for (int j = 0; j <= box_count; j++) {
                    if (goodvolume[i] <= resivolume[j]) {
                        resivolume[j] = resivolume[j] - goodvolume[i];
                        shouru = 0;
                        int num = 0;
                        for (int m = 0; m < resivolumle[j].length; m++) {
                            if (resivolumle[j][m] != 0) {
                                num++;
                            }
                        }
                        resivolumle[j][num] = i + 1;
                        break;
                    }
                }
                if (shouru == 1) {
                    box_count += 1;
                    resivolume[box_count] = Boxvolume - goodvolume[i];
                    resivolumle[box_count][0] = i + 1;
                }
            }
    
            int num = box_count + 1;
            System.out.println("一共需要" + num +"个箱子");
    
            for (int i = 0; i <= box_count; i++) {
                int j = i + 1;
                System.out.print("第" + j + "个箱子装有下面货物:");
                int numx = 0;
                for (int m = 0; m < resivolumle[i].length; m++) {
                    if (resivolumle[i][m] != 0) {
                        numx++;
                    }
                }
                for (int m = 0; m < numx; m++) {
                    System.out.print(resivolumle[i][m] + " ");
                }
                System.out.println("");
            }
        }
    }
    

    希望这些修改能够帮助您解决问题。


    如果以上回答对您有所帮助,点击一下采纳该答案~谢谢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月17日
  • 已采纳回答 5月9日
  • 创建了问题 5月9日

悬赏问题

  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
  • ¥15 振荡电路,ADS仿真
  • ¥15 关于#c语言#的问题,请各位专家解答!
  • ¥15 这个如何解决详细步骤
  • ¥15 在微信h5支付申请中,别人给钱就能用我的软件,这个的所属行业是啥?