Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes I want new Java question to Improve my skills Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I want new Java question to Improve my skills" Watch "I want new Java question to Improve my skills" New topic
Author

I want new Java question to Improve my skills

Lakshitha Nuwan
Greenhorn

Joined: Nov 01, 2012
Posts: 8
I am a beginner. so, i need some questions to improve my skills in for-loop and if else.. please help me..
Len Padson
Ranch Hand

Joined: Nov 21, 2011
Posts: 40
Welcome to the Ranch!!! A quick google search came up with this:

http://mathbits.com/MathBits/Java/Looping/MCLooping.htm

But if you are just looking for some challenges, here are a few i can think of:


1) To test both ifs and fors, write a program that counts how many of the first thousand numbers (1-1000) are even. The answer is obvious, but the program might be a tad more difficult.

2) Write a program to print one one, then two twos, then three threes, then four fours, etc.

3) For a more difficult problem, that will involve multiple loops and ifs, repeat number one, only determine how many of the first thousand numbers are divisible by two, three, four, five, ... all theway to a thousand, so that the output ends up looking like this:



Hope this answers your question, and if you have any more questions just reply!
Lakshitha Nuwan
Greenhorn

Joined: Nov 01, 2012
Posts: 8
Answers really challenged to me... but I try to get output as your thought..
Thank you for answers and looking for more challenges.
Lakshitha Nuwan
Greenhorn

Joined: Nov 01, 2012
Posts: 8
Is this the program for challenge 3?

class Example{
public static void main(String args[]){
int x=1;
int y;


for(int i=0; i<1000; i++){
y=1000/x;
System.out.println(x+" : "+y);
x++;

}

}
}
Len Padson
Ranch Hand

Joined: Nov 21, 2011
Posts: 40
That is a god start, but there are two things that I would like to point out. First of all, your for loop includes an incrementer, as it should (that's the i++). What this means is that every time the program runs through this loop, it adds one to i. Therefore, you should not have an additional i++ later in your code. Otherwise, every time the loop iterates (or runs through a cycle), i will be incremented twice, meaning you will skip over half the numbers.

Second, your solution should work, but it is not exactly how I intended for it to be done. You observed that the number of integers less than 1000 that are divisible by a given number is 1000 divided by that number. Had I not included a sample output, however, you would not have been able to make this observation. This may be a little advanced, as I am not sure how in depth you are at this point in Java, but a very operation is the modulo operator: %.

Modulo basically means remainder. For example, 5%2=1, because when 5 is divided by two, the remainder is one. Here are some more samples... hopefully you can catch on, and if not, just google modulo. The easiest way, then, to determine if one number is divisible by another, is to check if one modulo the other is equal to zero, meaning that the second goes into the first a whole number of times. For example:

10%2=0, so 10 is divisible by 2.
190%19=0, so 190 is divisible by 19.
39%3=0, so 39 is divisible by 3.

but,

35%8=3, and since 3 is not zero, 35 is not divisible by 8.


Now, I hope I haven't veered too off topic with this discussion, but chances are if you are learning loops now, you will be learning basic operations, +-/*%, soon. If you need anything else, please don't hesitate to ask!
Lakshitha Nuwan
Greenhorn

Joined: Nov 01, 2012
Posts: 8
if you leave more hints for these challenges, it will help me to understand what will you observe from it. (I can't get clear idea)
I followed about operators include remainder, Now I can do little things from it.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18875
    
  40

Lakshitha Nuwan wrote:I am a beginner. so, i need some questions to improve my skills in for-loop and if else.. please help me..


In my opinion, the best way to improve is to actually do a project. And generally, a complex project is better. This is why I always recommend doing something that you like. When you like something, you are more interested in it, and you have more incentive in succeeding.

For example, if you like puzzles, then how about writing a puzzle solver? Find something that you like, then find a project with it. Could you choose something that is too difficult? Sure, you can, but if it is for something that you like, you'll enjoy working on it, and hence, learn something from the attempt.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Lakshitha Nuwan
Greenhorn

Joined: Nov 01, 2012
Posts: 8
Thank you very much..
I try to make star patterns from for loop.

like this

*
**
***
****
*****
*****

*****
****
***
**
*

I can do these only from 1 star and 2 loops..
also try to do 1 star and 1 loop.

Thank you again..
Lakshitha Nuwan
Greenhorn

Joined: Nov 01, 2012
Posts: 8
I Think I got it.. In this program, had I broke any standard?

Aj Prieto
Ranch Hand

Joined: Sep 28, 2012
Posts: 72

Though your code works, there is a much simpler and cleaner solution that utilizes a nested "for" loop, which means a loop within a loop.

Also instead of having to declare a variable "y" and increment it each time it goes through a loop, you could just use the variable that was declared in the loop. See below using your code.



Da mihi sis bubulae frustum assae, solana tuberosa in modo Gallico fricta ac quassum lactatum coagulatum crassum.
Lakshitha Nuwan
Greenhorn

Joined: Nov 01, 2012
Posts: 8
got it.. Tank you..

I want more patterns.
Aj Prieto
Ranch Hand

Joined: Sep 28, 2012
Posts: 72

Lakshitha Nuwan wrote:got it.. Tank you..

I want more patterns.


When I changed the code in my previous post, I didn't check to see if it worked properly, so it may not work the way it was intended to. I just used it as an example for using the variable declared in the loop.

Perhaps you could try and make an "X" pattern.

Something similar to this. This has a size of 7.
Lakshitha Nuwan
Greenhorn

Joined: Nov 01, 2012
Posts: 8
"X" pattern really improved my thinking side..
is this the program for "x"?

Aj Prieto
Ranch Hand

Joined: Sep 28, 2012
Posts: 72

Ok, now try making the "X" and the star patterns with nested loops.

Here is an article I found that may help.
Nested "for" loops
 
GeeCON Prague 2014
 
subject: I want new Java question to Improve my skills