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
Marek Pasierbek
Level 9
Poznań
  • 10.07.2020
  • 249views
  • 5comments

Why is not OK?

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

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; import java.util.Scanner; /* 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 [] table = new int[20]; int min = table[0]; int max= table[0]; Scanner scann = new Scanner(System.in); for(int i=0; i<20; i++) { table[i] = scann.nextInt(); } for(int i =0; i<20; i++) { if(table[i] < min) { min = table[i]; } if(table[i] > max) { max = table[i]; } } int maximum = max; int minimum = min; //write your code here System.out.print(maximum + " " + minimum); } }
0
Comments (5)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Marek Pasierbek Working at Nexus Polska
10 July 2020, 13:58
so any idea?
0
Angel Li
Level 18 , Fremont, United States
13 July 2020, 17:16
1. I found an error with your logic in line 26 and line 30 (the if statements) because since you set the min and max to table[0], when comparing table[0] to table [i] and i is equal to 0, you're comparing the same thing. So I think it should be >= and <=, but I'm not sure if it would make a difference. 2. When you are adding the numbers to your array, you are adding Strings, so maybe change line 21 to include Integer.parseInt()? Other than the two points above, your code is exactly like mine (although I used BufferedReader), so it should verify.
0
null
Level 10 , Lufeng, China
10 July 2020, 12:50
代码没有错误 可能是输出的格式不合要求
0
Angel Li
Level 18 , Fremont, United States
13 July 2020, 17:07
Let me translate the above message: The code doesn't have any errors. It's probably the output's format doesn't meet the requirements.
0
Marek Pasierbek Working at Nexus Polska
14 July 2020, 05:07
Yes, some days ago I did this task. Thank You for help. Sometimes we need to break and back to exercises.
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.