Cześć W :P
Zastanawiam się dlaczego mamy 3 różne s. If u know What I mean :>
Wcześniej tylko ostatnia zmienna o tej samej nazwie zostawała aktualna zmienną.
Nie rozumiem dlaczego program wyświetla 4 razy te 3stringi zamiast tylko ostatniego Stringa 4 razy. Dlaczego w tym przypadku tak jakby 3 różne s są uznawane, skoro zmienne nie mogą mieć tej samej nazwy.
asia
Poziom 3
Dlaczego są 3 różne s?
Dyskutowane
Komentarze (3)
- Popularne
- Najnowsze
- Najstarsze
Musisz się zalogować, aby dodać komentarz
asia
2 grudnia 2021, 13:22
// thank u.
Of course 300 times 🤪 I'm a sadomasohist 🤪
0
Guadalupe Gagnon
1 grudnia 2021, 15:04
Think of a method as a reusable snippet of code that can be written once and used multiple times. Each time a method is called it runs a unique instance of the method. Calling it once or thousands of times matters not.
so,
After writing a method you can use it by calling it (typing its name) and passing it the proper arguments, in this case a String, inside parentheses. The argument passed from the calling point becomes the variable that is in the parentheses of the method, in this case String s, and MUST match that type:
So in this code, from the task, line 9 declares the method. It has one parameter (the parameters are in parentheses) of a String named s. In the main() method (yes, main is a method) the print method is called 3 times on lines 3, 4, and 5. Each call passes a String argument to the print() method which matches what line 9 says must be passed to the method. +1
Guadalupe Gagnon
1 grudnia 2021, 15:06
The benefit to using methods is that you can write less code. In this task, lets say you use 4 lines of code to print s 4 times:
Because methods are just "snippets", that would be the same exact thing as just typing this:
This is a small example, but if you were to call the print() method a lot more then you could save yourself a lot of typing. Imagine a method that had 30 lines of code in it that was called 10 times. That would be 40 lines of code to type. If you didn't use the method and just typed 30 lines of code in the 10 spots where the method would have been called you would need 300 lines of code. Another point, would you rather debug 40 lines of code or 300 lines of code? +1