for loop - How do I output a square in Java with a character like * or something else using run as a method and with using return? -
i want output square example *
. easy need use return command run method run test. square needs n x n big whereby n integer. problem dont know how variable. apparently need use unix newline character (dont know is). have this:
public static int run(int n) { int n = for(int row = 0; row!= ; row = row +1) { for(int col = 0; col!= ; col = col + 1); return } } }
the original assignment looked this. need run junit tests. why need use method run in combination return command
now use for-loops output square of stars of n×n. value of n willbe provided argument typeint.to create new line in string, use unix newline character. example of square n = 4:
* * * *
* * * *
* * * *
* * * *
this test. says expects string. really confused now.
import org.junit.test; import static org.junit.assert.*; public class assignment1_5test { @test public void getstarstest1() throws exception { string expected = "*\n"; assertequals(expected, assignment1_5.run(1)); } @test public void getstarstest2() throws exception { string expected = "**\n" + "**\n"; assertequals(expected, assignment1_5.run(2)); } @test public void getstarstest3() throws exception { string expected = "***\n" + "***\n" + "***\n"; assertequals(expected, assignment1_5.run(3)); }
here example comments.
public class test { public static void main(string[] args) { //call method wrote main system.out.println(run(4)); } public static string run(int n) { //you'll want use string variable can build on , add unix newline mentioned //so return type of method string string sq = ""; //go through n rows (int row = 0; row < n; row++) { //go through n columns (int col = 0; col < n; col++) { sq = sq + "*"; } //unix newline after each row complete sq = sq + "\n"; } //return string value return sq; } }
Comments
Post a Comment