0
import java.util.Scanner;

public class Ve {
  public static void main(String[]args){

    int hoyde;
    int linje = 0;

    Scanner tast = new Scanner(System.in);

    System.out.println("Hvor hoy skal din V bli?(mellom 2 og 10)");
    hoyde = tast.nextInt();
    tast.nextLine();

     //assert (hoyde <=2 && hoyde >=10) : "hoyde må være mellom 2 og 10";

    for(linje= 0; linje < hoyde;linje++) {
      int start = linje;
      int end = (hoyde-1)*2;

      for(linje= 0;linje<=linje;linje++){
        if(linje == end){
          System.out.println("*");
          break;
        }
        else if(linje == start){
          System.out.print("*");
        } else{
          System.out.print(" ");
        }
      }
    }
  }
}

I am trying to print out a V using *. Somehow when I run the code, it prints out 2 single stars on the same line. I am stuck on this one, and I can't seem to find out how I am supposed to spread given information. I use the Scanner for the input of a number for what height the V should have.

tmarwen
  • 15,750
  • 5
  • 43
  • 62
  • You can't use the same variable for both loops. Create a new one each time for your second loop. – Keppil Sep 10 '14 at 20:02
  • `for(...;linje<=linje;...){` this is going to cause problems `linge` should always equal `linge`. – Shaded Sep 10 '14 at 20:04

2 Answers2

1

Even though it is not addressing any Java related stuff but only pure ad hoc algorithm, it shouldn't be answered. But since you are using drjava I assume you are a just in your first path in Java world and it would be good to give a hand.

As @Keppil already stated in his comment, never use the same iteration primitive in two endorsed loop, unless there is really a special treatment (never met one myself), because you will be loosing the first loop index when its is incremented (decremented) by the inside loop.

There is also some algo approaches, and hope they will be clear with comments in below code:

public class FancyVDrawer
{
  public static void main(String[] args)
  {
    int hoyde;
    int linje = 0;
    Scanner tast = new Scanner(System.in);
    System.out.println("Hvor hoy skal din V bli?(mellom 2 og 10)");
    hoyde = tast.nextInt();
    tast.nextLine();
    //assert (hoyde <=2 && hoyde >=10) : "hoyde må være mellom 2 og 10";
    ++hoyde; //We need hoyde line stars thus should be incremented by one since we cannot draw one star in last line
    int end = (hoyde - 1) * 2; // The end should be calculated once, then decremented and not reassigned in each loop iteration
    for (linje = 0; linje < hoyde; linje++)
    {
      int start = linje;
      for (int i = 0; i < end; i++)
      {
        if ((i == start))
        {
          System.out.print("*");
        } else if (i == (end-1))
        {
          System.out.println("*");
          --end; // Decrement the end index once we are going to a new line
        }
        else
        {
          System.out.print(" ");
        }
      }
      if(linje == (hoyde - 2)) // Should break to prevent writing the last lonely star
        break;
    }
  }
}
tmarwen
  • 15,750
  • 5
  • 43
  • 62
0

It prints star 1 when (linje==start) and star 2 when (linje==end). After the inner loop, linje>hoyde, therefore exiting the outer loop.

As noted above you might want to give your loop variables different names.

Ajk_P
  • 1,874
  • 18
  • 23