Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Homework - using 'IF' for 'For Loops'

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Homework - using 'IF' for 'For Loops'

    Hey everyone. Slightly a beginner here but I'm doing Computer Science in high school. I use JCreator to write my codes and I just need a little help with the logic I need to use and some functions I would need to use.

    The task:

    First we made a code on inputting numbers using InputStreamReader and what the program had to do was create a multiplication table of numbers.

    First it had to ask for which number you want to multiply by -- let's say 5

    Then, it asked from which value you want to start multiplying -- let's say 2

    Finally, it asked for the last number in which the program would stop multiplying -- let's say 10

    basically it printed out

    2*5=10
    3*5=15
    4*5=20
    .
    .
    .
    10*5=50

    then it stopped.

    What I need to do now is to create an if function which asks that once that is completed, if the user wants to continue running that program. The program will keep running unless the user gives a command for it to stop.

    Also, my teacher recommended me to use methods and functions instead of doing everything in the public static void main.

    here's the code

    import java.io.*;
     
    public class ForLoop {
     
     
        public static void main(String[] args) throws IOException {
     
     
        InputStreamReader in=new InputStreamReader(System.in);
        BufferedReader strbuf=new BufferedReader(in);
     
        System.out.println("Enter table for: ");
        int number=Integer.parseInt(strbuf.readLine());
        System.out.println("The number you have entered is: "+number);
     
        System.out.println("Enter first value: ");
        int first=Integer.parseInt(strbuf.readLine());
        System.out.println("The first value you have entered is: "+first);
     
     
        System.out.println("Enter last value: ");
        int last=Integer.parseInt(strbuf.readLine());
        System.out.println("The last value you have entered is: "+last);
     
        for(int initial=first;initial<=last;initial++)
        {
        System.out.println(initial + "X" + number + "=" + (initial * number));
        }
     
        }
    }

    any ideas?

    Thanks!!
    Last edited by Enzo; September 16th, 2009 at 11:11 AM.


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Homework - using 'IF' for 'For Loops'

    Firstly, welcome to Java Programming Forums. I hope we can help you out!

    import java.io.*;
     
    public class ForLoop {
     
     
        public static void main(String[] args) throws IOException {
     
        do{
           InputStreamReader in=new InputStreamReader(System.in);
           BufferedReader strbuf=new BufferedReader(in);
     
           System.out.println("Enter table for: ");
           int number=Integer.parseInt(strbuf.readLine());
           System.out.println("The number you have entered is: "+number);
     
           System.out.println("Enter first value: ");
           int first=Integer.parseInt(strbuf.readLine());
           System.out.println("The first value you have entered is: "+first);
     
     
           System.out.println("Enter last value: ");
           int last=Integer.parseInt(strbuf.readLine());
           System.out.println("The last value you have entered is: "+last);
     
           for(int initial=first;initial<=last;initial++)
           {
              System.out.println(initial + "X" + number + "=" + (initial * number));
           }
        }while(Cont());
        }
     
       public boolean Cont()  throws IOException {
          System.out.print("Would you like to carry on (y/n): ");
          BufferedReader strbuf=new BufferedReader(new InputStreamReader(System.in));
     
          if(strbuf.readLine().equalsIgnoreCase("y")) return true;
          return false;
       }
    }

    Regards,
    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  3. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework - using 'IF' for 'For Loops'

    hey Chris, thanks so much for the help!

    however, it keeps giving me this error

    'non static method Cont cannot be referenced from a static context'

    do I need to put static Cont somewhere?

  4. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Homework - using 'IF' for 'For Loops'

    try to put static on your method.

  5. #5
    Junior Member
    Join Date
    Sep 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework - using 'IF' for 'For Loops'

    ok. got it.

    thanks again!

    +rep

  6. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Homework - using 'IF' for 'For Loops'

    lol thanks chronoz, again I had no way of testing so. I normally miss statics lol!

    Please mark as solved if thats everything!

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

Similar Threads

  1. Mini Sudoku game 4x4 ( four 2x2 grids) program
    By derky in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 16th, 2009, 07:39 AM
  2. How to Use different Java loops
    By JavaPF in forum Java Programming Tutorials
    Replies: 1
    Last Post: August 28th, 2009, 03:10 AM
  3. Replies: 11
    Last Post: April 29th, 2009, 03:12 AM
  4. [SOLVED] What is cast operator and how to use it?
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 27th, 2009, 06:11 AM
  5. [SOLVED] Java program to prompt and display the user input number as odd or even
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 22nd, 2009, 01:19 AM