Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中绘制带星号的半箭头?_Java_For Loop - Fatal编程技术网

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

如何在java中绘制带星号的半箭头?,java,for-loop,Java,For Loop,我在我的第一个java类中,我试图用星号画一个半箭头。我应该使用嵌套循环,其中内循环绘制*s,外循环迭代次数等于箭头底部的高度。我已经学会了if-else、while循环和for循环 到目前为止,我已经能够正确绘制输入值的箭头 箭头底部高度:5 箭头底部宽度:2 箭头宽度:4 当我尝试添加while循环作为外部循环时,程序超时。我不知所措 我需要使用的下一个输入是2,3,4。我的代码得到的是底部右侧的高度(2),但不是宽度 我需要的最后一个输入是3,3,7。我的代码一点也不正确。这就是我目前所拥

我在我的第一个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("*");

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

这是使用“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();
}
//打印箭头底座
对于(int h=0;h<箭头基准高度;++h)
{
//打印单行-每行都相同
对于(int w=0;w0;a--)
{
//打印单行-现在每行都不同了
//您必须计算您正在打印的星号数
for(int i=0;i
如果循环只包含一行,则不需要在循环中使用括号。例如:

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

for(int i = 0; i < a; i++)
{
    System.out.print("*");
}
for(int i=0;i
可以使用两个作为第一个,以使用捕获打印量的方法输入的宽度打印行数
*

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

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));
for(int i=0;i=0;i-=1)//头
System.out.println(“****************************”。子字符串(0,i));

对于此问题,您可能希望使用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 
}
Scanner scnr=新扫描仪(System.in);
int arrowsbaseheight=0;
int arrowBaseWidth=0;
int arrowHeadWidth=0;
int i=0;
System.out.println(“输入箭头底部高度:”);
arrowBaseHeight=scnr.nextInt();
System.out.println(“输入箭头底部宽度:”);
arrowBaseWidth=scnr.nextInt();
System.out.println(“输入箭头宽度:”);
箭头宽度=序号nextInt();
//您上面的代码|下面是修改后的代码
字符串ast=“”//字符串ast将包含我们想要的基本宽度的星号数量;

对于(int x=1;x我知道这有点晚了,但我正在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;
   }
}
import java.util.Scanner;
公共类半箭头{
公共静态void main(字符串[]args){
扫描仪scnr=新扫描仪(System.in);
int arrowsbaseheight=0;
int arrowBaseWidth=0;
int arrowHeadWidth=0;
System.out.println(“输入箭头底部高度:”);
arrowBaseHeight=scnr.nextInt();
System.out.println(“输入箭头底部宽度:”);
arrowBaseWidth=scnr.nextInt();
/*需要为用户输入添加下面的while循环,以确保
**他们的输入从未超过箭头宽度*/

而(箭头宽度我基于@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;
   }
}
import java.util.Scanner;
公共类半箭头{
公共静态void main(字符串[]args){
扫描仪scnr=新扫描仪(System.in);
int arrowsbaseheight=0;
int arrowBaseWidth=0;
int arrowHeadWidth=0;
System.out.println(“输入箭头底部高度:”);
arrowBaseHeight=scnr.nextInt();
System.out.println(“输入箭头底部宽度:”);
arrowBaseWidth=scnr.nextInt();
//System.out.println(“输入箭头宽度:”);
而(箭头宽度0;a--){
for(int i=0;i
你的问题措辞很好,但你能告诉我们你想画什么吗?用4个空格的缩进来格式化它。如果你看最后的输出语句,我相信它会是这样的:谢谢!这就是我的输出应该是什么样子,取决于用户输入的数量。你可能想解释一下n到你的代码中(并修复缩进)。问题提到他们正在学习Java,所以向他们灌输代码对他们没有好处。谢谢你!这几天来造成了很大的压力。你的解释很好。我不得不改变的一件事是
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;
   }
}