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

时间:2016-09-08 16:15:45

标签: java for-loop

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

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

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

我需要使用的下一个输入是2,3,4。我的代码获得右边的高度(2),但不是宽度。

我需要的最后一个输入是3,3,7。我的代码完全没有。这是我到目前为止所做的。

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

  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("*");

输出箭头的外观示例:

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

5 个答案:

答案 0 :(得分:1)

你必须制作两个嵌套循环。内部循环将以单行打印字符,外部循环将打印多行。

这是针对'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();
}

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

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

相当于:

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

答案 1 :(得分:1)

可以使用两个第一个打印输入宽度的行数,使用substring方法捕获打印量*

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

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));

答案 2 :(得分:1)

对于这个问题,您可能希望使用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 
}

此方法允许用户输入箭头的任何大小(当然保留在int的值边界内)

答案 3 :(得分:0)

我知道这有点晚了,但是我在zybook上经历了同样的挑战(碰巧是实验室活动)。上面标记为已解决的代码是正确的,但是我认为我会为大家澄清的代码中有些地方不对。下面是我过去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;
   }
}

答案 4 :(得分:0)

基于@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;
   }
}