Results 1 to 3 of 3
- 03-22-2011, 06:56 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
Need help almost finished problem thats due
Ok so the program I am writing is-
a program that stores a number in an integer variable x. The program then prints all the numbers from x to 1 that ends with either 3 or 5 or 7.
So far I have-
import javax.swing.JOptionPane;
public class C2{
public static void main ( String [] a)
{
String T = JOptionPane.showInputDialog(null, "Insert Number Here:");
int x = Integer.parseInt(T);
int counter= x;
while (counter>=1)
{
counter=counter-1;
{
if(x%10)
System.out.println(x);
}
}
}
}
The If statement is wrong, I know that mod 10 is how you get the last digit of a number but i cannot use it in my program correctly.
Please help
- 03-22-2011, 08:09 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
"if" evaluates boolean values (true/false). What does the "%" return? Is this a boolean? If not, what do you need to do with that result to get a boolean result that makes sense?
- 03-22-2011, 08:10 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 7
- Rep Power
- 0
Don't parse it to an integer :D just check if the last Character equals "3","5" or "7" (notice that 3,5,7 are Strings)
you can get the last character of a String with YourString.charAt(YourString.length()-1)
I know there are better solutions but it'll do for this job.Last edited by nathic; 03-22-2011 at 08:14 AM.
Similar Threads
-
How do I know that byte of stream is finished?
By mani_minhaj in forum New To JavaReplies: 2Last Post: 02-26-2010, 12:08 PM -
Finished Product: What now?
By Unome in forum Java AppletsReplies: 5Last Post: 02-11-2009, 10:41 AM -
How to construct my finished program?
By matpj in forum New To JavaReplies: 0Last Post: 01-14-2009, 05:37 PM -
finished paint!
By diggitydoggz in forum New To JavaReplies: 3Last Post: 01-04-2009, 10:33 AM -
How to run a code when a download is finished
By aneesahamedaa in forum New To JavaReplies: 4Last Post: 10-14-2008, 12:37 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks