java - Learning to use methods, keep getting error: "cannot convert from void to int" -
i'm trying make script prints out biggest , smallest number using methods, have no clue why methods don't work... placed comment next keep getting error.
i don't need getting script work appreciate it, if explain me why method doesn't work.
here code:
import java.util.scanner; public class minmax { public static void main(string[] args) { int nr1, nr2, nr3, biggest, smallest; scanner lukija = new scanner(system.in); system.out.print("input first value: "); nr1 = lukija.nextint(); system.out.print("input second value: "); nr2 = lukija.nextint(); system.out.print("input third value: "); nr3 = lukija.nextint(); biggest = biggest(nr1, nr2, nr3); // here keep getting error smallest = smallest(nr1, nr2, nr3); // here system.out.print(biggest + " biggest number."); system.out.print(smallest + " smallest number."); } public static void biggest(int nr1, int nr2, int nr3){ int biggest = 0; if (nr1>nr2 && nr1>nr3){ nr1=biggest; } else if (nr2>nr1 && nr2>nr3){ nr2=biggest; } else if (nr3>nr1 && nr3>nr2){ nr3=biggest; } } public static void smallest(int nr1, int nr2, int nr3){ int smallest = 0; if (nr1<nr2 && nr1<nr3){ nr1=smallest; } else if (nr2<nr1 && nr2<nr3){ nr2=smallest; } else if (nr3<nr1 && nr3<nr2){ nr3=smallest; } } }
modify return
signature of method(s), assign value smallest
(or biggest
) , return variable. like,
public static int biggest(int nr1, int nr2, int nr3){ int biggest = 0; if (nr1>nr2 && nr1>nr3){ biggest = nr1; } else if (nr2>nr1 && nr2>nr3){ biggest = nr2; } else if (nr3>nr1 && nr3>nr2){ biggest = nr3; } return biggest; } public static int smallest(int nr1, int nr2, int nr3){ int smallest = 0; if (nr1<nr2 && nr1<nr3){ smallest = nr1; } else if (nr2<nr1 && nr2<nr3){ smallest = nr2; } else if (nr3<nr1 && nr3<nr2){ // nr3=smallest; smallest = nr3; } return smallest; }
you simplify above like
public static int biggest(int nr1, int nr2, int nr3){ int biggest = math.max(nr1, nr2); return math.max(biggest, nr3); } public static int smallest(int nr1, int nr2, int nr3){ int smallest = math.min(nr1, nr2); return math.min(smallest, nr3); }
Comments
Post a Comment