这些方法在这个Java程序中做什么?
我不知道方法到底能做什么,所以我希望有人能用这个示例程序向我解释这个概念:这些方法在这个Java程序中做什么?,java,methods,Java,Methods,我不知道方法到底能做什么,所以我希望有人能用这个示例程序向我解释这个概念: 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
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
}
}
}
import java.io.*;
导入java.util.*;
公共课程B{
公共静态void main(字符串[]args)引发IOException{
字符串文件名;
字符串总数=”;
字符串c=“”;
字符串大小=”;
int num1=0,num2=0;
char ch;
扫描仪键盘=新扫描仪(System.in);
System.out.println(“输入要从中读取数据的文件名:”);
filename=键盘.next();
Scanner fromFile=new Scanner(new FileReader(filename));
while(fromFile.hasNext()){
字符串类型=fromFile.next();
ch=fromFile.next().charAt(0);
num1=fromFile.nextInt();
if(type.equals(“矩形”)){
num2=fromFile.nextInt();
}
系统输出打印(类型+“”);
系统输出打印(ch+“”);
系统输出打印(num1+“”);
if(type.equals(“矩形”)){
系统输出打印(num2);
}
System.out.println();
if(type.equals(“矩形”)){
矩形(ch,num1,num2);
}
if(类型等于(“三角形”)){
三角形(ch,num1);
}
}
}
/**绘制一个指定宽度和高度的矩形
@参数c创建图形的字符
@param height矩形的高度
@param width矩形的宽度
*/
公共静态空白矩形(字符c、整数高度、整数宽度){
对于(int i=1;i方法附带的文档在解释它们的功能方面做得很好。rectangle()
方法在使用参数'-',3和4调用时,将在运行程序的控制台中“绘制”一个3高4宽的矩形:
----
----
----
triangle()
方法类似;使用“-”和3调用时,您将得到如下形状:
-
--
---
方法是编写代码的基本方法,可以多次使用。Java中的方法允许您在代码中定义某些操作
这样想吧:你知道怎么画三角形,对吧?如果我告诉你“去画三角形”,你就知道该怎么做了。我不用说,“把你的铅笔放在一张纸上,画一条短的线,然后不用拿起铅笔,画另一条线,然后画一条连接你的铅笔现在到你开始的地方的最后一条线。”这是一大堆说明,所以让我们说,每当我想让你画一个三角形,我就说,triangle()
。类似地,让我们对rectangle()
执行相同的操作
但是如果这变得更复杂一点呢?我不是告诉你“画一个三角形”,而是说,“用ASCII字符画一个具有一定高度和宽度的三角形,“这会有多困难?其实很简单-你需要知道三件不同的事情:字符
,高度
,宽度
。一旦你有了这些,你就知道该做什么了
当你调用triangle()
和rectangle()
时,这正是你的代码所做的-你将三个参数传入其中每一个,你的代码将绘制一个三角形和一个矩形。你可以任意多次这样做。谢谢!!!这帮了大忙!!