The Artima Developer Community
Sponsored Link

Java Answers Forum
New to Java- Desperately need some help on basic java program

1 reply on 1 page. Most recent reply: Mar 23, 2011 4:07 AM by Sourav Datta

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
taylor m

Posts: 1
Nickname: tm02943
Registered: Mar, 2011

New to Java- Desperately need some help on basic java program Posted: Mar 21, 2011 8:58 PM
Reply to this message Reply
Advertisement
I am very new to Java and cannot figure out this problem even with research and lots of time spent trying.

The question is:

Write 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:

public static void main ( String [] a)
{
JOptionPane.showInputDialog(null, "Insert Number Here:");
int x;


}
}


Sourav Datta

Posts: 2
Nickname: soura20067
Registered: Aug, 2008

Re: New to Java- Desperately need some help on basic java program Posted: Mar 23, 2011 4:07 AM
Reply to this message Reply
If x is the integer then the following snippet should do the required work:

for (int i = x; i >= 1; i--) {
String s = i + "";
char lastDigit = s.charAt(s.length() - 1);
if (lastDigit == '5' ||
lastDigit == '3' ||
lastDigit == '7')

System.out.println(i);
}

This needs to be put inside main right after you read the value of x.

Flat View: This topic has 1 reply on 1 page
Topic: ArrayList - "Unchecked or unsafe operations" at compile Previous Topic   Next Topic Topic: JAVA OBJECTIVE QUESTIONS

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use