Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-17-2008, 08:19 AM
Member
 
Join Date: Oct 2008
Posts: 22
Rep Power: 0
mrdestroy is on a distinguished road
Unhappy Need help with While Loop
I got a problem with this one, hope you guys can help me. I've just learned Java few weeks.

Write a program segment that reads a natural number n (n>=1) from keyboard, then
calculates and displays the sum of all odd numbers from 1 to n. Use the while loop to
validate input, if the number is smaller than 1, display an error message and the user
can try again.

Sample run:
Please enter a natural number greater than 1: -1
Invalid value. Please enter a natural number greater than 1: 9
Sum of odd from 1 to 9 is: 25

Thanks in advanced.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 10-17-2008, 08:46 AM
Member
 
Join Date: Oct 2008
Posts: 44
Rep Power: 0
Unome is on a distinguished road
Default
Whats your problem? You don't know how to make a while loop?
while (decisioin)
{
//data and calculations
}

to calculate the sum of the odd numbers test a /2 and see if you get a whole number or a number with decimals. if it has a remainder than its an odd number!

hope that helps. clarify what part of the problem your having difficulty with
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-17-2008, 09:21 AM
Member
 
Join Date: Oct 2008
Posts: 22
Rep Power: 0
mrdestroy is on a distinguished road
Default
import java.util.Scanner;


public class OddNumberSum {

/**
* @param args
*/
public static void main(String[] args) {
int n = 1;

Scanner keyboard = new Scanner(System.in);


while (n >= 1){
System.out.println("Please enter a natural number greater than 1: ");
n = keyboard.nextInt();


}
{
System.out.println("Invalid value, Please enter a natural number greater than 1: ");
n = keyboard.nextInt();
//This is for enter again.
}



}
}

How can I calculate the sum of odd numbers.
Example:
If I enter 9, it means I have to calculate the sum of 1, 3, 5, 7, 9.
If I enter 8, I have to calculate the sum of 1, 3, 5, 7.

Last edited by mrdestroy; 10-17-2008 at 09:27 AM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-17-2008, 09:39 AM
Senior Member
 
Join Date: Aug 2008
Posts: 372
Rep Power: 2
Supamagier is on a distinguished road
Default
dude wtf?
Code:
}
{
System.out.println("Invalid value, Please enter a natural number greater than 1: ");
n = keyboard.nextInt();
//This is for enter again. 
}
__________________
I die a little on the inside...
Every time I get shot.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-17-2008, 12:43 PM
Member
 
Join Date: Oct 2008
Posts: 22
Rep Power: 0
mrdestroy is on a distinguished road
Default
If users enter a wrong number (<1), they have to enter again.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 10-18-2008, 11:54 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,504
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Use a do-while loop to fix it.

Code:
        int val;
        
        do {
            System.out.println("Enter the value: ");
            val = keyboard.nextInt();
        }while(val < 1);
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 10-18-2008, 02:14 PM
Member
 
Join Date: Oct 2008
Posts: 22
Rep Power: 0
mrdestroy is on a distinguished road
Default
Is there any ideas for while loop, pls???
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 10-18-2008, 04:18 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,442
Rep Power: 8
Fubarable is on a distinguished road
Default
what do you mean "any ideas for while loop?" You've been given some great ideas above. Let's see the latest of what you have in code, and then a decent question about that code.

Also, when posting your code, please use code tags so that your code will retain its formatting and be readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. Another way is to place the tag [code] at the top of your block of code and the tag [/code] at the bottom, like so:

Code:
[code]
  // your code block goes here.
  // note the differences between the tag at the top vs the bottom.
  // note that this code has to already be properly formatted with 3-4 space indentations for code tags to work
[/code]

good luck.

Last edited by Fubarable; 10-18-2008 at 04:53 PM.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 10-18-2008, 05:53 PM
Member
 
Join Date: Oct 2008
Posts: 22
Rep Power: 0
mrdestroy is on a distinguished road
Default
public static void main(String[] args) {
int n = 1, sum = 0;

Scanner keyboard = new Scanner(System.in);


while (n >= 1){
System.out.println("Please enter a natural number greater than 1: ");
n = keyboard.nextInt();
for(int i = 1; i <= n; i += 2)
{
sum += i;
}
System.out.println("Sum of odd numbers from 1 to " + n + " is: " + sum);
}
{
System.out.println("Invalid value, Please enter a natural number greater than 1: ");
n = keyboard.nextInt();
}

}
}
This is my code in while loop, but when I run this printed out "Please enter a natural number greater than 1:" again.
How can I stop this repeat?
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 10-18-2008, 06:00 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
Please see above about posting code with code tags.
At least label the end of loops with comments:
} // end while

} // end for(i)

Unformatted code is hard to read.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 10-18-2008, 11:10 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,504
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
I've no idea that why are are stick with the while loop. Do while is solution you have.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 10-19-2008, 06:16 AM
Member
 
Join Date: Oct 2008
Posts: 22
Rep Power: 0
mrdestroy is on a distinguished road
Default
Thank you all very much. But, while is a requirement in this problem, I have to do it following while. Do while from Mod is really great. I'm just new here so I didnt know about code tag. I will fix it in others.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 10-20-2008, 11:38 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,504
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
All depends on your analytical skills.

Code:
    public static void main(String[] args) {

        int n = 1, sum = 0;
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Please enter a natural number greater than 1: ");
        n = keyboard.nextInt();
        
        while (n <= 1){
            System.out.println("Invalid number, Please enter a natural " +
                    "number greater than 1: ");
            n = keyboard.nextInt();
        }

        for(int i = 1; i <= n; i += 2) {
            sum += i;
        }
        System.out.println("Sum of odd numbers from 1 to " + n + " is: " + sum);
    }
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 10-20-2008, 03:28 PM
Member
 
Join Date: Oct 2008
Posts: 22
Rep Power: 0
mrdestroy is on a distinguished road
Default
That's perfect. Thank you very much.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 10-20-2008, 03:29 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,504
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
You are welcome. Is that what you are looking?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
For Loop kian_hong2000 New To Java 1 08-07-2008 03:01 PM
while loop Unknown1369 New To Java 5 07-08-2008 11:15 AM
How to use While loop Java Tip java.lang 0 04-17-2008 08:44 PM
while loop michcio New To Java 5 01-27-2008 01:56 AM
can you help me with this for loop? java_fun2007 New To Java 6 12-22-2007 11:20 AM


All times are GMT +2. The time now is 11:01 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org