MOOC Java:高级占星术,右倾三角形问题
所以我正在学习关于Java的MOOC课程,并且已经被困在同一个问题上好几天了。我把其他的东西都记下来了,但我似乎不知道怎么把三角形弄对。第一部分是根据输入大小打印一个右倾三角形。这是我的密码:MOOC Java:高级占星术,右倾三角形问题,java,Java,所以我正在学习关于Java的MOOC课程,并且已经被困在同一个问题上好几天了。我把其他的东西都记下来了,但我似乎不知道怎么把三角形弄对。第一部分是根据输入大小打印一个右倾三角形。这是我的密码: public class AdvancedAstrology { public static void printStars(int number) { // part 1 of the exercise int i = 0; while (i
public class AdvancedAstrology {
public static void printStars(int number) {
// part 1 of the exercise
int i = 0;
while (i < number){
System.out.print("*");
i++;
}
System.out.println("");
}
public static void printSpaces(int number){
// part 1 of the exercise
int i = 0;
while (i < number){
System.out.print(" ");
i++;
}
}
public static void printTriangle(int size) {
// part 2 of the exercise
int i = 0;
int j = size;
if(size == 1){
System.out.print("*");
System.out.println("");
}else{
while (i <= size){
printSpaces(j);
printStars(i);
j--;
i++;
}
}
}
public static void christmasTree(int height){
// part 3 of the exercise
int i = 1;
int j = height - 1;
while (i <= height){
printSpaces(j);
printStars(i*2-1);
i++;
j--;
}
printSpaces(height-2);
printStars(3);
printSpaces(height-2);
printStars(3);
}
public static void main(String[] args) {
// The tests are not checking the main, so you can modify it freely.
printTriangle(3);
System.out.println("---");
christmasTree(4);
System.out.println("---");
christmasTree(10);
}
}
我已经找到了这些树,但问题是右倾三角形上方的空白区域不允许我通过测试。以下是我得到的错误:
ComparisonFailure: You printed incorrectly when printTriangle(3) was called. expected:< []*
**
***
> but was:< [
]*
**
***
>
三角形在每条线的前面都有一个空格
编辑:感谢各位的快速回复!所有测试都已通过您的
printTriangle
方法从i=0开始,这意味着您的第一行打印0颗星。将其设置为i=1
public static void printTriangle(int size) {
// part 2 of the exercise
int i = 1; //<-- This line changed
int j = size - 1; //<-- This line is also changed
if(size == 1){
System.out.print("*");
System.out.println("");
}else{
while (i <= size){
printSpaces(j);
printStars(i);
j--;
i++;
}
}
}
publicstaticvoidprintTriangle(int-size){
//练习的第2部分
int i=1;//您可以在printTriangle方法中添加以下代码行:
if (i != 0) {
printSpaces(j);
printStars(i);
}
因此,它变成:
public static void printTriangle(int size) {
// part 2 of the exercise
int i = 0;
int j = size;
if (size == 1) {
System.out.print("*");
System.out.println("");
} else {
while (i <= size) {
if (i != 0) {
printSpaces(j);
printStars(i);
}
j--;
i++;
}
}
}
因此,最终版本是:
public static void printTriangle(int size) {
// part 2 of the exercise
int i = 1;
int j = size - 1;
if (size == 1) {
System.out.print("*");
System.out.println("");
} else {
while (i <= size) {
printSpaces(j);
printStars(i);
j--;
i++;
}
}
}
publicstaticvoidprintTriangle(int-size){
//练习的第2部分
int i=1;
int j=尺寸-1;
如果(大小==1){
系统输出打印(“*”);
System.out.println(“”);
}否则{
而(我你的意思是这样吗
printTriangle(4);
印刷品
*
**
***
****
方法
public static void printTriangle(int size) {
// part 2 of the exercise
for(int i = 1; i <= size; i++) {
System.out.println(" ".repeat(size-i) + "*".repeat(i));
}
}
publicstaticvoidprintTriangle(int-size){
//练习的第2部分
对于(int i=1;感谢您的快速评论,此问题已得到简单解决,但现在导致了一个不同的问题。顶部的空间现在已移动到三角形的一侧。仅三角形的输出已更改为:`***********************--`我会发布编辑,太棒了!我不认为我会想到这一点,但它是工作!
printTriangle(4);
*
**
***
****
public static void printTriangle(int size) {
// part 2 of the exercise
for(int i = 1; i <= size; i++) {
System.out.println(" ".repeat(size-i) + "*".repeat(i));
}
}