java 如何在java中绘制带星号的半箭头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39395849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:20:12  来源:igfitidea点击:

How can I draw a half arrow with asterisks in java?

javafor-loop

提问by J Learning

I am in my first java class, and I am trying to draw a half arrow using asterisks. I am supposed to use a nested loop where the inner loop draws the *s and the outer loop iterates the number of times equal to the height of the arrow base. I have learned if-else, while loops, and for loops.

我在我的第一个 java 课上,我正在尝试使用星号绘制一个半箭头。我应该使用嵌套循环,其中内循环绘制 *s,外循环迭代等于箭头底高度的次数。我已经学会了 if-else、while 循环和 for 循环。

So far, I have been able to correctly draw the arrow for input values of
arrow base height: 5
arrow base width: 2
arrow head width: 4

到目前为止,我已经能够正确地为
箭头基高的输入值绘制箭头:5
箭头基宽:2
箭头宽度:4

When I try to add a while loop as the outer loop, the program times out. I am at a loss.

当我尝试添加一个 while 循环作为外循环时,程序超时。我很茫然。

The next input I need to use is 2, 3, 4. My code gets the height of the base right (2), but not the width.

我需要使用的下一个输入是 2、3、4。我的代码获取底部右侧 (2) 的高度,而不是宽度。

The last input I need is 3, 3, 7. My code gets none of that right at all. This is what I have so far.

我需要的最后一个输入是 3、3、7。我的代码根本没有得到正确的输入。这就是我迄今为止所拥有的。

What kind of loops should I be using to get the widths correct?

我应该使用什么样的循环来获得正确的宽度?

  Scanner scnr = new Scanner(System.in);
  int arrowBaseHeight = 0;
  int arrowBaseWidth  = 0;
  int arrowHeadWidth = 0;
  int i = 0;

  System.out.println("Enter arrow base height: ");
  arrowBaseHeight = scnr.nextInt();

  System.out.println("Enter arrow base width: ");
  arrowBaseWidth = scnr.nextInt();

  System.out.println("Enter arrow head width: ");
  arrowHeadWidth = scnr.nextInt();


  for (i = 1; i <= arrowBaseHeight; ++i) {
      // Draw arrow base (height = 3, width = 2)
      System.out.println("**");
  }

  // Draw arrow head (width = 4)
  System.out.println("****");
  System.out.println("***");
  System.out.println("**");
  System.out.println("*");

Example of how the output arrow may look:

输出箭头的外观示例:

**
**
**
**
****
***
**
*

采纳答案by Jakob Strobl

For this problem, you would want to use for-loops since you know the exact number of times it should repeat. You know this because the user inputs the size of each part of the arrow.

对于这个问题,你会想要使用 for 循环,因为你知道它应该重复的确切次数。您知道这一点是因为用户输入了箭头每个部分的大小。

Scanner scnr = new Scanner(System.in);

int arrowBaseHeight = 0;
int arrowBaseWidth  = 0;
int arrowHeadWidth = 0;
int i = 0;

System.out.println("Enter arrow base height: ");
arrowBaseHeight = scnr.nextInt();

System.out.println("Enter arrow base width: ");
arrowBaseWidth = scnr.nextInt();

System.out.println("Enter arrow head width: ");
arrowHeadWidth = scnr.nextInt();

//Your code above | Below is the modified code

String ast = ""; //String ast will contain how many asterisk we want for the base width;

for (int x = 1; x <= arrowBaseWidth; x++) //Loop forms the base width of the arrow
{
    ast += "*"; //This adds as many asterisks as we need to make the base width. SO if they enter 4, we get 4 *;
}


for (i = 1; i < arrowBaseHeight; ++i) 
{   
    System.out.println(ast); //Prints out the base width, which is now a String object
}

int tempHeadWidth = arrowHeadWidth; //Added this tempHeadWidth variable since we will be modifying it directly and 
                                    //we don't want to modify the original data and variable (it will cause problems if we do.

for (int y = 1; y <= arrowHeadWidth; y++) 
{
    for(int z = tempHeadWidth; z > 0; z--) //This loop prints the amount of asterisks we need per line in the arrowHead
    {
        System.out.print("*");
    } 
    // Once the loop above is finished, the rest of the code will execute in the main for-loop and then scheck if it will run again.
    tempHeadWidth -= 1; //So we are lowering the tempHeadWidth by one so the next time it enters 
                        //the nested (2nd) for loop it will be one asterisk smaller

    System.out.println(); //This makes a new line to keep adding more stars for the next row 
}

This method allows the user to input any size for the arrow (while staying in the value boundaries of an int of course)

此方法允许用户输入任意大小的箭头(当然,同时保持在 int 的值边界内)

回答by hal

You have to make two nested loops. Inner loops will be printing characters in single line and outer loops will be printing multiple lines.

您必须进行两个嵌套循环。内循环将打印单行字符,外循环将打印多行。

