如何在Java中用星号绘制一条半箭头?

3
我正在上我的第一堂Java课,我试图用星号绘制半箭头。我应该使用嵌套循环,其中内部循环绘制星号,外部循环迭代次数等于箭头底部的高度。我已经学会了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("*");

输出箭头的示例:

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

你的问题表述得很清楚,但是你能展示一下你想要绘制什么吗?请使用4个空格进行缩进。 - Tim Biegeleisen
如果您查看最后的输出语句,我相信它会像这样:http://pastebin.com/raw/hKu1Hxmp - N.J.Dawson
谢谢!这就是我的输出应该看起来的样子,取决于用户输入的数字。 - J Learning
6个回答

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
你可能需要在你的代码中加入一些解释(并修复缩进)。问题提到他们正在学习Java,所以直接给他们代码不会对他们有益。 - N.J.Dawson

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

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值范围内)。

非常感谢!这让我在过去的几天里感到很有压力。你的解释非常好。唯一需要更改的是第二个for循环中的<变成了<=,但我不确定为什么。 - J Learning

0
import java.util.Scanner; 

public class DrawHalfArrow {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      int arrowBaseHeight;
      int arrowBaseWidth;
      int arrowHeadWidth;
      
      System.out.println("Enter arrow base height:");
      arrowBaseHeight = scnr.nextInt();
      
      System.out.println("Enter arrow base width:");
      arrowBaseWidth = scnr.nextInt();
      
      
      do {
         System.out.println("Enter arrow head width:");
         arrowHeadWidth = scnr.nextInt();
      } while (arrowHeadWidth <= arrowBaseWidth); 
      
      System.out.println("");
      
      // Draw arrow base 
      int row;
      int column;
      for (row = 0; row < arrowBaseHeight; ++row) {
         for (column = 0; column < arrowBaseWidth; ++column) {
            System.out.print("*");
         }
         System.out.println("");
      }
            
      // Draw arrow head 
      for (row = 0; row < arrowHeadWidth; ++row) {
         int thisRowWidth = arrowHeadWidth - row;
         for (column = 0; column < thisRowWidth; ++column) {
            System.out.print("*");
         }
         System.out.println("");
      }
            
   }
}


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

0

我知道这有点晚了,但我现在正在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;
   }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接