代码如何知道参数“linenum”的值是什么?和“亚麻”。是? (JAVA) [英] How does the code know what the values for the parameters "linenum" and "linenumber" are? (JAVA)

查看:113
本文介绍了代码如何知道参数“linenum”的值是什么?和“亚麻”。是? (JAVA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿!



我是一个完全初学者学习Java所以我很抱歉这个简单的问题!以下程序输出金字塔。我想知道后三种方法如何知道分配给它们的参数的值。他们从哪里得到这个值?



Hey!

I'm a complete beginner learning Java so I am sorry for the simple question! The following program outputs a pyramid. I am wondering how the last three methods know what value to assign to their parameters. Where do they get this value?

import java.util.Scanner;

public class StaticPyramidProg
{
   public final static int MARGIN = 10;

   public static char brickCharacter;
   public static int height;
   
   public static void main(String[] args)
   {
      Scanner scan = new Scanner(System.in);
      System.out.print("\n\tEnter the number of lines for the pyramid: ");
      height = scan.nextInt();
      System.out.print("\tEnter the character from which the pyramid should be made: ");
      brickCharacter = scan.next().charAt(0);
      
      System.out.println(pyramidString());
   }
   
   //-----------------------------------------------------------------
   //  Returns a string containing a representation of the pyramid.
   //-----------------------------------------------------------------
   public static String pyramidString()
   {
      String pattern = "\n";
      for (int lineCount = 1; lineCount <= height; lineCount++)
      {
         pattern += pyramidLine(lineCount);
      }
      return pattern;
   }
   
   //-----------------------------------------------------------------
   //  Returns a string containing a representation of 
   //  a given line of the pyramid.
   //-----------------------------------------------------------------
   private static String pyramidLine(int lineNumber)
   {
      String line = "";
      line += spacesForPyramidLine(lineNumber);
      line += symbolsForPyramidLine(lineNumber);
      line += "\n";
      return line;
   }
   
   //-----------------------------------------------------------------
   //  Returns a string of spaces for the given line of the pyramid.
   //-----------------------------------------------------------------
   private static String spacesForPyramidLine(int lineNum)
   {
      String lineSpaces = "";
      for (int spacesCount = 1; spacesCount <= (MARGIN + height + 1 - lineNum); spacesCount++)
      {
         lineSpaces += " ";
      }
      return lineSpaces;
   }
   
   //-----------------------------------------------------------------
   //  Returns a string of symbols for the given line of the pyramid.
   //-----------------------------------------------------------------
   private static String symbolsForPyramidLine(int lineNum)
   {
      String lineSymbols = "";
      for (int symbolsCount = 1; symbolsCount <= ((lineNum * 2) - 1); symbolsCount++)
      {
         lineSymbols += brickCharacter;
      }
      return lineSymbols;
   }
}





非常感谢你的帮助!!



我尝试了什么:



这不仅仅是一个问题而不是排除故障



Thank you so much for your help!!

What I have tried:

This is more of a question than a troubleshoot

推荐答案

在调用它时将它们传递给函数。

定义函数时,指定需要给出的参数:

You pass them to the function when you call it.
When you define a function, you specify what parameters it needs to be given:
private static string pyramidLine(int lineNumber)
{
   ...
}

说当你调用pyramidLine时,你必须给它一个整数,它将返回一个字符串。

在函数体内,参数值存储在一个变量中 - 在本例中称为lineNumber - 函数体的代码可以使用。这与该名称的任何其他变量无关:函数参数是该函数的本地函数,仅适用于该函数。

因此,当您调用该函数时,您只需传递一个整数值:

Says "when you call pyramidLine, you must give it an integer, and it will return a string".
Within the body of the function, the parameter value is stored in a variable - in this case called lineNumber - which the code of the function body can work with. This is unrelated to any other variables of that name: the function parameter is local to that function and that function only.
So when you call the function, you just pass it an integer value:

String s;
s = pyramidLine(666);

或者:

Or:

String s;
int lines = 666;
s = pyramidLine(lines);

甚至:

Or even:

String s;
int lines = 666;
s = pyramidLine(lines * 10);

评估该值,并将其传递给函数。

The value is evaluated, and passed to the function.


这篇关于代码如何知道参数“linenum”的值是什么?和“亚麻”。是? (JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