Results 1 to 12 of 12
  1. #1
    tm02943 is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Unhappy New to Java and connot figure out simple java program- please help

    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;


    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    The JoptionPane will return a String value. You need to assign that to a variable. The you need to parse that into an int and assign it to your x variable. Do that and test your code several times until you are satisfied that it works.

    Then move onto the next step. Use a while loop that counts x down to 1. Test this by printing out all numbers.

    Next step only print numbers that end in 3, 5 or 7. Oops I was doing multiples. You need to mod by 10 instead. 43 % 10 = 3. 1087 % 10 = 7 etc.
    Last edited by Junky; 03-22-2011 at 05:08 AM.

  3. #3
    tm02943 is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    Dear Junky-

    Step one makes sense to me and i worked on it and got that part.

    Step 2 and 3 dont make sense to me and i need further instruction.

    Sorry for being so bad at Java but please work with me.

    So far I am up to this point:


    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);



    }
    }

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Do you know what a while loop is? You use a while loop that keeps looping as long as your number is greater than or equal to 1. Inside the loop have a print statement that prints the number. If I enter 10 then the output should be:
    10
    9
    8
    7
    6
    5
    4
    3
    2
    1

  5. #5
    tm02943 is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    Ok so ive been trying to set up this while loop but am not getting far. I cannot figure out how to set it up for the life of me..... so far ive got:

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

    int i=1;

    while (i<=x)
    {
    if(i%2==0)
    {
    System.out.print(i+" ");
    }
    }
    }
    }

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Why do you have the variable i?
    It has the value of 1, so mod by 2 will never equal 0.
    Why are you printing i?
    When does the value of x and/or i change to make the loop stop?

  7. #7
    tm02943 is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    Finally got step 2,


    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)
    {
    System.out.println(" "+ counter);
    counter=counter-1;
    }
    }
    }



    Step 3- is the most difficult part, i dont even know how to start

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Why do you need the counter variable? Just use x.

    As I said in a previous post you need to mod your number by 10. When the result is 3, 5 or 7 you print the number. Otherwise print nothing. Always decrease your number.

  9. #9
    tm02943 is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    Im tried and this is what I came up with


    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=3)
    System.out.println(x);
    {
    if(x%10=5)
    System.out.println(x);
    {
    if (x%10=7)
    System.out.println(x);
    }

    }
    }
    }
    }
    }




    What am i doing wrong?

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    When you post code use code tags. Put [ code ] before and [ /code ] after without the spaces.

    "What am I doing wrong" provides zero information. Fully describe what is happening and what you expect to happen instead.

    You decrease the variable before you do your tests.

    Check your curly braces as it looks like you have too many.

    Added bonus you can achieve this with only one if statement not three.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Also you are doing an assignment and not a comparison in the if statements

  12. #12
    tm02943 is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    Whats happening is that it will not mod the numbers by ten. The way I set it up before is the only way i know how. I just tried using one if statement and didnt get anywhere. PLEASE show me what the IF statement should look like. Im not trying to get you to do my work I just have never dealt with this % mod stuff and need an example.

Similar Threads

  1. need help to figure out basic java program
    By shane123 in forum New To Java
    Replies: 21
    Last Post: 12-02-2011, 05:12 AM
  2. Simple java program, need help
    By cliffh in forum New To Java
    Replies: 1
    Last Post: 10-21-2010, 04:32 AM
  3. Simple Java program
    By Rolle in forum New To Java
    Replies: 3
    Last Post: 10-26-2009, 05:05 PM
  4. help with simple program in java
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 09:03 PM
  5. help with simple java program
    By leonard in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 10:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •