1

Can someone tell me how I can change the values of x and y to '*' and '.'? Making the variables string doesn't help because I need N to be an integer as it is user input, and will conflict in loop.

import java.util.Scanner;

public class nvalue
{
public static void main(String[] args)
{
    int x, y, N;
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter Value of N : ");
    N = keyboard.nextInt();

    for(x=2; x<=N; x++)
    {
        for(y=1; y<=5; y++)
        {
         System.out.println(y +" * "+ x + " = " + (x*y));
        }
        System.out.println("----------------------");
    }
}
}

Basically I need it to output this:

* * * * * * 
. * * * * *
. . * * * *
. . . * * *
. . . . * *
. . . . . *
1
  • to * and . under what criteria? Commented Dec 12, 2013 at 8:11

5 Answers 5

2

You can do :

for (int i = 0; i < N; ++i) {
  for (int j = 0; j < i; ++j) {
     System.out.print(".");
  }

  for (int j = i; j < N; ++j) {
     System.out.print("*");
  }

  System.out.println(); // Next line
}

Note the use of print and not println for printing without a line break.

Sign up to request clarification or add additional context in comments.

4 Comments

You can even change the second for loop to for (; j < N; ++j) {, because as of the beginning of that loop, j is already equal to N.
@Widdershins not true, j is visible only in the for block
Well that's easy, just replace the first for block with int j = 0; for (; j < i; ++j) {. This is still a very fast and clean way of doing it.
I'm sure this is all extremely negligible in impact and will save approximately 1 cpu cycle per row drawn, but it's still more elegant. In my book, if you can make it any faster without making it look way worse, there's no reason not to do so.
2
for(x=0; x<N; x++){
    String ln = "";
    for(y=0; y<5; y++){
       ln += (y < x) ? "* " : ". ";
    }
    System.out.println(ln);
}

2 Comments

It's the trinary comparison operator - essentially a way to put an if into one statement.
now i understand. its not as complicated as looks lol. if y<x print "*", else print "." ... Boolean-ish. thanks.
1
for(x=1; x<=N; x++)
{
    for(y=1; y<=5; y++)
    {
      if (y >= x) {
         System.out.print(" * ");
      } else {
         System.out.print(" . ");
      }
    }
    System.out.println();
}

Comments

0
public static void main(String[] args) {
    int x, y, N;
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter Value of N : ");
    N = keyboard.nextInt();

    for(x=0; x<N; x++)
    {
        for(y=0; y<N; y++)
        {
            if (y >= x)    {
                System.out.print(" * ");
            }
            else {
                System.out.print(" . ");
            }
        }
        System.out.println();
    }

}

Comments

0

You can try this.

for(int x=0; x<N; x++){
  for(int y=0; y<N;y++){
   if(y<x){
      System.out.print(".");
    }else{
      System.out.print("*");
    }
   }
   System.out.println();
}

Damn, someone posted the same answer above me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.