CodeGym
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
Tay2von
Level 6
Oklahoma City
  • 13.01.2021
  • 296views
  • 1comment

ALL TASKS PASS EXCEPT ONE.... What am I missing?

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


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.*; import static java.lang.Integer.MIN_VALUE; /* Task about algorithms */ public class Solution { public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int maximum = Integer.MIN_VALUE; int num = Integer.parseInt(reader.readLine()); for (int i = 0; i < num; i++) { int nextInt = Integer.parseInt(reader.readLine()); if (nextInt > maximum) { maximum = nextInt; } else { if (num > maximum) maximum = num; } }if(maximum != MIN_VALUE){ System.out.println(maximum); } } }
0
Comments (1)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Guadalupe Gagnon
Level 37 , Tampa, United States
13 January 2021, 04:06
You do get a number N at line 17 and call it "num", then use it as a counter for the loop. That part is done well. The first problem is that you then use the num to set maximum if you enter the lowest value of an int at line 25. The num is only a counter and should not be used as a number that sets maximum. The next problem is that this code assumes that the user, after entering N, then enters numbers that are greater than the min value of an int. Assuming that the first problem is fixed, If I entered 1 for N, then entered the min value of an int this code would not output anything because maximum would still be equal to MIN_VALUE and the if statement at line 27 would filter that out but it should output what I entered. These problems need to be fixed for your code to pass.
0
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.