-3

メソッドが正確に何をするのか混乱しているので、誰かがこのサンプルプログラムで概念を説明してくれることを望んでいました:

import java.io.*;
import java.util.*;

public class programB {

   public static void main(String[] args) throws IOException {

      String filename;
      String total="";
      String c = "";
      String size = "";
      int num1=0, num2=0;
      char ch;

      Scanner keyboard = new Scanner(System.in);

      System.out.println("Enter the name of the file you want to read data from:"); 
      filename=keyboard.next(); 

      Scanner fromFile = new Scanner(new FileReader(filename));

      while (fromFile.hasNext()) {
         String type = fromFile.next();

         ch = fromFile.next().charAt(0);
         num1 = fromFile.nextInt();
         if (type.equals("rectangle")) {
            num2 = fromFile.nextInt();
         }
         System.out.print(type + " ");
         System.out.print(ch + " ");
         System.out.print(num1 + " ");
         if (type.equals("rectangle")) {
            System.out.print(num2);
        }
         System.out.println();
         if (type.equals("rectangle")){
            rectangle(ch,num1,num2);

         }
         if (type.equals("triangle")){
            triangle(ch, num1);
         }

      }


   }
   /** Draw a rectangle of the specified width and height
    @param c the character that creates the drawing
    @param height the height of the rectangle
    @param width the width of the rectangle
*/
   public static void rectangle(char c, int height, int width){

      for (int i=1; i<=height; i++) {
         System.out.println();
         for (int a=1; a<= width; a++) {
            System.out.print(c);
     }

  }
  System.out.println();

   // put statements here to display a rectangle on the screen using
   // character c and the width and height parameters
   }



/** Draw a right triangle.
@param c the character that creates the drawing
@param size the height and width of the triangle
*/

   public static void triangle(char c, int size){
      for (int i=1; i<=size;i++) {
         for (int j=1; j<=i; j++) {
            System.out.print(c);
         }
         System.out.println();

      // put statements here to display a triangle on the screen using
      // character c and the size parameter
      }
   }
 }

メソッドは何のためのもので、何をするものですか? オンラインで調べてみたり、教科書を読んだりしていますが、まだその概念に本当に混乱しています。

4

2 に答える 2