CodeGym
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
Karolis Garšva
Level 16
Gravesend
  • 08.07.2021
  • 273views
  • 4comments

where is the mistake?

Question about the task Min and max in arrays
Java Syntax,  Level 7,  Lesson 12
Resolved

Create an array of 20 numbers.
Populate it with numbers from the keyboard.
Find the maximum and minimum numbers in the array.
Display the maximum and minimum numbers, separated by a space.

Requirements:
  • Create an array of integers (int[]) with 20 elements.
  • Read 20 integers from the keyboard and add them to the array.
  • Find the maximum and minimum integers, and then display them separated by a space.
  • Use a for loop.
package com.codegym.task.task07.task0721; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /* Min and max in arrays */ public class Solution { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int[] numbers = new int[20]; int maximum = numbers[0]; int minimum = numbers[0]; for (int i = 0; i < 20; i++) { numbers[i] = Integer.parseInt(reader.readLine()); } for (int i = 0; i < numbers.length; i++) { if (maximum < numbers[i]) { maximum = numbers[i]; } if (minimum > numbers[i]) { minimum = numbers[i]; } } System.out.print(maximum + " " + minimum); } }
0
Comments (4)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Karolis Garšva
Level 16 , Gravesend, United Kingdom
8 July 2021, 20:23
yes my output is 111 and -150. So whats the problem?
0
Thomas
Level 31 , Bayreuth, Germany
8 July 2021, 20:59solution
At that time you initialize minimum and maximum you haven't filled the array with values and therfore minimum and maximum get assigned 0.
+1
Karolis Garšva
Level 16 , Gravesend, United Kingdom
8 July 2021, 21:12
Thanks mate, the problem was not finding min and max. if I would look at the code for another hour I would figured it out my self :D
0
Guadalupe Gagnon
Level 37 , Tampa, United States
8 July 2021, 19:50
Try entering 20 negative numbers. For example: -1, -2, -3, -4, -5, ... -20 The correct output would be: "-1 -20"
+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.