CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Start learning now
  • All questions
Brandon
Level 9
Toledo
  • 13.05.2020
  • 338views
  • 4comments

What does this want to happen for negative numbers?

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

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 count; boolean first = true; int n = Integer.parseInt(reader.readLine()); if (n > 0) { for (count = 0; count < n; count++) { int a = Integer.parseInt(reader.readLine()); if (first) { maximum = a; first = false; } maximum = (a > maximum ? a : maximum); } }else; if (maximum <= 0) ; else System.out.println(maximum); } }
0
Comments (4)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Ashish RajAnand
Level 13 , Bhilai , India
13 May 2020, 04:17
let take -2,-3,-4,-5 //ans = - 2 you set maximum =0; then your output is 0. correct it store first number in maximum
0
Brandon
Level 9 , Toledo, United States
13 May 2020, 05:13
I did. In mine, doing it your way my answer would be -2. It would be set in lines 22-25. I'm not sure how it's supposed to be set up, but I'm pretty sure my way works completely.
0
Ashish RajAnand
Level 13 , Bhilai , India
13 May 2020, 07:23solution
see at line 33-36 ad remove it. this is reason your code not work for negative number. simply print maximum
+3
Brandon
Level 9 , Toledo, United States
13 May 2020, 13:24
Thanks, I just misunderstood that last one.
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.