timi先生 2020-06-07 18:13 采纳率: 50%
浏览 234
已采纳

有没有大佬帮忙看看,程序为什么能运行但报错不出结果。

package zh.codegym.task.task05.task0532;

import java.util.Scanner;
/*
有关算法的任务
*/

public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int i;

    if (N > 0) {
        int max = 0;

        for (i = 1; i <= N; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            max = a < b ? a : b;


        }
        System.out.println(max);
    }
    if(N<=0){
        System.out.println();
    }

}

}
编写程序,使其:
1. 从控制台读取数字 N(必须大于 0)
2. 从控制台读取 N 个数字
3.显示 N 个输入数字中的最大值。

要求:
1.程序应从键盘读取这些数字。
2.程序必须在屏幕上显示一个数字。
3.该类必须包含 public static void main 方法。
4.不要向 Solution 类添加新方法。
5.程序应显示 N 个输入数字中的最大值。
6.如果 N 小于或等于 0,程序不应显示任何内容。
绝望到家了,啥都满足了。就是没满足条件5,气死我鸟了。有大哥告诉我问题出在哪了么!!

  • 写回答

1条回答 默认 最新

  • dxt_snow 2020-06-08 01:14
    关注
    import java.util.Scanner;
    
    public class Solution {
        public static void main(String[] args){
            Scanner scan = new Scanner(System.in);
            int N = scan.nextInt();
            if(N > 0){
                int max = scan.nextInt();   //假设第一个数是最大的
                int i = 1;
                while(i < N){
                    int next = scan.nextInt();  //不断读取下一个数
                    i++;
                    if(max < next){
                        max = next;     //将读到的数与当前最大值比较,维护最大值变量
                    }
                }
                System.out.println(max);
            }
            scan.close();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作