xiaobaimengxino 2021-10-16 17:17 采纳率: 87.5%
浏览 20
已结题

用户指定列表个数并输入列表,判断该列表是否升序,我代码有两错误,帮我看看要怎么改,thanks

 public static void main(String[] args){
        /*让用户输入列表数的个数*/
        System.out.print("enter the size of the list");
        Scanner input=new Scanner(System.in);
        int geshu=input.nextInt();
        /*让用户输入列表*/
        int [] shuzu=new int[geshu];
        System.out.print("enter the contents of the list:");
        for(int i=0;i<geshu;i++){
            shuzu[i]= input.nextInt();
        }
        /*检查是否升序*/
        int a=shuzu[0];
        for(int j=1;j<geshu;j++){
            if(shuzu[j]>=a){
                a=shuzu[j];
                continue;
            }
            else {
                boolean buer=true;
                break;
            }
        }
        /*显示结果*/
        System.out.print("the list has 9 integers :"+shuzu[++]);/*expression expected*/
        if(buer==true){/*cannot resolve symbol 'buer' */
            System.out.print("the list is already sorted");
        }
        else
            System.out.print("the list is not sorted");
    }


  • 写回答

1条回答 默认 最新

  • 字菁悠心 2021-10-16 20:46
    关注

    第25行没有看懂,第26行buer没有定义为方法里面的全局变量,是有问题的,建议改成如下:

    public static void main(String[] args){
            /*让用户输入列表数的个数*/
            System.out.print("enter the size of the list");
            Scanner input=new Scanner(System.in);
            int geshu=input.nextInt();
            /*让用户输入列表*/
            int [] shuzu=new int[geshu];
            System.out.print("enter the contents of the list:");
            for(int i=0;i<geshu;i++){
                shuzu[i]= input.nextInt();
            }
            /*检查是否升序*/
            int a=shuzu[0];
            boolean buer=false;
            for(int j=1;j<geshu;j++){
                if(shuzu[j]>=a){
                    a=shuzu[j];
                    continue;
                }
                else {
                    buer=true;
                    break;
                }
            }
            /*显示结果*/
            System.out.print("the list has 9 integers :"+shuzu.length);/*expression expected*/
            if(buer==true){/*cannot resolve symbol 'buer' */
                System.out.print("the list is already sorted");
            }
            else
                System.out.print("the list is not sorted");
        }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月25日
  • 已采纳回答 10月17日
  • 创建了问题 10月16日

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示