0
Why the following is not working
import java.util.Scanner; class Program { public static void main(String[ ] args) { Scanner number = new Scanner(System.in); int a,b; System.out.println("enter a"); a = number.nextint; System.out.println("enter b"); b = number.nextint; static max(int a , int b){ if(a > b) { return a; } else { return b; } } }
15 odpowiedzi
+ 2
I wrote the code on here and have it working.. check my codes for one I named "Yours"
+ 1
A number of things. try this in your main method..
Scanner number = new Scanner(System.in);
System.out.println("enter a");
int a = number.nextInt();
System.out.println("enter b");
int b = number.nextInt();
max(a,b);
and your method should go outside of your main method. try putting this either before or after your main method.
public static int max(int x, int y){
if(x > y) {
return x;
}
else {
return y;
}
}
+ 1
What you call the parameters in your max function doesn't matter. Leaky Egg renamed them for readability: If the names are the same, one might think they had something to do with a and b. They do not. If you had another pair of int, you could put them in as well.
+ 1
x and y are just arguments for your method. What variable names you use for your method arguments don't matter, but it's best to not use names you already use for variables. Since you already have a variable of a and b, I just used 2 different variable names of x and y.
when you call your method, you give value to x and y. here we called the method and gave x the value of a and y the value of b
max(a,b);
or you could just assign x and y numbers.
max(3,5)
Main is a method. The method that runs when your program starts. you can put your scanner and your inputs here if you want, or you could write another method to handle that.
0
bro please explain y the scanner and stuff in main and after that we write our method and in our method we are comparing x and y and not a and b ... ??? I didn't get it
0
okay thank you very much I got some idea about it ... I ask again after trying again on pc :)
0
I did as I wrote leaky but it's not proceeding
0
what error is it throwing?
0
duplicate local variable .... a and same for b
illegal modifier for parameter Max; only final is permitted
syntax error on token "(" , ; expected
syntax error on token "," , ; expected
void methods cannot return a value
at Mymethods.main(Mymethods.java:7)
0
I'm not sure on all of it, but duplicate local variable means you try to initiate the a and b variables multiple times.
int a .. this initiates variable a .. when you want to use that variable either to get a value or set a value, you no longer need to put int before it. it is already declared. putting int in front of a again trys to declare it again.
int a;
a=5;
a=a+a;
notice I declared it only once then I can use it as much as I want
Void.. when you are writing a method, and you name it something like
public void max() ... it does something, but it doesn't return a value. that's essentially what void means. if you write something like
public static int max() ... calling int in the method name tells the method is is going to return an integer. at the end of the method it looks for a return statement with an integer.
what IDE are you using? ones like Eclipse can help keep some of this straight for you
0
@LeakyEgg okay I got that point not to use int again and again ... and IDE I am using is eclipse only brother ... it displays crosses or tip at the left side but since I have no professor to rely on I am relying on this app and you guys for my doubts .... well coming back to the code after changing to only a crosses got removed then to return a value it asked me to change void to int that is public static int main(string[]args){ and now only cross remains on the int max line ..... it says illegal modifier for parameter max only final is permitted .... 2 syntax error .... and after running with error ... the output displays main method must return a value of type void in class my methods , please define main method as
public static void main(string[]args) ........ I am confused
0
public static void main (String[] args) {
}
this is the standard main.. do you have
return;
anywhere in your main function? main can't be used to return values, only to run code or call methods.
0
I have no where return in main I just wrote the same codes what u told at first all issues are resolved except the last one I told u
0
I am at work at the moment, in a couple hours when I get home if you haven't solved the issues, I will have you email me your code and I will put it in my eclipse and see what's up
- 1
thank you brother that will help me a lot