Results 1 to 4 of 4
  1. #1
    Xavier is offline Member
    Join Date
    May 2013
    Posts
    7
    Rep Power
    0

    Default help with replacing "for" statements

    my teacher told us to make a multiplication table program that displays the multiplicants of the number the user enters. i need to change the "for" statements because my class hasn't learned that yet, but i know it from prior knowledge. please help me out with this tough situation. This is the code i have so far:

    import java.util.Scanner;

    public class MultiplicationTable {
    public static void main(String[] args) {
    int user = 0;
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter number of columns: ");
    user = scan.nextInt();
    for(int i = 0; i <= user;i++) {
    System.out.print("\t" + i);}
    System.out.println();
    for(int i = 1 ;i <= user;i++) {
    System.out.print("\t" + i);
    for(int j=1;j <= user;j++) {
    System.out.print("\t" + i*j);
    }
    System.out.println();
    }
    }
    }

    Thanks!

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    3

    Default Re: help with replacing "for" statements

    why do you print an empty line after your print statement? just use

    Java Code:
    System.out.println("\t" + i)
    
    //or
    
    System.out.print("\t" + i + "\n")
    You havent been taught for loops? Thats bull *** by your school! for loops are a fundamental especially for basic programming tasks!

    Anyways, what have you learned? have you learned do-while loops, or just while loops?
    Last edited by doWhile; 05-10-2013 at 11:38 PM. Reason: Removed profanity

  3. #3
    Xavier is offline Member
    Join Date
    May 2013
    Posts
    7
    Rep Power
    0

    Default Re: help with replacing "for" statements

    The class has just got into java and have only learned while loops so far. We just came out of visual basic to get a basic understanding of programming.

  4. #4
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    3

    Default Re: help with replacing "for" statements

    not too hard to swap a for loop to a while loop :

    Java Code:
    int x = scan.nextInt();
    int i = 0;
    while(i < x) {
    
    //do you print statments
    
    i++
    
    }

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 09:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 04:06 PM
  3. I cant figure out my "if "else" statements any help?
    By prendergast36 in forum New To Java
    Replies: 9
    Last Post: 09-17-2011, 01:36 AM
  4. Replies: 1
    Last Post: 10-20-2008, 08:35 AM
  5. Replies: 1
    Last Post: 05-07-2008, 02:19 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
  •