Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1.  
  2. class DrawBox {
  3.  
  4.     public static void main(String[] args) {
  5.         drawBox(10, 15);
  6.         /*
  7.      * FORMÅL:
  8.      * for-loops samt parametrisere printf
  9.      * dvs at System.out.printf("%14s%n","#") bliver til
  10.      * format = "%" + variabel + "s%n";
  11.      * myChar = "*"
  12.      * System.out.printf(format,myChar);
  13.      *
  14.      *
  15.      * OPGAVEN:
  16.      * tegn en firkant vha  metoden drawBox hvor højde og bredde
  17.      * angives som parametre
  18.      *
  19.      * TESTKØRSEL:
  20.      * drawBox(6,10) giver flg. figur
  21.  
  22.         **********
  23.         *        *
  24.         *        *
  25.         *        *
  26.         *        *
  27.         **********
  28.  
  29.          */
  30.  
  31.     }
  32.  
  33.     public static void drawBox(int height, int width) {
  34.         String format = "%s%" + (width - 1) + "s%n";
  35. //    String myEmpty = " ";
  36.         writeChars('*', width);
  37.         for (int i = 0; i < height - 2; i++) {
  38.             System.out.printf(format, "*", "*");
  39.         }
  40.         // TODO: skriv for-loop hvor hver iteration tegner en linje
  41.         // og hvor antallet af loops begrænses af height samt at
  42.         // width indgår i formateringen af printf.
  43.         // a la format = "*%"+width+"s" og så printf(format,args)
  44.         writeChars('*', width);
  45.  
  46.     }
  47.  
  48.     public static void writeChars(char myChar, int frequency) {
  49.         //TODO: skriv for-loopet der printer myChar det antal gange
  50.         // der er med som anden parameter i metodekaldet
  51.         for (int i = 0; i < frequency; i++) {
  52.             System.out.print(myChar);
  53.         }
  54.  
  55.         System.out.println();
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement