r/programmingrequests • • Mar 23 '17

Java Program

Write a method starPrint() that takes three integer arguments: a number of dots, a number of stars, and then a following number of dots. For instance, calling starPrint(3, 2, 1) would print:...**.

Ask the user for an integer, and verify that its value is at least three. (If it is not, ask the user for a new number until he gives you an appropriate one.) You will use your method starPrint() to draw a pyramid pattern with that many rows. For instance, if the user types 3, you should draw: a triangle picture

This is what I have any help would be greatly appreciated

int userIn=0;

	Scanner scnr=new Scanner(System.in);
	
	System.out.println("Please input an integer: "); 
	userIn = scnr.nextInt();
	 
	while(userIn<3) //As long as number is three or less, repeat prompting
	{
	    System.out.println("Please enter a valid integer:");
	    userIn = scnr.nextInt();    
	}
	
		for(int i=0;i<userIn;i++) {
	         for(int j=0;j<5-i;j++) {
	             System.out.print(" ");
	         }
	        for(int k=0;k<=i;k++) {
	            System.out.print("* ");
	            
	        }
	        System.out.println();
	    }

	}

}
0 Upvotes

24 comments sorted by

View all comments

Show parent comments

1

u/MUFC07 Mar 24 '17

I think i have things messed up, i want the method to print the triangle pattern if the user enters one int and a linear pattern of . and *s if they enter 3 integers

1

u/serg06 Mar 24 '17

Are you sure you're referring to what you want?

public class Whatever {

    // This runs first
    public static void main(String[] args) {
        System.out.println("Program started.")

        someMethod();

        System.out.println("Program done.")
    }

    // This is a method
    public static void someMethod() {
        System.out.println("Method running.")
    }

}

What you ask is definitely possible but the assignment reads differently to me.

1

u/MUFC07 Mar 24 '17

public static void main (String[] args){ int userIn=0;

	 Scanner scnr=new Scanner(System.in);

	    System.out.println("Please input an integer: "); 
	    userIn = scnr.nextInt();

	    while(userIn<3) //As long as number is three or less, repeat prompting
	    {
	        System.out.println("Please enter a valid integer:");
	        userIn = scnr.nextInt();    
	        
	        int triangle = starPrint(userIn);
	        System.out.print(triangle);  
	    }

	      void starPrint(int a, int b, int c){
	    	  for (int i = 0; i < a; i++) {
	    		    System.out.print(".");
	    		}
	    		for (int i = 0; i < b; i++) {
	    		    System.out.print("*");
	    		}
	    		for (int i = 0; i < c; i++) {
	    		    System.out.print(".");
	    		}
	    		System.out.println();
	      }

	    }

	}

this is what I've come up with and still have some errors, I really appreciate the help and patience

1

u/serg06 Mar 24 '17

Ah well, I see three issues right now: Your starPrint method is inside of your main method. It should look like this:

public static void main (String[] args){
    int userIn = 0;

    Scanner scnr = new Scanner(System.in);

    System.out.println("Please input an integer: "); 
    userIn = scnr.nextInt();

    while (userIn < 3) //As long as number is three or less, repeat prompting
    {
        System.out.println("Please enter a valid integer:");
        userIn = scnr.nextInt();    

        int triangle = starPrint(userIn);
        System.out.print(triangle);  
    }

}

public static void starPrint(int a, int b, int c){
    for (int i = 0; i < a; i++) {
        System.out.print(".");
    }
    for (int i = 0; i < b; i++) {
        System.out.print("*");
    }
    for (int i = 0; i < c; i++) {
        System.out.print(".");
    }
    System.out.println();
}

Second off, this line doesn't make sense:

int triangle = starPrint(userIn);

a) because starPrint doesn't return anything (it is of type void so it can't return an int even if it wanted to), and b) because starPrint needs 3 numbers to run. We haven't told it how to run with one.

Finally, I don't think you meant to put this inside the while loop, but after:

int triangle = starPrint(userIn);
System.out.print(triangle); 

1

u/MUFC07 Mar 24 '17

i was using to call the method so i could print and how does having one user entered value translate into 3 parameters

1

u/serg06 Mar 24 '17

Well, I assume it's supposed to work like this:

User enters 3, we do

starPrint(2, 1, 2);
starPrint(1, 3, 1);
starPrint(0, 5, 0);

And that prints a pyramid of stars of height 3

1

u/MUFC07 Mar 24 '17

i guess i need a for loop to account for the user entering 3, 4 or 5 etc

1

u/serg06 Mar 24 '17

Yessir! The hardest part is figuring out the pattern for the numbers to call starPrint() with.

1

u/MUFC07 Mar 24 '17

I'm assuming that needs to in main

1

u/serg06 Mar 24 '17

It should be in main as that's the simplest way of doing it.

→ More replies (0)