So I am trying to create a program which takes two integers i input, and returns the bigger of the two. So it would function like this when I type it in Terminal
java Assign5 23 67
the bigger value of the two is 67
So far this is what I have
//Main Method
public class asign5 {
public static void main (String[] args){
int num1;
int num2;
num1 = Integer.parseInt(args[0]);
num2 = Integer.parseInt(args[1]);
System.out.println("The larger value of the two is: " + getMax(num1, num2));
}
}
//getMax Method
public static int getMax(num1, num2){
if (num1 > num2) { return num1; }
else
{return num2;}
}
}
It keeps telling me in my main method that getMax is not a valid method although I have created the getMax method. In the getMax method it keeps telling me that public static int is synthax. I've actually spent the last two hours 开发者_JAVA技巧on this roadblock, but have not figured out myself and it's frustrating my brains out. Can someone help me?
You've closed the asign5
class declaration after main
, so the getMax
method isn't actually in a class at the moment - just remove the second closing brace from before the getMax
method. If you had appropriate indentation, this should be obvious: I'd recommend using either an IDE or a text editor which is Java-aware so that you can see this sort of thing very easily.
You also need to change the getMax
method signature to specify types for num1
and num2
:
public static int getMax(int num1, int num2)
精彩评论