CodeGym
Promotion
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
Jeff Jeffy
Level 15
Irvine
  • 11.03.2019
  • 1156views
  • 3comments

I don't know whats wrong.

Question about the task Maximum of four numbers
Java Syntax,  Level 4,  Lesson 6
Resolved

Use the keyboard to enter four numbers, and display the maximum of them. If the maximum occurs more than once, just display it once.

Requirements:
  • The program should read the numbers from the keyboard.
  • The program must display a number on the screen.
  • The program should display the maximum of four numbers.
  • If there are several maximum numbers, display any of them.
package com.codegym.task.task04.task0419; /* Maximum of four numbers */ import java.io.*; import java.util.Scanner; public class Solution { public static void main(String[] args) throws Exception { //write your code here Scanner in=new Scanner(System.in); int a=in.nextInt(); int b=in.nextInt(); int c=in.nextInt(); int d=in.nextInt(); int x=a; if (a<b){ x=b; if(b<c){ x=c; } else if(b<d){ x=d; } } else if(a<c){ x=c; if(c<b){ x=b; } else if(c<d){ x=d; } } else if(a<d){ x=d; if(d<b){ x=b; } else if(d<c){ x=c; } } System.out.println(x); } }
0
Comments (3)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Okanlawon Oluwatobi Damilare
Level 9 , Banjul, Gambia
18 March 2019, 20:21
try BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int a; int b; int c; int d; try{ String sa = reader.readLine(); a = Integer.parseInt(sa); String sb = reader.readLine(); b = Integer.parseInt(sb); String sc = reader.readLine(); c = Integer.parseInt(sc); String sd = reader.readLine(); d = Integer.parseInt(sd); int ma = minimum(a,b); int mb = minimum(c,d); if(ma > mb){ System.out.println(ma); }else{ System.out.println(mb); } }catch(IOException e){ e.printStackTrace(); } } private static int minimum(int a, int b){ if(a > b){ return a; }else{ return b; }
0
Serg
Level 17 , Minsk, Belarus
19 April 2019, 12:28
this tasks is about cycles not math methods and the purpose of it is to learn how to use cycles
0
Guadalupe Gagnon
Level 37 , Tampa, United States
11 March 2019, 13:56solution
The big problem with your code is the you are doing way too much with your if and else ifs. On top of this, using else-ifs you are stopping execution of the code before everything is evaluated properly. Lets say that you enter the numbers a = 1,b = 2,c = 3, d = 4, stepping through your code would look like this: At line 20 this would return true because 1 is less than 2 at line 21 x would be set to 2 at line 22 this would return true because 2 is less than 4 at line 23 x would be set to 3 Nothing else would evaluate and line 47 would print 3 even though 4 is the largest value. I'll message you a solve.
+2
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.