This is solution for your question with 'for' loops.

这是您使用“for”循环的问题的解决方案。

//printing arrow base
for (int h = 0; h < arrowBaseHeight; ++h)
{
  //printing single line - every line is the same
  for(int w = 0; w < arrowBaseWidth; w++)
    System.out.print("*");
  //finishing line
  System.out.println();
}

//printing arrow head
//starting with provided width and decreasing it with every iteration
for (int a = arrowHeadWidth; a > 0 ; a--)
{
  //printing single line - now every line is different
  //you have to count how many asterisks you are printing
  for(int i = 0; i < a; i++)
    System.out.print("*");
  //finishing line
  System.out.println();
}

You don't need to use brackets in loops if they contain only one line. For example:

如果循环中只包含一行,则不需要在循环中使用括号。例如:

for(int i = 0; i < a; i++)
    System.out.print("*");

is equivalent to:

相当于:

for(int i = 0; i < a; i++)
{
    System.out.print("*");
}

回答by Dev. Joel

Could use two for the first to print the number of rows with the entered width entered using the substringmethod to capture the amount of printing *

可以使用两个作为第一个打印具有输入宽度的行数,使用子字符串方法输入以捕获打印量*

then the second printing for the head of the arrow and go decreasing the width of the arrow

然后第二次打印箭头的头部并减小箭头的宽度

for (int i = 0;i < arrowBaseHeight; i++) 
 //when there is more than one instruction within a structure can be written without {}
    System.out.println("*************************".substring(0, arrowBaseWidth));

System.out.println("");
for (int i = arrowHeadWidth; i>=0; i-=1)  // head
    System.out.println("*************************".substring(0, i));

回答by RobertB922

I know this is a bit late but I am going through this same challenge on zybooks (which happens to be a lab activity). The above code marked solved is right but there are a few things that are off in the code that I thought I would clarify for everyone. Below is the code I used to pass 100%, and I will comment on the areas that needed to be modified. I also made the indentation a little easier to read. I hope this helps anyone needing a little push in the right direction.

我知道这有点晚了,但我正在 zybooks 上经历同样的挑战(这恰好是一项实验室活动)。上面标记为已解决的代码是正确的,但代码中有一些问题我想我会为大家澄清。下面是我以前100%通过的代码,需要修改的地方我再评论。我还使缩进更容易阅读。我希望这可以帮助任何需要朝正确方向推动的人。

import java.util.Scanner; 

public class DrawHalfArrow {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      int arrowBaseHeight = 0;
      int arrowBaseWidth  = 0;
      int arrowHeadWidth = 0;

  System.out.println("Enter arrow base height: ");
  arrowBaseHeight = scnr.nextInt();

  System.out.println("Enter arrow base width: ");
  arrowBaseWidth = scnr.nextInt();

/* The while loop below needed to be added for the user input to make sure
** that their input never exceeded that of the arrowHeadWidth */

  while (arrowHeadWidth <= arrowBaseWidth) {
     System.out.println("Enter arrow head width: ");
     arrowHeadWidth = scnr.nextInt();
  }

  String ast = "";
  for (int x = 1; x <= arrowBaseWidth; x++) {
     ast+= "*";
  }

/* Here 'i' needed to be intialized as an integer ('int'). Also the '=' needed
** to be added to i<= arrowBaseHeight */

  for (int i = 1; i <= arrowBaseHeight; ++i) { 
        System.out.println(ast);
  }
  int tempHeadWidth = arrowHeadWidth;
  for (int y =1; y <= arrowHeadWidth; y++) {
     for (int z = tempHeadWidth; z > 0; z--) {
        System.out.print("*");
     }
     tempHeadWidth -= 1;
     System.out.println();
  }

  return;
   }
}

回答by Carlito Santiago

My Full Solution based off @dev joels answer

我的完整解决方案基于@dev joels 的回答

import java.util.Scanner; 

public class DrawHalfArrow {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      int arrowBaseHeight = 0;
      int arrowBaseWidth  = 0;
      int arrowHeadWidth = 0;

      System.out.println("Enter arrow base height: ");
      arrowBaseHeight = scnr.nextInt();

      System.out.println("Enter arrow base width: ");
      arrowBaseWidth = scnr.nextInt();

      //System.out.println("Enter arrow head width: ");

      while (arrowHeadWidth <= arrowBaseWidth) {
        System.out.println("Enter arrow head width: ");
        arrowHeadWidth = scnr.nextInt();
      }       

      for (int h = 0; h < arrowBaseHeight; ++h) {
         for(int w = 0; w < arrowBaseWidth; w++) 
            System.out.print("*");
        System.out.println();
      }

       for (int a = arrowHeadWidth; a > 0 ; a--) {
          for(int i = 0; i < a; i++)
             System.out.print("*");
         System.out.println();
       }
      return;
   }
}