0
Why dont we explain whats the name in the code?
class MyClass { static void sayHello(String name) { System.out.println("Hello " + name); } public static void main(String[ ] args) { sayHello("David"); sayHello("Amy"); } }
10 odpowiedzi
+ 1
modulo operator %:
division with rest:
5/3 = 3 rest 2
4/2 = 2 rest 0
1/5 = 0 rest 1
If you want to get the rest of division you use % --> 5 % 3 = 2; 4 % 2 = 0. You need this operator to check if a number is divisble by another number.
In my example I want to check if a number is even, means that the number have to be divisible by 2. (rest = 0)
The method returns true if the number is even.
You call the method (boolean even = isEven (4)) --> jump in the method --> return true --> leave the method
Thats why its not necessary to write else return false.
Make small steps. Start with a Scanner and think about your needed inputs. Users: String; price: double; product String (jpg is difficult for a beginner so do this later if you familiar enough with java --> reading a file in java? using graphics in java?)
Arrays are possible or you can take a List like ArrayList. Make familiar with both and look whats better for you.
+ 1
The method expects a String:
String name;
If you call the method, name will be initialized:
sayHello ("David"):
String name = "David";
I hope I have understand your question.
+ 1
Inside the brackets you write what the method should expect:
The first part is the type of the variable, the second part is the name of the variable
For example:
int age
String country
Long number ...
If the method do not need anything you write nothing inside the brackets.
+ 1
Static void sayHello (){
System.out.println ("Hello")
}
Static void squareNum (int x){
System.out.println (x*x);
}
public static void main (String [] args){
sayHello ();
int number = 4;
squareNum (number);
squareNum (12);
}
+ 1
sayHello or squareNum is the name of the method. Normally you choose a name that explain what the method is doing.
System.out.println is a method and you know that is printing something.
void means that the method don't return a value.
You can not write String x = sayHello ();
Or int x = squareNum (4);
Read more about methods in the sololearn lessons then you will see the difference.
+ 1
ok ok i got it this
static void sayHello(String name) {
System.out.println("Hello" + name) does the trick
this return thing is confusing do
+ 1
For short:
Public static int sum (int a, int b){
return a + b;
}
int number = sum (4,5);
This method take two int and return the sum (also an int)
public static boolean isEven (int number){
if (number % 2 == 0){
return true;
}
return false;
}
boolean even = isEven (4);
take a number; return a boolean
0
so the " ( ) " stands for string and whats in it is name
0
Bro but the method isnt sayHello or aquareNum right... this is what we make up right?
the method here in the code is
void right?
0
that boolean part is difficult.. because ure not using the Else so how does system know when to use treu or false damn i got to learn also about this modulo thing because % doesnt stand for percentage but for remembering if I understanded it right.
how does the coding work if you want to have users that can ad products with jpg price etc
is this more focussing on arrays and scanner? I know i have a long road untill i got this