Learn Programming with Java Comments
Comments
-
Very Exciting
-
it's very interesting for me !
-
I can't wait to get started
-
hope its not hard
-
So excited! :)
-
Cant' wait to learn more about it
-
Its pretty great!
-
I can't wait to get started
-
new passion
-
Is very good
-
Your Site Is Simply Incredible.
-
I hope I get this right, it's so exciting!
-
I'm so excited
-
___________________▄▄▄▀▀▀▀▀▀▀▄
_______________▄▀▀____▀▀▀▀▄____█
___________▄▀▀__▀▀▀▀▀▀▄___▀▄___█
__________█▄▄▄▄▄▄_______▀▄__▀▄__█
_________█_________▀▄______█____█_█______▄█_____________▀▄_____▐___▐_▌
______██_______________▀▄___▐_▄▀▀▀▄
______█________██_______▌__▐▄▀______█
______█_________█_______▌__▐▐________▐
_____▐__________▌_____▄▀▀▀__▌_______▐_____________▄▄▄▄▄▄
______▌__________▀▀▀▀________▀▀▄▄▄▀______▄▄████▓▓▓▓▓▓▓███▄
______▌____________________________▄▀__▄▄█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▄
______▐__________________________▄▀_▄█▓▓▓▓▓▓▓▓▓▓_____▓▓____▓▓█▄
_______▌______________________▄▀_▄█▓▓▓▓▓▓▓▓▓▓▓____▓▓_▓▓_▓▓__▓▓█
_____▄▀▄_________________▄▀▀▌██▓▓▓▓▓▓▓▓▓▓▓▓▓__▓▓▓___▓▓_▓▓__▓▓█
____▌____▀▀▀▄▄▄▄▄▄▄▄▀▀___▌█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓__▓________▓▓___▓▓▓█
_____▀▄_________________▄▀▀▓▓▓▓▓▓▓▓█████████████▄▄_____▓▓__▓▓▓█
_______█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█▓▓▓▓▓██▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓██▄▄___▓▓▓▓▓█
_______█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█▓▓███▓▓▓▓████▓▓▓▓▓▓▓▓▓▓▓▓▓██▓▓▓▓▓▓█
________█▓▓▓▓▓▓▓▓▓▓▓▓▓▓█▓█▓▓██░░███████░██▓▓▓▓▓▓▓▓▓▓██▓▓▓▓▓█
________█▓▓▓▓▓▓▓▓▓▓▓▓▓▓██▓░░░░░█░░░░░██░░░░██▓▓▓▓▓▓▓▓▓██▓▓▓▓▌
________█▓▓▓▓▓▓▓▓▓▓▓▓▓▓███░░░░░░░░____░██░░░░░░░██▓▓▓▓▓▓▓██▓▓▌
________▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓██░░░░░░░________░░░░░░░░░██████▓▓▓▓▓█▓▌
________▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓██░░░░░░___▓▓▓▓▓░░░░░░░███░░███▓▓▓▓▓█▓▌
_________█▓▓▓▓▓▓▓▓▓▓▓▓▓██░░░░░___▓▓█▄▄▓░░░░░░░░___░░░░█▓▓▓▓▓█▓▌
_________█▓▓▓▓▓▓▓▓▓▓▓▓▓█░░█░░░___▓▓██░░░░░░░░▓▓▓▓__░░░░█▓▓▓▓██
_________█▓▓▓▓▓▓▓▓▓▓▓▓▓█░███░░____▓░░░░░░░░░░░█▄█▓__░░░░█▓▓█▓█
_________▐▓▓▓▓▓▓▓▓▓▓▓▓▓█░█████░░░░░░░░░░░░░░░░░█▓__░░░░███▓█
__________█▓▓▓▓▓▓▓▓▓▓▓▓█░░███████░░░░░░░░░░░░░░░▓_░░░░░██▓█
__________█▓▓▓▓▓▓▓▓▓▓▓▓█░░░███████░░░░░░░░░░░░░░_░░░░░██▓█
__________█▓▓▓▓▓▓▓▓▓▓▓▓█░░░███████░░░░░░░░░░░░░░░░░░░██▓█
___________█▓▓▓▓▓▓▓▓▓▓▓▓█░░░░███████░░░░░░░░░░░█████░██░░░
___________█▓▓▓▓▓▓▓▓▓▓▓▓█░░░░░░__███████░░░░░███████░░█░░░░
___________█▓▓▓▓▓▓▓▓▓▓▓▓▓█░░░░░░█▄▄▄▀▀▀▀████████████░░█░░░░
___________▐▓▓▓▓▓▓▓▓▓▓▓▓█░░░░░░██████▄__▀▀░░░███░░░░░█░░░
___________▐▓▓▓▓▓▓▓▓▓▓▓█▒█░░░░░░▓▓▓▓▓███▄░░░░░░░░░░░░░░░______▄▄▄
___________█▓▓▓▓▓▓▓▓▓█▒▒▒▒█░░░░░░▓▓▓▓▓█░░░░░░░░░░░░░░░▄▄▄_▄▀▀____▀▄
__________█▓▓▓▓▓▓▓▓▓█▒▒▒▒█▓▓░░░░░░░░░░░░░░░░░░░░░____▄▀____▀▄_________▀▄
_________█▓▓▓▓▓▓▓▓▓█▒▒▒▒█▓▓▓▓░░░░░░░░░░░░░░░░░______▐▄________█▄▄▀▀▀▄__█
________█▓▓▓▓▓▓▓▓█▒▒▒▒▒▒█▓▓▓▓▓▓▓░░░░░░░░░____________█_█______▐_________▀▄▌
_______█▓▓▓▓▓▓▓▓█▒▒▒▒▒▒███▓▓▓▓▓▓▓▓▓▓▓█▒▒▄___________█__▀▄____█____▄▄▄____▐
______█▓▓▓▓▓▓▓█_______▒▒█▒▒██▓▓▓▓▓▓▓▓▓▓█▒▒▒▄_________█____▀▀█▀▄▀▀▀___▀▀▄▄▐
_____█▓▓▓▓▓██▒_________▒█▒▒▒▒▒███▓▓▓▓▓▓█▒▒▒██________▐_______▀█_____________█
____█▓▓████▒█▒_________▒█▒▒▒▒▒▒▒▒███████▒▒▒▒██_______█_______▐______▄▄▄_____█
█▒██▒▒▒▒▒▒█▒▒__▒▒▒█▒▒▒▒▒▒▒▒▒▒▒▒▒▒█▒____▒█▓█__▄█__█______▀▄▄▀▀____▀▀▄▄█
█▒▒▒▒▒▒▒▒▒▒█▒▒▒████▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█_____█▓▓█▓▓▌_▐________▐____________▐
█▒▒▒▒▒▒▒▒▒▒▒███▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█▒_____█▓▓▓█▓▌__▌_______▐_____▄▄____▐
█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█▒▒____█▓▓▓█▓▓▌__▌_______▀▄▄▀______▐
█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒███████▓▓█▓▓▓▌_▀▄_______________▄▀
█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒███▒▒▒▒▒▒▒██▓▓▓▓▓▌__▀▄_________▄▀▀
█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒██▒▒▒▒▒▒▒▒▒▒▒▒▒█▓▓▓▓▓▀▄__▀▄▄█▀▀▀
█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒██▓▓▓▓██▄▄▄▀
█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒████
█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█
█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█▒▒▒█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█▄▄▄▄▄
_█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█▒▒▒▒█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒███▒▒▒▒▒▒██▄▄
_█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█▒▒▒▒█▒▒▒▒▒▒▒▒▒▒▒▒███▒▒▒▒▒▒▒▒▒▒▒▒▒█▄
█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█▒▒▒▒█▒▒▒▒▒▒▒▒▒▒▒█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█
__█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█▒▒▒▒█▒▒▒▒▒▒▒▒▒██▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█
___█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█▒▒▒▒█▒▒▒▒▒▒▒▒█▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░▒▒▒▒▒▒▌
__█▒▒▒▒▒▒▒▒▒▒▒▒▒██▒▒▒▒▒▒▒█▒▒▒▒█▒▒▒▒▒▒█▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░░▒▒▌
____█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█████████████▒▒▒▒▒█▒▒▒▒▒▒▒▒░░░░▒▒▒▒▒▒▒▒▒▒▒░▒▌
_____█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█_______▐▒▒▒▒█▒▒▒▒▒▒▒░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░▌
______█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█________█▒▒█▒▒▒▒▒▒░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▌
_______█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█________█▒█▒▒▒▒▒▒░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▌
________█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█________█▒▒▒▒▒▒░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█
_________█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█________█▒▒▒▒░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█
_________█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█________█▒▒▒░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒█▀
__________█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█_______█▒░░░▒▒▒▒▒░░░░░░░░▒▒▒█▀▀▀
___________█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█_______█░▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░█▀
____________█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█_______█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█▀
_____________█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█_______█▒▒▒▒▒▒▒▒▒▒▒▒█▀
_____________█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█_______▀▀▀███████▀▀
______________█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█
_______________█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█
________________█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█
_________________█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█
__________________█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒██▒█
___________________█▒▒▒▒▒▒▒▒▒▒▒▒▒██▒▒▒▒█
___________________█▒▒▒▒▒▒▒▒████▒▒▒▒▒▒▒█
___________________█████████▒▒▒▒▒▒▒▒▒▒▒█
____________________█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█
____________________█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█
_____________________█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░▌
_____________________█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░▌
______________________█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░▌
_______________________█▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░█
________________________█▒▒▒▒▒▒▒▒▒▒▒░░░█
__________________________██▒▒▒▒▒▒░░░█▀
_____________________________█░░░░░█▀
_______________________________▀▀▀▀ -
:P your welcome
-
What the fudge is going on why can't I continue
-
cool
-
this is so cool
-
ممتاز لدجة لا يصدّق
-
Jewel of India | Indian Restaurant | Now Open Weekend Lunch Buffet | Seattle
4735 University Wy NE, Seattle, WA 98105
+12065235275
Seattle is home to a diverse population, and this diversityis reflected in its culinary scene. An Indian Cuisine Restaurant In Seattle WA, serves as a culinary oasis where locals and visitors can enjoy traditional Indian dishes. These restaurants pride themselves on using fresh, high-quality ingredients and authentic spices to create meals that transport you straight to the heart of India.
-
Heatwave Product Manufacturers is a leading B2B platform connecting buyers and suppliers in the thermal product industry. Specializing in a
diverse range of heatwave solutions, we cater to businesses seeking high-quality heating equipment, temperature control systems, and thermal management products.
-
it will become harder
-
how do you know.
-
it does become much harder
-
im on arrays now
-
is there a way to reset your progress?
-
This bit is rather scary after you finish reading the whole thing. It is however very simple.
-
It looks very hard and difficult but just you wait and see when you apply it
-
Cool
-
correct me if i'm wrong
"return" has no mathematical function. It is to like "return 5+5" to return the sum of 5+5 to the screen so when the program runs it shows the sum of 5+5 ?
I know I'm not clear but if someone understands pls reply :P -
return is used in a method to return a value to the code that called it (see next page). This is similar to how a mathematical function returns a value.
-
It will be easy if you read the article carefully
-
its not as complicated as it looks
-
I read quick help
-
This one was a doozy for me. I tried to change the variable name(doStuff) and create new values within the name variable I created. Anyways I must have written 3 or 4 lines, just to find out I only needed to simple lines :( .
-
I love this site. Thank you very much!
-
Brilliant way to teach! I have been searching through out the whole web for a sophisticated JAVA learn by doing site! Really glad to say that I have fortunate enough to experience learneroo.com! :)
-
I don't solve the problem...
Would someone help me?import java.util.Scanner;
public class Main {
public static int doStuff(int a, int b){
a =10;
b =20;int sum=a+b;
return(sum)/2;a= 14;
b = -14;int sum=a+b;
return(sum)/2;} //your code here
}
//boilerplate code public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); for(int i=0; i<n; i++){ int a = in.nextInt(); int b= in.nextInt(); int result = doStuff(a, b); System.out.println(result); } }
}
-
Awesome!
-
Wow Simple
-
Excellent site, I have tried codebat, codeacdemy, udemy, tutorialpoint, learnjavaonline.org, http://docs.oracle.com/javase/tutorial/ but this is the best so far
int mySum = a + b; //creates variable to hold sum of a = b
return mySum/2; //return average -
The quotes are definitely motivating :)
-
It is taking a very long time to show the results and it is really annoying.
-
I was more confused about the value of int a and int b then anything i couldn't work out how it had come to the results if no number have been declared. so i was trying to declare it the whole time which failed me.
-
i think this code will be efficient if we generalize both the numerator and denominator with a variable(s) and give a condition where denominator is >0
-
Its not as complicated as it first seems. Just remember how the order of operations works when you write the code.
-
Hi @Yuta, the code is simple
int sum = a + b;
return sum / 2;you are putting variable sum inside parenthesis which is wrong.
-
ahh specifying parameters is what got me as well Mezbaur
-
-
the simplest answer is to put return a/2+b/2 for you idiots who can't figure it out for yourselves
-
Have tried several times but getting "Compilation failed" Where do I go to check the correct coding?
-
lol..seriously? made several lines and giving values to int a and b. This is fun
-
can i use public static void main (string []args) instead of public static int doStuff
-
If you look at the code on the bottom you'll see that main method there, which calls the method doStuff. (See static and main methods for more info.)
-
just put return a/2+b/2 where it says your code here
yuta -
oh That was so easy.
-
public static int doStuff(int a, int b){ int sum = a + b; return (sum)/2; }
-
It takes more time to run the code
-
return (a+b)/2;
Is a better answer then storing it on a variable first -
this site is very useful to me to learn javacoding..
-
There is no value of A & B then how it's working. tell me any one
-
what is use of public and static
-
this one is hard for me
actually have no idea what to do -
i had to cheat
-
-
Easy I'm liking this website.
-
Enter Integer or Decimal Answer: I expect int sum == answer
-
I love this site. Now every other book is looking like a reference sheet.
-
You say "In the following code, what number will sum equal?"
It would be better to say "In the following code, what number will sum become?"
Also, you should give an extra example of multi-line assignment of variables before testing student understandingint x = 1;
int y = 333;
int total = x+y \ total is a container containing the number 334
int x = 5; \ x is now assigned the value of the number 5
int total = x+y \ total is a container which has been changed to contain the number 338Perhaps using sum or total as variable names is not a good idea. Maybe int z = x + y
For beginners, operators can be easily confused with variables -
This is amazing thank you so much
-
Thanks to this website, I dont feel like a dumb as.. anymore!
-
so good, I like it so much!
-
Good site so far :)
-
I'm not able to answer.
-
It's easy. Forget the int x = 2 and go to x = 100 and add the y to the x.
-
I have increased my programming knowledge by 500% in the last 10 minutes.
-
In the code why it is written
int x = 2;
int y = 5;
x = 100; .Can anyone tell me why data type int is not written befor x=100; -
As mentioned on the previous page, you need to declare a variable's type the first time, but afterwards you can use it without re-mentioning what type it is.
-
Yes, but i want to learn programming. And isnt this a bit to easy to really learn programming?
-
You can start off easy. There's lot's of challenging material later if you want it!
-
ok i want that challenging stuff :-)
-
When you're ready, check out Algorithms
-
we will, but we are not ready yet :-)
-
this is just like js and c
-
This was my hardest challenge yet...How does 100 + 5 = 105 if x=2???? Computers are DU|\/|@$$3$ !!!! If X(2)=100 And X(1)=2 and Y(5) then wouldn't x+x+y =107? the only place xxy ever made sense was in Klinefelter syndrome! :p
-
Thanks.
-
Got confused here since x = 100; wasn't given any data type.
-
Great Website! Thanks.
-
This website makes a lot of sense, because a lot of people are not employed or partially employed and can't afford the high monthly costs of other training programs. Also, the questions are simple
enough to build confidence rather than destroy confidence as many other training sites, (including free ones) do. Learneroo, you have my loyalty.
-
Am so great-full for the amazing challenges on this website
-
I solved this problem with first declaring sum and mul as integers. Then returning their values using modulus.
-
the question confused me, when did i have to multiply at any point???
-
@Ryan, the product of
a
andb
means multiplya
byb
. Would this be clearer:
Multiplya
byb
and then get the remainder from dividing that product by the sum ofa
andb
? -
Yeah, thank you. The product made me think addition but i guess that would be the sum. Maybe i should have taken more math classes lol. So far this has been the best teaching program i have used
online. Some of the ?'s are somewhat difficult but thats what learning is about. Thank you for the email and reply.
-
Excellent tutorial thus far. It is probably hard to understand the precise syntax that is needed to pose the problems.
-
i was so confused by the wording of product. to me it processed as the same thing as sum :/
-
I read the challenge about 5 times and operated exactly what they wrote: return (a*b)%(a+b);
I hope this helps. Wording can be tricky but it's all going back to basic math terminology.
-
The remainder of 5 % 3 could have been -1 too.
-
This one tripped me too. It can be done this way as well:
int product = a * b;
int sum = a + b;
return product % sum; -
PEMDAS is also known as BEDMAS (brackets instead of parentheses)
-
I am a math teacher so this wasnt to hard for me :-)
-
I have a question... I understand that you can just return the value of "(a * b) % (a + b);" ... But I also tried doing this one by assigning to value to a new variable, int C. I added an int
C in public static int, and set the equation equal to C, returned C, but the code didn't work. How would I store its value to a new variable?
-
i got confused, i got it when i read the question again.
-
Good way of learning..
-
that's really awsome man
-
My solutions was like ArtTric
-
I don't know where to input int product = a*b;
Int sum = a+b; -
I got it correct after 3 tries. I may be wrong but I surmise that '%'is to be put between code that identifies what variables you are working with such as 'sum' and really means: "divide and also give me the remainder".
-
yeeyee
-
something is wrong here because how is 5*1 divided by (5+1) have a remainder of 5?
basic math says its .8 with a remainder of 2. so 5*1 is 5. well 5 divided by 6 equals 0.8 r2
-
I get the code, but it has a flaw because 5*1 divided by 6 does not have a remainder of 5. its 2
because basic math says that 5/6 is 0.8 with a remainder of 2.
-
guys i did this
//int num
//(1*5)%(6);
return (a*b)%(a+b) -
int mul = a * b;
int sum = a+b;
int result = mul%sum;
return result; -
What is going on in this solution:
Applying the rules for Math and Operator Order of Precedence
1. addition is performed first because of ( )
2. multiplication is the next operation- modulus is the last operation performed before returning the results
solution:
return ( a * b % ( a + b )); -
it can be also done by this:
int product = a+b;
return (a*b)%product;
// product is the sum of A and B, i could have named it anything// -
This is really hard :/
-
Meho the Wormcloack finds this easy
-
This is about the properties of int...
-
Cool problem, I like how you link it to a current problem that coding can be used to explore :D Keep up the good work guys! By the way, is / performing integer division?
-
@Stanley, if both numbers are integers,
/
will lose the decimal place from the answer. -
Ah, I was a bit confused but now I realise that we only want integer outputs for the Collatz Conjecture anyway. Thanks for the help :D
-
public class Main {
public static int doStuff(int a){ int NrQift = a % 2; if(a == NrQift){ return a/2; } else{ return 3 * a + 1;
} }
I'm going to take a break, until I get a reply for this question.
This is what I tried doing here, btw the variable name "NrQift" is from my language(albanian) meaning "even nr".So can someone help me out here ? I'm not trying to cheat, don't tell me the answer just help me out by explaining how do I do in the "if( How do I make the code know I want a even number as a condition for the code in the { } )
I also tried doing if(a % 2) but that didn't work either.
Tyvm -
I figured it out already after giving a bit more thought on the "Hint" xD Ty anyway whoever was gonna reply :)
-
Pretty sure this should work...
if (a == a % 2) {
return a / 2;
}
else {
return (a * 3) + 1;
}why not?
-
@ArtTric
That won't work because a%2 should equal 0 (meaning that there is no remainder and therefore the number is even) and not equal to a. -
So simple...
int c=a%2; // take remainder of the input and save it
//in variable c.if(c==0){ return a/2; //if the remainder == 0.
// then return a/2; } else{ return 3*a+1; //if its not then .... }
-
I've done some work on the Collatz Conjecture before, so I kinda knew the answer. This is simpler than what has been posted already:
if (a % 2 != 0) { // odd
return (a * 3) + 1; // a times 3 plus 1
} else { // even
return a / 2; // a divided by 2
} -
@mina the operation needs to be on the right side. Its just a programming rule. example :
if (x - 2 = 0) // error
if (x = 2) // correct
it wont work in this example but thats just the rule.
-
Why was I wrong?
if (a % 2 == 0) {
return a / 2;
}
else {
return (a * 3) + 1;
} -
this is hard :/
-
Took me a while but finally figured it out. One thing that kept tripping me up was =. I understand that the equal sign in Java is used to say that an expression is true right? When is it appropriate
to use the equal sign then. I kept trying to use it in my if statement , that's what was messing me up.
-
See Variables in Programming and Algebra for
=
vs.==
. -
The solution is as straight-fwd as the problem, so apply simple maths and follow syntax.
-
I want to become a hacker, so i guess i have to learn all of this first :-)
-
needs more sauce
-
How we are using doStuff method in main class ?
-
what is wrong in below mentioned code
package basicpgm;
import java.util.Scanner;public class Main {
public static int doStuff(int a){ return 5*3+1; } public static void main(String[] args) { int result = doStuff(a); System.out.println(result); } }
-
if ( (x & 1) == 0 ) { even... } else { odd... }
the low bit will always be set on an odd number. -
This is so good I love this
-
import java.util.Scanner;
public class Main {
public static int doStuff(int a){ if(a%2 != 0){ return(3*a + 1); else{ return (a/2);
} } //your code here } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); for(int i=0; i<n; i++){ int a = in.nextInt(); int result = doStuff(a); System.out.println(result); } }
} this is my code. The compiler is throwing a single error.
/usercode/Main.java:8: error: 'else' without 'if'
else{
^
1 error******* Cna somebody please help -
if (a%2==0) { return a/2; } else { return 3*a+1; } i dont know what is wrrong in this
-
I have figured out:
import java.util.Scanner;public class Main {
public static int doStuff(int a){ a=5; int s=a%2; int b=2; int c=4;
int d=6; int e=8; int f=0; int g=1; int h=3; int i=5; int j=7; int k=9; if(s==b){ a=a/2; } else if(s==c){ a=a/2; } else if(s==d){ a=a/2; } else if(s==e){ a=a/2; } else if(s==f){ a=a/2; } else if(s==g){ a=3*a+1; } else if(s==h){ a=3*a+1; } else if(s==i){ a=3*a+1; } else if(s==j){ a=3*a+1; } else{ a=3*a+1; } return a; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); for(int i=0; i<n; i++){ int a = in.nextInt(); int result = doStuff(a); System.out.println(result); } }
}
-
this will do it
// this if/else block will give result for any number a
if (a%2>0){
return 3*a+1
}
else{
return a/2; -
I do it.
-
ya this is really confusing
-
the only thing confusing about it is the use of the == vs the =. This concept is in most programming languages so if you program in other languages, this isn't confusing at all.
If (you == dont){
return str x = 'get used to it';
} -
this is crazy hard to me..
-
how do i remove the cheat mark my submission was the exact answer :/
-
@Jason, sorry there's no way (currently) to remove the cheat once the cheat button has been clicked.
-
yes this course is very good idea but is should be motivated by java tutorials like beginner ,intermediate,advanced
-
hey im sorry but im still stuck in this on challenge everything looks perfect but i'm still getting errors please help..
-
@Jason, you want to increase the iterator each time. For example, to print the numbers from 1 through 10, you could create a loop like this:
for(int i=1; i<= 10; i=i+1){ printNum(i); }
You need to do that to go from a to b instead.
-
I love the random quotes ♥
-
why is mine not working!!!
-
@Robert, make sure to declare any variable you use, even if it's just used as a loop index.
-
I do understand the While Loop and the example about wanting to repeat something 10 times, but I have a question ( quite stupid really but I just wanna make sure even though I know what is correct xD )
So If we have
{ int index = 1;
while(index <= 10){
doSmth();
index = index + 1
}
}
so now after executing this program the index will at sometime be 2 3 4 5 6 7 8 9 and in the end 10.
Does this mean that after every time the code is executed the { int index = 1 } changes to 2 3 4 5 6 7 8 9 or 10 from the "index = index + 1" ?
I know it does but does that appear in the "int index = ?" -
@Adhurim, "int index=1" is outside the while loop, so it is executed only 1 time.
-
I really was tempted to click the hint for this one but I sat there and thought about the ways to make it work. I still have trouble with the for loop but by just setting int i = a would have been so simple. Oh well I'm glad I got it to work with the while loop but I should avoid it because of the possibility of errors.
-
I did that and although it is correct but I should think of simpler way as the model answer :D :
public static void doStuff(int a, int b){
int tmp = a;
for (int i = 0 ; i< b-a+1;i++)
{printNum(tmp); tmp= tmp+1; } }
-
Yeah this one is tough. There should be an explanation of how to include multiple indexes in the first part of the for condition. Do you just use the & and also there is no explanation on how to printout inclusive information i.e. 1-5
-
Are we supposed to solve with the while loop or the "for" loop?
-
it says error:for(int i=a;i<=b;i+1){
not a statement ^ -
it says error:for(int i=a;i<=b;i+1){
not a statement ^ -
you have to put: i = i+1; or you can do an abreviation, like i++;
-
This is a great course. I am more comfortable with Java now, and it has only been 30 minutes.
-
int kq=0; for(int i=0;kq<b;i=i+1){ kq=a+i; printNum(kq); }
-
-
yeah i have the same thing but different variables ( i used n) and it's not working.
-
while (a <= b) {
printNum(a++);
} -
import java.util.Scanner;
public class Main {
public static void main(String[] args){Scanner input = new Scanner (System.in); System.out.print("Enter 1st number: ");
int num1=input.nextInt(); System.out.print("Enter 2nd number: "); int num2=input.nextInt(); for (int i=num1; i<=num2; i++){ System.out.print(i + " "); } }
}
-
for the for loop challenge:
for (int i=a; i<=b;i++) {
printNum (i);
}this basically assigns i as being a for the first time since a is less than b; the condition is to display including
b, so i must be less or equal than b and then increment it. the body of the for loop prints i by calling the printNum method.
-
for (int i = a; i <= b; i++){
System.out.println(i);
} -
just replace all the code with:
import java.util.Scanner;
public class Main {public static void doStuff(int a, int b){
long x= a;
while(x<b+1){
System.out.print(x+" ");
x = x + 1;
}} public static void printNum(long x){ } //boilerplate code public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); for(int i=0; i<n; i++){ int a = in.nextInt(); int b= in.nextInt(); doStuff(a, b); System.out.println(""); } }
}
-
Can I use While loop for the problem code above?
-
The only issue I had was thinking that he wanted actual numbers, then I realized that this is a module that gets called rather than core code. Oops.
-
i did the right code but the part that was already done further in the code was wrong. help me?
public static void printNum(int x){
system.out.print (x+"");
}
it says illegal start of expression. -
I think you need to be careful not to add extra curly braces or remove existing ones.
-
i did. i even reset the page and re wrote the code
-
i am confusion
-
I like this site :DD
-
huthtiuhithruhriurutrrihrwuerwuuherwhuewuhrwwwhutwhuwtwthuwuhwwetuewhewherihuerwihuweiuhwetuhiuhweiuhweuheuhweuhweuweiuuhiguwuuwgewuewgugewuwgueugwuhewuhwfuhuewuwguhhuueguwuhgwgwuegueghuegwupoohuguhgrughurwuhghuruhrwurhwhrhowhguhwoprugruowhwiphrwuogugrwurhrguwgwghughurwhuuorwhuhuwhgurgwugugrwuuwuphrwpuruhuwrhugrhgwhugrghughgurh7guhe7rguyher7h7uteiyhtyeu5yitgyhrueyhuheuighiuerhgiurhu
-
no comments? has no one finished this yet? its not that difficult...
-
@Mitchell, many people have solved it, just not many commented here. We don't want users to paste solutions or long code samples here, but you can link to your most recent submission instead by clicking on "Load Link" below the comment form.
-
Will the answer that was given run forever since the condition is less than or equal to a.?
-
Bernard, it will stop when 'i' in the for statement increments up to the same number as the 'a'.
-
int kq=1;
for(int i=a;i>0;i=i-1){
kq=kq*i;
}
return kq; -
You can solve this with recursion.
public static int doStuff(int a){
if (a==0 || a==1) return 1;
return doStuff(a-1)*a;
} -
I just gave 2 as the initial value of the variable i so I think my code will just jump the first useless iteration: 1*1.
my code -
import java.util.Scanner;
public class Factorial {
public static void main(String[] args){Scanner input = new Scanner (System.in); System.out.print("Enter a number to calculate: ");
int num=input.nextInt(); int result=1; for (int i=1; i<=num; i++){ result = result*i; } System.out.println(result); }
}
-
int b = a;
while(a!=1)
{
b = b*(a-1);
a -= 1;
}
return b; -
why don't we get infinate free lessions!?!?!?this is the only good site i have found )=
)=
)= -
Here's a coupon that gives you membership for $2 for the first month: LEARNCODING99
Enter it at learneroo.com/get-membership before checking out.
(Membership goes to regular rate afterwards, but you can cancel if you're no longer interested.) -
It doesn't seem to compile my answer but it runs on Eclipse just fine.
Here is my codepublic class DataTypes {
public static void main(String[] args) { int a = 2; double b = 7.5; double sum = a + b; System.out.println("The sum is" + sum); }
}
-
@William the only class that can be public is Main. So either remove 'public' or rename the class to 'Main'.
-
First getting an error while compiling about return statement missing, wheb added about incompatible types - required String, found double
Here the code:public static String doStuff(int a, double b){
double sum = a+b;
return sum;
String message = "The sum is "+ sum;
}Hint and cheat missing, what's wrong in my code? Thx
-
@Honzis, you need to return a String that contains the correct message. So remove
return sum
and returnmessage
instead. -
Thanks for the quick answer, got it now. :)
-
What is the code to solve this
-
@pat, I added an explanation, which you can 'cheat' to view.
-
Don't system.out.print, return.
-
@William, in this question You should implement code in doStuff() method and return the message. my code
-
@Honzis , You should return only a string type in doStuff() method. because return type of doStuff method is string, check my code :) my code
-
Or you could cast a double on the sum my code
-
Just FYI, I had originally put my string as "The sum is: " + sum. It said it was incorrect because I put the : in. So, even though it ran fine, gave the correct answer, etc., it showed incorrect
due to that. Just for any others who may have missed it for something that tiny and couldn't figure out why what they were doing was wrong.
-
import java.util.Scanner;
public class DataTypes
{
public static void main(String[] args)
{
int a;
double b;
Scanner input = new Scanner(System.in);System.out.println ("Enter an integer number: "); a = input.nextInt(); System.out.println ("Enter an double number: "); b = input.nextDouble(); double sum = a+b; String result = "Your sum is " + sum; System.out.println (result); }
}
-
The exercise is to get you to understand how to attach a variable into a string, over complicating the output or process only clouds the issue. keep it simple like this...
String answer = "The sum is " + (a+b);
return answer; -
This error was returned. What is the problem with this syntax?
Main.java:6: error: unexpected type
return ((a%b) = 0);
^
required: variable
found: value
1 error -
=
is an assignment, you probability want an equality check==
-
I didn't finish reading the whole lesson but what if
{
boolean haveMoneyLeft = yourMoney > yourDebts;
}What if I have a debt of 500gold and yourMoney is 500gold too
this makes:
{
boolean haveMoneyLeft = false;
}
right ? -
Nice this was an easy one. thanks again for the great site.
-
import java.util.Scanner;
public class BooleanExm
{
public static void main(String[] args)
{
double a;
double b;
Scanner input = new Scanner(System.in);System.out.println ("Enter any number: "); a = input.nextDouble(); System.out.println ("Enter any number: "); b = input.nextDouble(); boolean output = a%b==0; System.out.println (output); }
}
-
import java.util.println;
public class Main(){
public static void main(String[] args){
int a = 4;
int b = 2;
if(a%b==0){
System.out.println("true");
}else{
System.out.println("false");
} -
import java.util.Scanner;
public class Main(){
public static void main(String[] args){
int a = 4;
int b = 2;
if((a%b)==0){
System.out.println("true");
}else{
System.out.println("false");
}
}
} -
Keep it simple, the answer is right there.....
boolean x;
if(a%b ==0){
x = true;
}
else{
x=false;
}
return x;
} -
In my solution, I used only =, not == , and I got a correct response. Can you tell me why that is? Thanks.
-
inverse way also works 'a==1 && b ==1'
-
'a==1 && b ==1' can work too
-
@Carlos, with an overall not
!
, yes. -
What is the code to solve this?
-
boolean bLarger = ( a==1 && b==1);
return (!bLarger);
this works -
can be done in 1 line of code using sums
'return ((a + b) != 2);' -
boolean gates = a!=1 || b!=1;
-
-
return a!=1 || b!=1;
-
also this solution is true :
return !(a==1 && b==1); -
!((a == 1) && (b == 1))
also works! -
This might be a slight cheat, but we know that a and b are both integers, meaning there are no fractions in play. Therefore, the following also works: my code
-
import java.util.Scanner;
public class BooleanExm2
{
public static void main(String[] args)
{
int a;
int b;
Scanner input = new Scanner(System.in);System.out.println ("Enter 1 or 0: "); a = input.nextInt(); System.out.println ("Enter 1 or 0: "); b = input.nextInt(); boolean output = a==1 && b==0|| a==0 && b==1; System.out.println (output); }
}
-
Lol, my too-many-steps solution:
boolean inputa = a > b || a < b;
boolean inputb = a == 0 && b == 0;
boolean both = inputa || inputb;
return both; -
import java.util.Scanner;
public class Main(){public static void main(String[] args){
int a= 4;
int b= 1;boolean collect=(a==1 && b==1); boolean recollect=(a==1||b==1); System.out.println("recollect" + recollect); System.out.println("collect" + collect); }
}
-
thanks guys!
-
public class Main {
public static void doStuff(String name){
name = name + "!";
String hello = "Hello ";
String message = hello + name;
System.out.printIn(message);
}How come each input doesn't print out on their own line?
-
@Ian, Sorry, the correct code is with a lowercase 'L', not an uppercase I:
System.out.println
. It stands for Print Line. -
Thanks, this same issue had me stumbled for both of the Array Loop Practices too.
-
You can complete the task with only one line: System.out.printIn([Some Stuff]);
-
I agree with Cliff, that is how I did it:
Hint:
string + variable + char -
nice article
-
It's just as simple as forgetting the exclamation point to be incorrect.
-
I got all the results correct, and it says "Incorrect". Why ?
-
public static void doStuff(String name){
name = "Jim!";
String hi1 = "Hello ";
String hey1 = hi1 + name;
System.out.println(hey1);
name = "Sarah!";String hi2 = "Hello "; String hey2 = hi2 + name; System.out.println(hey2); name = "Tom!"; String hi3 = "Hello "; String hey3 = hi3 + name; System.out.println(hey3); name = "Harry!"; String hi4 = "Hello "; String hey4 = hi4 + name; System.out.println(hey4); }
Here is how I did it but still says Incorrect even though the Results are all Correct.
-
I don't quite understand this, can someone help me by explaining why
when I do
name = "Jim";
System.out.println("Hello " + name + '!');it prints four
Hello Jim
Hello Jim
Hello Jim
Hello JimI don't understand.
-
@AdhurimEsati, you are given the the String
name
each time as a parameter, you just need to print that. -
another simple solution :
System.out.println ("Hello "+name+"!"); -
import java.util.Scanner;
public class Main {
public static void main (String[] args){Scanner input= new Scanner (System.in); System.out.print ("Enter any Name: "); String name=input.nextLine(); System.out.println ("Hello " + name + "!"); }
}
-
import java.util.Scanner;
public class Main(){public static void main(String[] args){
String name="Nisha";
System.out.print("Hello" + name);
}
} -
How many possible solutions are there to this challenge?
-
There are many possible ways, but one way is particularly straightforward.
-
This code fragment also works:
for(int x = 1; x <= b; x++)
-
yo keesh can anyone post the answer...
-
import java.util.Scanner;
public class PrintLoop
{
public static void main(String[] args)
{
int a;
int b;
Scanner input = new Scanner(System.in);System.out.println ("Enter any number: "); a = input.nextInt(); System.out.println ("Enter any number: "); b = input.nextInt(); for (int i=1; i<=b; i++){ System.out.print (a); } }
}
-
for(int c = b; c > 0; c = c - 1)
-
import java.util.Scanner;
public class Loop1{
public static void main(String args[]){
Scanner input1 = new Scanner(System.in);
System.out.println("Enter First number !");String val1 = input1.nextLine(); int a = Integer.parseInt(val1); System.out.println("Enter Second number !"); String val2 = input1.nextLine(); int b = Integer.parseInt(val2); for(int i=1; i<=b; i++){ System.out.print(a); } }
}
-
import java.util.Scanner;
public class Main(){
public static void main(String[] args){
int a=2;
int b=4;
for(int i=1;i<=b;i=i+1){
System.out.print(a);
}
}
} -
BUG!
-
I got the answer in the end, but my first attempt was:
int CC = ar[ar.length-1];
int VV = ar[0];
return CC + VV; -
int[] fl = new int[2];
fl[0] = ar[0];
fl[1] = ar[ar.length - 1];
return fl; -
Thanks for the site I am really learning from it.
One thing I'd like to be able to do is practice typing things out as I'm following along with your explanations. I think it would help reinforcethe information. Could you add blank text entry fields (not compilers) here and there in the lesson to give the user scratch space?
-
@Robin, if you just want to take notes I think it would make sense to do in your own note-taking program, though we can consider adding such a feature. Note that if you click on "split-screen
view", you can view the code editor and content side-by-side, which may work for your purpose. And if you decide you want to run different code snippets, you can try the visual debugger (launched yesterday).
-
Just a funny quote here... On the challenge Second-to-Last i scratched my head for two days trying to think how to declare the variable in a single sentence the reason was because i was understanding: from the second (meaning starting at index 1) to the last, rather than actually thinking about penultimate! Frustrated not being able to declare the variable i decided that i was not understanding the exercise, clear enough google explained me second-to-last=penultimate... OMG LOL
Pura vida.
-David
-
What does "Second to last" mean?
-
HOLY MOLY. 2D ARRAYS MADE MY SUDOKU SOLVER EASIER!
-
And here is the answer for the challenge:
int [] ars = {ar [0], ar [ar.length -1]};
return ars; -
these are very good
-
If you get a number from an array, and do not know what slot its in. Is there a code to retrieve the slot?
Example;
I found the largest number in the array and want to know what slot that is in. -
I don't understand why my challenges are wrong:
1. int n = numbers[6];
2. boolean [] frank= new boolean [6]; -
I had the write awnser but it still does not work, what is wrong in this:
int[] ar2 = new int[2];
ar2[0] = ar[0];
ar2[1] = ar[ar.lenght - 1];
return ar2; -
sorry i solved it lenght mus be length pfffffffffffff :-)
-
Is the free memebership limit forever? Doesn't it restore after certain time?
-
It doesn't currently restore since it's considered a free trial.
-
Or declare regular variables:
int a = ar[0];
int b = ar[ar.length -1];
int[] az = {a,b};return az;
-
Second-to-last means, the second element in the array starting counting from the last index.
-
import java.util.Scanner;
public class Main(){
public static void main(String[] args){
int n;
int[] numbers= new int[8];
int[] numbers={2,3,4,5,6,7,8,9};
for(int i=0;i<8;i=i+1){
System.out.print(numbers[i]);
if(i==6){
n=numbers[i]
System.out.print(n);
}
}
}
} -
Do it easy guys ...
int[] res = {ar[0], ar[ar.length -1]};
return res; -
Hint: System.out.print(...); prints the numbers on the same line.
-
Hint 2: remember to print the empty char for both if and else :)
-
why am i getting errors in the boilerplate code :\
-
forgot the curly brackets haha :P
-
if(numbers[i] > largest){
largest = numbers[i];
}Shouldn't be numbers[i] < largest ?
-
How to print on one line everything, I am having issues with that.
-
Ehh, just use System.out.print(); instead of System.out.println(""); You didn't mention it earlier :(
-
//What´s wrong with my Code??
//PLZ help I don´t get any further...
//blessed
import java.util.*;
class Main{
static void doStuff(int[] ar){
for(int i=0; i<=ar.length;i++){if (i!=3){ String[] ar2 = new String[i]; System.out.print(ar2[i]); }else{ System.out.print(ar2["no3"]); } } //your code here } //boilerplate code public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int c=0;c<t;c++){ int n = in.nextInt(); int[] ar = new int[n]; for(int i=0;i<n;i++){ ar[i]=in.nextInt(); } doStuff(ar); System.out.println(""); } }
}
-
@Andreas, we will be running a membership option for people to get help with their errors. Meanwhile, here's some tips: You don't need to create a new array, just print the items in the given array. To print "no3", just type
System.out.print("no3");
-
How should I do?
when i run this code, the compiler return the error message:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1at Main.doStuff(Main.java:8) at Main.main(Main.java:27)
How should i do? please help me.
import java.util.*;
public class Main{
static void doStuff(int[] ar){
//your code here
for(int j = 0; j <= ar.length; j++){
if(j % 3 != 0){
String[] ar2 = new String[j];
System.out.print(ar2[j]);
}
else{
System.out.print("no3");
}
}
}//boilerplate code public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int c=0;c<t;c++){ int n = in.nextInt(); int[] ar = new int[n]; for(int i=0;i<n;i++){ ar[i]=in.nextInt(); } doStuff(ar); System.out.println(""); } }
}
-
Someone please post the answer so I can see what I am doing wrong. :/
-
@jordan, I would suggest starting by printing the array, and then move on to modifying it for multiples of 3. The code at the top of the page shows how to print an array, though you'll want
print
instead ofprintln
. -
package Testpac;
import java.util.Scanner;
public class Maon_Test
{public static void main(String[] args) { Scanner sc=new Scanner(System.in);
int t=sc.nextInt(); for(int c=0;c<t;c++) { int n=sc.nextInt(); int []ar=new int [n]; for(int i=0;i<n;i++) { ar[i]=sc.nextInt(); } doStuff(ar); System.out.println(""); } } private static void doStuff(int[] ar) { // TODO Auto-generated method stub for(int i=0;i<ar.length;i++) { if(ar[i]%3==0) { String no3; no3=String.valueOf(ar[i]); no3="no3"; System.out.print(no3); } else System.out.print(ar[i]); } }
}
-
CORRECT:
for(int i = 0; i < ar.length; i = i+1)
{if (ar[i]%3==0){ System.out.print("no3"); } else{ System.out.print(" "+ar[i]); }
}
-
Does anyone have any idea what boilerplate code means?
-
@Prashant, in these challenges, it's just standard code that is included to process the input for you. I.e. it sets up the variables or arrays and passes them to the method
doStuff()
. -
another simple solution using for each :
for (int element : ar)
{
if (element%3 == 0) System.out.print("no3 ");
else System.out.print(element+" ");
} -
I'm just wondering whether or not you have a guide for using streams?
-
Sorry these tutorials are focused on the basics of Java for beginners, so we don't cover that.
-
There is probably better ways but I worked out the largest number first.
-
static void doStuff(int[] ar){
//your code here
int smallest=0;
for(int i=0;i<ar.length;i++){
if(ar[i]<smallest){
smallest=ar[i];
}
}
return System.out.print(smallest); -
Hello! Don't understand the error...any help would be apreciated!
-
Can you help me understand what wrong I have done:
static void doStuff(int[] ar){
//your code here
int smallest=ar[0];
for(int i=0;i<ar.length;i++){
if(ar[i] < smallest){
smallest =ar[i];
}} System.out.print(smallest); }
-
@mohammad, it looks like your print statement is outside the method, which doesn't work in Java. move it up to before the
}
so it runs when the for-loop is finsihed. -
I did as you said... the code work in eclipse but not in above editor...
-
@mohammad, it looks like there are some other errors in your code also. See if you can modify the code from the previous page to find the smallest number instead of the largest.
-
i cant understand what its wrong here, i plug the code of the last page and modify and the output still doesnt make any sense.
'static void doStuff(int[] ar){int min = ar[0]; for(int i=0; i < ar.length; i = i+1){ if(ar[i] < min){ min = ar[i]; } }
System.out.print(min);
}' -
i found the answer. There is a mistake in the boilerplate code, under 'doStuff(ar);' you have to put the line 'System.out.println("");'. Then the code should work.
//boilerplate code
public static void main(String[] args) {
Scanner in = new Scanner(System.in);int t = in.nextInt(); for(int c=0;c<t;c++){ int n = in.nextInt(); int[] ar = new int[n]; for(int i=0;i<n;i++){ ar[i]=in.nextInt(); } doStuff(ar); System.out.println(""); //this line should be added to the code } }
-
@Peter, thanks. I mentioned in the question to print each number on its own line, so that if you use
System.out.println
(with theln
for line) it works, and you don't need to edit the boilerplatecode. However since this may be confusing, I updated the boilerplate code so both
print
andprintln
now work. -
int smallest = ar[0];
for(int i=0; i < ar.length; i = i +1){if(ar[i] < smallest){ smallest = ar[i]; } } System.out.println(smallest);
-
Small suggestion (however this may only be me), after reading the instructions it says "(Print each number on its own line.)", i think this is misleading or requires further clarification.
Because first it is said to print the smallest number but then print each number...
-
int valor = ar[0];
for(int i = 0; i<= ar.length - 1; i = i + 1){
if (valor <= ar[i]){
}
else {
valor = ar[i];
}
}System.out.print(valor);
//ar.length-1 because for example i have an string with 10 positions, but it always starts from 0 to 9 so them the value of the tenth it´s on the position 9. xD
// if (valor <= ar[i]) if the value stored in the variable valor is smaller than the value in the string you do nothing.
// else valor = ar[i]; it will store the new value only if it is bigger.
// System.out.print(valor); it will print only after it has finished all the for loop, out of the "{ }" of the loop -
Hello, I keep getting the correct outputs, but with .0 on the end. What am I doing wrong?
import java.util.*;
class Main{
static void doStuff(int[] ar){double minimum = ar[0];
for (int i = 0; i < ar.length; i++)
{
if (ar[i] < ar[0])
{
minimum = ar[i];}
}
System.out.println( minimum );
}//boilerplate code public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int c=0;c<t;c++){ int n = in.nextInt(); int[] ar = new int[n]; for(int i=0;i<n;i++){ ar[i]=in.nextInt(); } doStuff(ar); System.out.println(""); } }
}
-
@TyB, use ints instead of doubles.
-
Thank you!
-
private static void doStuff(int[] ar) { Arrays.sort(ar); System.out.println(ar[0]); }
-
' static void doStuff(int[] ar){
int small=ar[0];
for (int i=0; iar[i]){
small=ar[i];
}
}
System.out.print(small);
}' -
I used this strategy: static void doStuff(int[] ar){
int smallest = 0;
int teller = 0;
for(int i = 0; i < ar.length; i = i+1){
if(teller == 0){smallest = ar[i]; teller = 1; } else if(ar[i] < smallest){ smallest = ar[i]; } } System.out.print(smallest); }
-
im getting errors like
Main.java:12: error: ';' expected
public static void main(String[] args) { -
what does this mean?
cannot return a value from method whose result type is void
return smallest; -
another solution using for each:
int tmp = ar[0];
for (int element : ar)
{
if (element < tmp ) tmp = element;
}System.out.println (tmp);
-
ugghh had to cheat on this one ....will do better next time
-
thisis good!
-
happy to finish and continue
-
Am so glad i found this site.. good work. thanks a lot.
-
One of the best interactive tutorials i have seen until now.Good job!
-
finished
-
This was truly a great intro to Java programming. The challenges were engaging and rewarding, and the material conceptually explained in a way that a beginner like myself could grasp. It's a step forward for learning.
-
Yay! Basics are cleared now. This was VERY basic.
-
Pretty neat! I was looking for something my son could try and this seems like a good possibility for him to try. Thanks!
-
Well that was fast
-
Pretty neat site. Love it!
-
I like this quick reference!
-
REally Cool!
-
nice one
-
thanks :)
-
thanks :)
-
what next?
-
@bhavana, please see the previous node, whats next
-
thanks :)
-
nice one
-
nice
Quantum
Dec 7, 9:09 PMI already love it!