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
CodeGym/Help with Java Tasks/What's wrong with my code
Anonymous #427092
Level 3
Erode
  • 23.03.2019
  • 1059views
  • 3comments

What's wrong with my code

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) { //write your code here if((a<b) && (a<c)) return a; else if((b<a) && (b<c)) return b; else return c; } 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 (3)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Rajat Singh
Level 4 , Delhi, India
26 March 2019, 10:46
Try this out
public class Solution {
    public static int min(int a, int b, int c) {
        if((a<=b) && (a<=c))
        {
            return a;
        }
        if((b<=a) && (b<=c))
        {
            return b;
        }
        if((c<=a) && (c<=b))
        {
        return c;
        }
        return 0;//write your code here
    }
0
Akash Tripathi
Level 6 , Delhi, India
24 March 2019, 03:42
just use <= instead of <. Solved
0
Khurram
Level 16 , Lahore, Pakistan
23 March 2019, 13:42
in the last case min(5, 5, 10) a = 5, b = 5, c = 10 first condition is false because 5 is not less than 5 (a < b is false) second condition is also false, again (b < a is false) so the answer becomes c = 10 you need to cater the cases where the numbers could be equal
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 a 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.