aviliocorteo
aviliocorteo
JCHJava Community | Help. Code. Learn.
Created by aviliocorteo on 1/8/2025 in #java-help
why is the method not returning the value and its always returning 0?
Main.java
package com.functions;


import java.util.Scanner;


public class Main {
private static int first;
private static int second;
private static int third;

public Main() {
first = 0;
second = 0;
third = 0;
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number one: ");
first = in.nextInt();

System.out.print("Enter number two: ");
second = in.nextInt();

System.out.print("Enter number three: ");
third = in.nextInt();

MaxandMin test = new MaxandMin();
test.getFirst();
test.getSecond();
test.getThird();
System.out.println(test.findMax());
System.out.println(test.findMin());
}
}
package com.functions;


import java.util.Scanner;


public class Main {
private static int first;
private static int second;
private static int third;

public Main() {
first = 0;
second = 0;
third = 0;
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number one: ");
first = in.nextInt();

System.out.print("Enter number two: ");
second = in.nextInt();

System.out.print("Enter number three: ");
third = in.nextInt();

MaxandMin test = new MaxandMin();
test.getFirst();
test.getSecond();
test.getThird();
System.out.println(test.findMax());
System.out.println(test.findMin());
}
}
MaxandMin.java
package com.functions;

public class MaxandMin {
private int first;
private int second;
private int third;

public MaxandMin() {
first = 0;
second = 0;
third = 0;
}

public int getFirst() {
first = 0;
return first;
}

public int getSecond() {
second = 0;
return second;
}

public int getThird() {
third = 0;
return third;
}

public int findMax() {
int max = first;

if(second > max) {
max = second;
} else if(third > max) {
max = third;
}
return max;
}

public int findMin() {
int min = first;

if(min < second) {
min = second;
} else if (min < third) {
min = third;
}
return min;
}
}
package com.functions;

public class MaxandMin {
private int first;
private int second;
private int third;

public MaxandMin() {
first = 0;
second = 0;
third = 0;
}

public int getFirst() {
first = 0;
return first;
}

public int getSecond() {
second = 0;
return second;
}

public int getThird() {
third = 0;
return third;
}

public int findMax() {
int max = first;

if(second > max) {
max = second;
} else if(third > max) {
max = third;
}
return max;
}

public int findMin() {
int min = first;

if(min < second) {
min = second;
} else if (min < third) {
min = third;
}
return min;
}
}
7 replies