Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
March 20th, 2013, 08:57 PM
 Life_Student
Contributing User

Join Date: Mar 2013
Posts: 41
Time spent in forums: 16 h 8 m 19 sec
Reputation Power: 1
Larger num using a method

Ok here my current problem:
Create a main class file. (1) Inside create a method max() that has two floating point
parameters x and y and returns the larger of x and y; (2) In the main method, input two
numbers from the keyboard, and then use the max() method you created to compute the
larger value and output it.

this is what i have the problem is that teoutput tends to be the second number entered not the larger of the two, not sure how to change that.

import java.util.Scanner;
public class LargerNum {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter two values: ");
double FirstNum = input.nextDouble();
double SecondNum = input.nextDouble();
double Final = MaxMethod(FirstNum, SecondNum);

}
public static double MaxMethod(double L, double M){
double Temp = L;
double TempTwo = M;
double Large;
if (Temp >= TempTwo){
Large = Temp;}
else;

Large = TempTwo;
System.out.println(Large);
return Large;

}}

#2
March 20th, 2013, 09:16 PM
 Life_Student
Contributing User

Join Date: Mar 2013
Posts: 41
Time spent in forums: 16 h 8 m 19 sec
Reputation Power: 1
import java.util.Scanner;
public class LargerNum {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter two values: ");
double FirstNum = input.nextDouble();
double SecondNum = input.nextDouble();
double Final = MaxMethod(FirstNum, SecondNum);

}
public static double MaxMethod(double L, double M){
double Temp = L;
double TempTwo = M;
double Large;
if (Temp >= TempTwo){
Large = Temp;}
else
Large = TempTwo;
System.out.println(Large);
return Large;

}}
this code works but the println needs to be in the main method so how do i send the larger number determined in the maxmethod back to the main method for output?

 Viewing: Dev Shed Forums > Programming Languages > Java Help > Larger num using a method