CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
Fa Yu
Level 16
Tashkent
  • 03.06.2019
  • 1047views
  • 4comments

It works perfect as per conditions, but won't verify. Help me please to find out the issue.

Question about the task Task about algorithms
Java Syntax,  Level 5,  Lesson 12
Archived


Write a program that:
1. reads a number N (must be greater than 0) from the console
2. reads N numbers from the console
3. Displays the maximum of the N entered numbers.

Requirements:
  • The program should read the numbers from the keyboard.
  • The program must display a number on the screen.
  • The class must have a public static void main method.
  • Don't add new methods to the Solution class.
  • The program should display the maximum of the N entered numbers.
  • The program should not display anything if N is less than or equal to 0.
package com.codegym.task.task05.task0532; import java.io.*; /* Task about algorithms */ public class Solution { public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int maximum = 0; int N = Integer.parseInt(reader.readLine()); int[] num = new int[N]; for (int i = 0; i < N; i++) { num[i] = Integer.parseInt(reader.readLine()); if (num[i] >= maximum) maximum = num[i]; } //write your code here System.out.println(maximum); } }
0
Comments (4)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
P.B.Kalyan Krishna
Level 22 , Guntur, India
3 June 2019, 16:04
You have taken '0' as the maximum. This won't work in all circumstances. Suppose I give negative integers as input. You have to find maximum amongst them. Let {-1,-2,-3,-8,-65} be the array. All of them are negative. So what is the maximum amongst them? Clearly -1. But according to your program maximum is '0', which is not even present in the array. So what is the answer? One way is to designate the first element of the array as maximum. Compare every other element with this and if there is any other element greater than this then it will be the new maximum. Or let the smallest negative integer in the 'int' range be the maximum and compare every other element in the array with it. These two approaches will work.
+1
Guadalupe Gagnon
Level 37 , Tampa, United States
3 June 2019, 15:54
The variable maximum should not be set to an initial value of 0. After N, what if the user enters all negative numbers? Your code would output 0, but that would not be the max. Fix this and you will pass.
0
Fa Yu
Level 16 , Tashkent, Uzbekistan
3 June 2019, 15:57
Thanks. I found out solution in other answers. But I am not aware in my 5th level about Integer.MIN_VALUE :))
+1
Guadalupe Gagnon
Level 37 , Tampa, United States
3 June 2019, 16:16
Everyone makes the same mistake the first time with this problem. I did.
+1
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.