[英]How can I draw a half arrow with asterisks in java?
我在我的第一個 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();
}
如果它們只包含一行,則不需要在循環中使用括號。 例如:
for(int i = 0; i < a; i++)
System.out.print("*");
相當於:
for(int i = 0; i < a; i++)
{
System.out.print("*");
}
可以使用兩個作為第一個來打印輸入寬度的行數,使用子字符串方法來捕獲打印量*
然后第二次打印箭頭的頭部並減小箭頭的寬度
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 循環,因為你知道它應該重復的確切次數。 您知道這一點是因為用戶輸入了箭頭每個部分的大小。
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 的值邊界內)
我知道這有點晚了,但我正在 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;
}
}
我的完整解決方案基於@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;
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("");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.