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
JeRiF94
Level 22
Baku
  • 08.05.2019
  • 1435views
  • 1comment

Wanna help

Question about the task Task about algorithms
Java Syntax,  Level 10,  Lesson 11
Resolved

Read 30 numbers from the keyboard. Display the 10th and 11th smallest numbers.

Explanation:

The lowest number is the 1st smallest.
The next lowest after that is the 2nd smallest

For example:
1 6 5 7 1 15 63 88
The first smallest is 1
The second smallest is 1
The third smallest is 5
The fourth smallest is 6

Requirements:
  • The program must read data from the keyboard.
  • The program should display text on the screen.
  • The Solution class must have two methods.
  • The sort() method must sort an array of elements.
  • The main() method should call the sort() method.
  • The program should display the 10th and 11th smallest numbers. Each value should be on a new line.
package com.codegym.task.task10.task1020; import java.io.BufferedReader; import java.io.InputStreamReader; /* Task about algorithms */ public class Solution { public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int[] array = new int[30]; for (int i = 0; i < 30; i++) { array[i] = Integer.parseInt(reader.readLine()); } sort(array); System.out.println(array[9]); System.out.println(array[10]); } public static void sort(int[] array) { for (int i = 1 ; i < array.length ; i++){ int indexOfMin = i; for (int j = i + 1 ; j < array.length ; j++){ if (array[indexOfMin] > array[j]){ indexOfMin = j; } } int temp = array[indexOfMin]; array[indexOfMin] = array[i]; array[i] = temp; } } }
0
Comments (1)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Sergio
Level 25 , Moscow, Russian Federation
8 May 2019, 11:10solution
See the line 26. The initial value for i must be 0, and last index to check is:
i < array.length - 1
+4
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.