Java program that will ask the height and width of a rectangle and display the rectangle using "*" and "+" -


i having trouble program , quite frustrating! ive been trying solve cant pls need help.all can

import java.util.*;         public class main {     public static void main(string[] args) {         scanner s=new scanner(system.in);          system.out.print("enter height: ");         int h = s.nextint();          system.out.print("enter width: ");         int w = s.nextint();          (int i=1; i<=h; i++) {             (int j=1; j<=w; j++) {                 system.out.print("*");             }             system.out.println();         }     } } 

the output of program is:

enter height: 4 enter width: 4   **** **** **** **** 

and desired output should be:

enter height: 4 enter with: 4  ++++ **** ++++ **** 

another thing write program same different output be:

enter height: 3 enter width: 10  +*+*+*+*+* +*+*+*+*+* +*+*+*+*+* 

thanks in advance help.

]1

for first solution, put code inside inner loop

if(i%2==1)     system.out.print("+"); else      system.out.print("*"); 

for second solution: use above code replace 'if(i%2==1)' 'if(j%2==1)'


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -