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
Sargam
Level 3
Aligarh
  • 20.10.2018
  • 915views
  • 2comments

can' t find the error

Question about the task Minimum of three numbers
Java Syntax,  Level 2,  Lesson 8
Under discussion

Write a function that computes the minimum of three numbers.

Hint:
You need to write the body of the existing min function.

Requirements:
  • The program should display text on the screen.
  • The min method should not display text on the screen.
  • The main method should call the min method four times.
  • The main method should display the result of the min method. Each time, on a new line.
  • The min method must return the minimum of the numbers a, b, and c.
package com.codegym.task.task02.task0216; /* Minimum of three numbers */ public class Solution { public static int min(int a, int b, int c) { if(a<b && b<c) return a; else if(b<a && a<c) return b; else return c;//write your code here } public static void main(String[] args) throws Exception { System.out.println(min(1, 2, 3)); System.out.println(min(-1, -2, -3)); System.out.println(min(3, 5, 3)); System.out.println(min(5, 5, 10)); } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Anindya Bhattacharya
Level 5 , Karimpur, India
21 October 2018, 16:46
Use "<=" Another logic - public class Solution { public static int min(int a, int b, int c) { if(a <= b & a <= c) return a; else if(b <= a & b <= c) return b; else return c;//write your code here }
0
Titex
Level 3 , Stockholm, Sweden
20 October 2018, 21:47
Are you really sure that all variables return the min value? When I run your code I get: 1 -3 3 10 Some numbers are the same, look at this line
System.out.println(min(5, 5, 10));
The first statement you have is that if a is less than b, which it's not because 5 is not less than 5 The second statement you have is if b is less than a, which it's not because 5 is not less than 5 The third statement says that if none of the above is correct, then display c. This is why you get 10 A good example would be to check if it's less than or equal to, you do this by typing the less than operator followed by equal operator. Your first check would look like this.
if (a <= b && b <= c)
return a
What that says is basicly, is a less than or equal to b and is b less than or equal to c then return a. Now don't use that code because it's not complete, I'll give another example to describe why it would fail. You are so close though but I would think about what you compare. Take this line
System.out.println(min(3, 5, 3));
With your first comparison, if a is less than b and if b is less than c, then display a. This will never happen, because b is not less than c. As a last tip, compare a to both b and c and as a second check compare b against a and c. Now you only have c left. Good luck :)
+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.