Results 1 to 8 of 8
Thread: Help with Program . . .
- 09-18-2008, 02:52 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 3
- Rep Power
- 0
Help with Program . . .
First Off . . . Im not looking for anyone to write the code for me. That being said I need some pointers on what I'm doing wrong with my program.
Program Description: Should have a "while" loop that asks repeatedly for a number until a number divisible by 3 or 7 is given. When that occurs, your program should display the number of numbers read.
Here is what I have thus far :
There is something misplaced or left out. . . Im new to programming and haven't figured out to get what's in my head translated to the computer.import java.util.Scanner;
public class DivisibleBy3or7
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("This program reads numbers until one is divisible by 3 or 7");
System.out.println("Write a Number: ");
int Number = input.nextInt();
int i = 1;
int sum = 0;
while (( Number % 3 != 0 ) && ( Number % 7 != 0 ))
{
sum = sum + 1
System.out.println("Write a Number; ");
i++;
}
System.out.println("There were " + sum + " numbers read ");
}
}
Thanks,
Dan
- 09-18-2008, 03:01 AM #2
What does your program do? Does it work? Does it have errors?
I don't see any asking being done in the while loop."while" loop that asks repeatedly
- 09-18-2008, 03:08 AM #3
Member
- Join Date
- Sep 2008
- Posts
- 3
- Rep Power
- 0
I'm sorry. . It doesn't work. . .it just prints the first two lines. . then when I enter a number it does a continuous print of "Write a Number".
Im sure my error is with my while loop. Im not to sure how to get it to ask "Write a Number" one time at a time.
- 09-18-2008, 03:11 AM #4
Your not asking inside the loop. How will Number ever change?I don't see any asking being done in the while loop.
- 09-18-2008, 04:35 AM #5
Java Code:import java.util.Scanner; public class Divisibleby3or7 { int number=0; public Divisibleby3or7(){ Scanner input = new Scanner(System.in); int sum = 0; do { sum = sum + 1; System.out.println("This program reads numbers until one is divisible by 3 or 7"); System.out.println("Write a Number: "); number = input.nextInt(); }while(( number % 3 != 0 )||( number % 7 != 0 )); System.out.println("There were " + sum + " numbers read "); } public static void main(String[] args) { new Divisibleby3or7(); } }
- 09-18-2008, 06:17 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You are much clever pal. Really nice to see your attempt here that make to learn Java. Keep going.
Regrading your code, first thing is you misses a semicolon at the end of following line, within the while loop.
Now look at the code from the beginning. Using a scanner object you collect an int number, say 4. Then you entered to the while loop and do the validation. According to your validation while loop return true. So you can entered to the while loop. There you have three lines.Java Code:sum = sum + 1
Second line of the while loop, increment the value of sum by 1. Third line just display a line of text to the console. On line four you do another increment as well.Java Code:[LIST=1][*]while (( Number % 3 != 0 ) && ( Number % 7 != 0 )) {[*]sum = sum + 1;[*]System.out.println("Write a Number; ");[*]i++;[*]}[/LIST]
So is there any process taken to exit from the while loop? NO, so you have endless loop. Until you exit the application, it prints a line of text to the console.
What you have to do is, use a do-while loop. In do-while loop before the condition is check it guaranteed that body of the loop execute at least once.
But the following line is never executed.
Think about that, what exactly happen.Java Code:}while(( number % 3 != 0 )||( number % 7 != 0 ));
- 09-18-2008, 06:50 AM #7
oh shit i made a little mistake with condition in while,it was too late at night,that i wrote the code,lol,but i think he understood the idea with do-while loop
- 09-18-2008, 07:47 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
Help with program
By bri1547 in forum New To JavaReplies: 16Last Post: 07-27-2008, 05:26 AM -
Executing a program within a program
By gibsonrocker800 in forum New To JavaReplies: 5Last Post: 05-12-2008, 08:24 AM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
cannot run the program
By amiey in forum New To JavaReplies: 1Last Post: 11-20-2007, 04:13 AM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks