我对究竟是什么方法感到困惑,所以我希望有人用这个示例程序向我解释这个概念:
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
}
}
}
方法是什么,它们有什么作用?我试过在网上研究它,我一直在研究我的教科书,但我仍然对这个概念感到困惑。