Results 1 to 5 of 5
- 03-02-2011, 05:46 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
Boolean always true?? What am I doing wrong?
First let me say that I've found a way to achieve my goal not using a boolean anyway, but I still can't let go of this problem until I figure it out and it feels cheap if I just forget about it.
This program determines if a number is a prime number. After a loop which tests if the given number is divisible by 1 to 1 less than the given number (and is not 1 itself, and is not being divided by 1), it should switch the boolean value to false and break the loop. Otherwise, it keeps going until the loop breaks itself (the counter matches the number being tested) and the boolean value stays true.
This sounds great and logical to me, but the boolean is ALWAYS staying true! The program is saying every number you enter is a prime number! What am I doing wrong?
Code:Java Code://declare and construct variables int iNumber = 0, iPrimeCheck = 0; String sOutput = "", sNumber = ""; boolean bIsPrime = true; sNumber = JOptionPane.showInputDialog("Enter a number: "); iNumber = Integer.parseInt(sNumber); for (iPrimeCheck = 1; iPrimeCheck < iNumber; iPrimeCheck++) { if (iNumber % iPrimeCheck == 0 && (iNumber != 1 && iPrimeCheck != 1)) { bIsPrime = false; break; } } if (bIsPrime == true) sOutput = "Prime!"; else sOutput = "Not Prime!"; //display message to user with sOutput JOptionPane.showMessageDialog(null, sOutput, "Prime Numbers", JOptionPane.INFORMATION_MESSAGE); System.exit(0); //exit
- 03-02-2011, 06:02 PM #2
This seems to work okay for me. It just told me that 8 and 10 are not prime. What values are you using to generate incorrect output?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-02-2011, 06:04 PM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Try starting the loop at 2 and only test
The code however looks fine, although it can be made to look a bit better.Java Code:if(number % i == 0)
Last edited by sunde887; 03-02-2011 at 06:07 PM.
- 03-02-2011, 06:05 PM #4
It also seemed to be working for me.
- 03-02-2011, 06:13 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
What the . .
This thing is just frying my brain. For some reason I thought 3 was not a prime number and assumed I was getting bad results. Wow.
These little Math assignments are the hardest part of learning JAVA. You have to go back to stuff you haven't learned about in 10 years. Its like building a deck on mud.
Sorry!
Similar Threads
-
actionListener & setSelected(true)
By new2java2009 in forum New To JavaReplies: 11Last Post: 01-13-2012, 01:46 PM -
while(true)
By ravian in forum New To JavaReplies: 7Last Post: 06-29-2011, 06:05 AM -
classes, true purpose of
By mac in forum New To JavaReplies: 1Last Post: 12-22-2009, 04:34 PM -
true && !true
By SirFalcon in forum New To JavaReplies: 3Last Post: 10-29-2009, 02:33 AM -
which one of the following is true about interface?
By makpandian in forum New To JavaReplies: 3Last Post: 06-30-2009, 12:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks