[英]Nested for loops to print inside a rectangle frame
我正在尝试在矩形框内打印一棵树,虽然我的方法完全有效,但我无法弄清楚如何相互实现它们。
我试图在框架方法中打印树方法而不是空格,但它不起作用,现在我不知道该怎么做。
我目前正在学习 java,所以它对我来说仍然是一个全新的领域。
import java.util.Scanner;
public class Weihnachtsbaum {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter a number between 1 and 20: ");
int height = scan.nextInt();
while((height < 1) || (height > 20))
{
System.out.println("Number not inside parameters!");
System.out.print("Enter a number between 1 and 20: ");
height = scan.nextInt();
}
//this is only to test that the outputs are right
tree(height);
System.out.println();
frame(height);
}
static void tree(int height)
{
for(int i = 1;i <=height; i++)
{
//printing star
if(i == 1)
{
for (int j = height - 2; j > 0; j--)
{
System.out.print(" ");
}
for (int k = 1; k > 0; k--)
{
System.out.println(">()<");
}
}
//printing spaces
for (int space = 1;space <= height - i; space++)
{
System.out.print(" ");
}
//branches
for (int branch = 1; branch <= i; branch++)
{
System.out.print("/\\");
}
//next line
System.out.println();
//lower part of tree
if(i == height)
{
for(int j = (height-1);j > 0;j--)
{
System.out.print(" ");
}
for(int k = 2;k > 0;k--)
{
System.out.print("|");
}
}
}
}
static void frame(int height)
{
for(int rows = 0; rows <= height+4; rows++)
{
for(int columns = 0; columns <= height*2+4; columns++)
{
if((rows == 0 && columns == 0) || (rows == height+4 && columns==0) || (columns == height*2+4 && rows == 0) || (rows == height+4 && columns == height*2+4))
{
System.out.print("+");
}
else if( rows == 0 || rows == height+4)
{
System.out.print("-");
}
else if(columns == 0 || columns == height*2+4)
{
System.out.print("|");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}
我想我可能已经做到了,即使代码不是最好看的,但这应该能做到,感谢您的帮助。 如果你能帮助我最小化代码,我将不胜感激,但如果不能,这应该足以获得批准,因为它有效。
import java.util.Scanner;
public class weihnachtsbaum2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Bitte eine Zahl zwischen 1 und 20 eingeben: ");
int height = scan.nextInt();
while ((height < 1) || (height > 20))
{
System.out.println("Nicht gültige Zahl! ");
System.out.print("Bite eine gültige Zahl zwischen 1 und 20 eingeben: ");
height = scan.nextInt();
}
topFrame(height);
middlePart(height);
bottomPart(height);
}
static void topFrame(int height)
{
//Top of Frame
for (int rows = 0; rows <= height; rows++)
{
for (int columns = 0; columns <= height+2; columns++)
{
if((rows == 0 && columns == 0) || (rows == height && columns==height+2))
{
System.out.print("+");
}
else if( rows == 0 || rows == height)
{
System.out.print("-");
}
}
if(rows == height)
{
System.out.println();
}
}
}
static void middlePart(int height)
{
for (int rows = 0; rows <= height-1; rows++)
{ //spalten
//printing star
if(rows == 0)
{
System.out.print("|");
for (int j = height; j > 0; j--)
{
System.out.print(" ");
}
for (int k = 1; k > 0; k--)
{
System.out.print(">()<");
}
for(int x = 0; x <= height-1; x++)
{
System.out.print(" ");
}
System.out.print("|\n");
}
if(rows >= 0)
{
System.out.print("|");
}
for (int spaceLeft = 0;spaceLeft <= height - rows; spaceLeft++)
{
System.out.print(" ");
}
//branches
for (int branch = 0; branch <= rows; branch++)
{
System.out.print("/\\");
}
for (int spaceRight = 0;spaceRight <= height - rows; spaceRight++)
{
System.out.print(" ");
}
System.out.print("|");
System.out.println();
if(rows == height-1)
{
System.out.print("|");
for(int j = (height);j >= 0;j--)
{
System.out.print(" ");
}
for(int k = 2;k > 0;k--)
{
System.out.print("|");
}
for (int spaceRight = 0;spaceRight <= height; spaceRight++)
{
System.out.print(" ");
}
System.out.print("|");
}
}
System.out.println();
}
static void bottomPart(int height)
{
//bottom of Frame
for (int rows = 0; rows <= height; rows++)
{ //spalten
for (int columns = 0; columns <= height+2; columns++)
{
if((rows == 0 && columns == 0) || (rows == height && columns==height+2))
{
System.out.print("+");
}
else if( rows == 0 || rows == height)
{
System.out.print("-");
}
}
if(rows == height)
{
System.out.println();
}
}
}
}
使用循环的另一种方法是使用String.repeat语句。 您也可以使用相同的方法打印顶部和底部边框。
public class Weihnachtsbaum2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Bitte eine Zahl zwischen 1 und 20 eingeben: ");
int height = scan.nextInt();
while ((height < 1) || (height > 20)) {
System.out.println("Nicht gültige Zahl! ");
System.out.print(
"Bite eine gültige Zahl zwischen 1 und 20 eingeben: ");
height = scan.nextInt();
}
topAndBottom(height);
middlePart(height);
topAndBottom(height);
}
static void topAndBottom(int height) {
// Top of Frame
System.out.println("+" + "-".repeat(2 * (height+2)) + "+");
}
static void middlePart(int height) {
String spaces = " ".repeat(height);
String leftEnds = "|" + spaces;
String rightEnds = spaces + "|";
System.out.println(leftEnds + ">()<" + rightEnds);
for (int row = 1; row <= height; row++) {
spaces = " ".repeat(height-row+1);
String left = "| " + spaces;
String right = spaces + " |";
String center = "/\\".repeat(row);
System.out.println(left + center + right);
}
System.out.println(leftEnds + " || " + rightEnds);
}
}
也许是这样的(不是最干净的代码,但可以工作)。 请注意一些更改 - 我已经对height
进行了硬编码,但您可以轻松地使用现有代码从用户那里获取输入。 此外,该程序不会打印星号 - 将其作为熟悉代码的练习:)
public class MyTree {
public static void main(String[] args) {
// You can reuse your code to accept input here
int height = 12;
printTree(height);
}
public static void printTree(int height){
printTop(height); // Prints the top row of frame
for(int i=0; i<height; i++){
printRow(i+1, height);
}
printTrunk(height); // Prints the tree stock/trunk
printBottom(height); // Prints the bottom row of the frame
}
public static void printTop(int height){
System.out.print("+");
for(int i=0; i < (height*2) + 4; i++){
System.out.print("-");
}
System.out.println("+");
}
private static void printBottom(int height){
printTop(height);
}
private static void printSpaces(int spaces) {
for(int i=0; i<spaces; i++){
System.out.print(" ");
}
}
private static void printRow(int rowNum, int height){
int spaces = (height - rowNum) + 3;
System.out.print("|");
printSpaces(spaces-1);
for(int i=0; i<rowNum; i++){
System.out.print("/\\");
}
printSpaces(spaces-1);
System.out.print("|");
System.out.println("");
}
private static void printTrunk(int height) {
System.out.print("|");
printSpaces(height + 1);
System.out.print("||");
printSpaces(height + 1);
System.out.print("|");
System.out.println("");
}
}
为输入 10 打印树(注意没有星号),如下所示:
随意根据自己的喜好改进代码(那里有很多scope)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.