输入负高度时结束过程
Ending process when negative height is entered
我无法完成这个程序,我们应该编写一个程序来使用“*”绘制一个三角形,它要求高度和宽度,我们需要让它进入一个循环它一直要求更多的高度和宽度,直到用户在高度中输入负值,它应该存储在那里。这是我目前所拥有的。
import java.util.Scanner;
public class 矩形 {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
System.out.print("\n\n"
+ "Drawing Rectangles\n"
+ "------------------\n\n");
while(height >= 0) {
int height = -1, width = -1;
System.out.print("Enter the height and width of a rectangle: ");
height = kbd.nextInt();
width = kbd.nextInt();
kbd.nextLine();
System.out.println("\n"
+ "Here is your rectangle:");
drawRectangle(height, width);
System.out.print("\n\n");
}
}
private static void drawRectangle(int height, int width) {
for (int line = 1; line <= height; line++) {
for (int star = 1; star <= width; star++) {
System.out.print("*");
}
System.out.println();
}
}
}
到目前为止我有这个,它开始,要求高度,宽度然后绘制一个三角形就是这样。我有什么想法可以让它进入一个循环以要求更多并且仅当用户在高度中输入负值时才停止?不允许使用 IF 语句。提前致谢
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
System.out.print("\n\n"
+ "Drawing Rectangles\n"
+ "------------------\n\n");
int height = -1, width = -1;
System.out.print("Enter the height and width of a rectangle: ");
height = kbd.nextInt();
width = kbd.nextInt();
kbd.nextLine();
while(height > -1) {
System.out.println("\n" + "Here is your rectangle:");
drawRectangle(height, width);
System.out.print("\n\n");
System.out.print("Enter the height and width of a rectangle: ");
height = kbd.nextInt();
width = kbd.nextInt();
kbd.nextLine();
}
}
private static void drawRectangle(int height, int width) {
for (int line = 1; line <= height; line++) {
for (int star = 1; star <= width; star++) {
System.out.print("*");
}
System.out.println();
}
}
我无法完成这个程序,我们应该编写一个程序来使用“*”绘制一个三角形,它要求高度和宽度,我们需要让它进入一个循环它一直要求更多的高度和宽度,直到用户在高度中输入负值,它应该存储在那里。这是我目前所拥有的。
import java.util.Scanner;
public class 矩形 {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
System.out.print("\n\n"
+ "Drawing Rectangles\n"
+ "------------------\n\n");
while(height >= 0) {
int height = -1, width = -1;
System.out.print("Enter the height and width of a rectangle: ");
height = kbd.nextInt();
width = kbd.nextInt();
kbd.nextLine();
System.out.println("\n"
+ "Here is your rectangle:");
drawRectangle(height, width);
System.out.print("\n\n");
}
}
private static void drawRectangle(int height, int width) {
for (int line = 1; line <= height; line++) {
for (int star = 1; star <= width; star++) {
System.out.print("*");
}
System.out.println();
}
}
}
到目前为止我有这个,它开始,要求高度,宽度然后绘制一个三角形就是这样。我有什么想法可以让它进入一个循环以要求更多并且仅当用户在高度中输入负值时才停止?不允许使用 IF 语句。提前致谢
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
System.out.print("\n\n"
+ "Drawing Rectangles\n"
+ "------------------\n\n");
int height = -1, width = -1;
System.out.print("Enter the height and width of a rectangle: ");
height = kbd.nextInt();
width = kbd.nextInt();
kbd.nextLine();
while(height > -1) {
System.out.println("\n" + "Here is your rectangle:");
drawRectangle(height, width);
System.out.print("\n\n");
System.out.print("Enter the height and width of a rectangle: ");
height = kbd.nextInt();
width = kbd.nextInt();
kbd.nextLine();
}
}
private static void drawRectangle(int height, int width) {
for (int line = 1; line <= height; line++) {
for (int star = 1; star <= width; star++) {
System.out.print("*");
}
System.out.println();
}
}