跪求如何用Array写这个程序
题目要求如下
1,使用二维阵列去储存值
2,让阵列的大小是20*20,(type char arr[20][20]).这个画布的大小是固定的,所有的值都储存在这个画布当中。
3,这个程序可以让用户选择画出的图形形状。
4,画出一个矩形,让用户输入行数和列数以及ASCII 字符和矩形在画布中的位置的位置。
5,确保产生的图形是由用户输入的ASCII字符组成的。("*", "$", Anything on the keyboard!)。
示例1如下
1.Type S to draw a rectangle
2.Type T to draw a Triangle
3.Type D to Display
4.Type Q to Quit
S
Enter Height
3
Enter Width
4
Enter character
$
Enter location on canvas (x and y coordinate)
3
3
1.Type S to draw a rectangle
2.Type T to draw a Triangle
3.Type D to Display
4.Type Q to Quit
D
$$$$
$$$$
$$$$
1.Type S to draw a rectangle
2.Type T to draw a Triangle
3.Type D to Display
4.Type Q to Quit
S
Enter Height
5
Enter Width
5
Enter character
@
Enter location on canvas (x and y coordinate)
6
6
1.Type S to draw a rectangle
2.Type T to draw a Triangle
3.Type D to Display
4.Type Q to Quit
D
$$$$
$$$$
$$$$
@@@@@
@@@@@
@@@@@
@@@@@
@@@@@
-------------------------------------------------------------------------
示例2如下
1.Type S to draw a rectangle
2.Type T to draw a Triangle
3.Type D to Display
4.Type Q to Quit
S
Enter Height
3
Enter Width
3
Enter character
$
Enter location on canvas (x and y coordinate)
2
3
1.Type S to draw a rectangle
2.Type T to draw a Triangle
3.Type D to Display
4.Type Q to Quit
S
Enter Height
5
Enter Width
6
Enter character
#
Enter location on canvas (x and y coordinate)
2
5
1.Type S to draw a rectangle
2.Type T to draw a Triangle
3.Type D to Display
4.Type Q to Quit
D
$$######
$$######
$$######
######
######
1.Type S to draw a rectangle
2.Type T to draw a Triangle
3.Type D to Display
4.Type Q to Quit
S
Enter Height
5
Enter Width
5
Enter character
?
Enter location on canvas (x and y coordinate)
5
2
1.Type S to draw a rectangle
2.Type T to draw a Triangle
3.Type D to Display
4.Type Q to Quit
D
$$######
$$######
$$######
?????####
?????####
?????
?????
?????
恳请各位大神写出完整代码,谢谢了,以上内容是我翻译的,原题在下面的网址,代码只用显示矩形就行了。
原题网址:https://wiki.ittc.ku.edu/ittc/EECS168:Lab9
[解决办法]
所有都写太复杂,写了一个简陋的,没有check,
你自己改改吧。。。
import java.util.Scanner;public class Test { private static char arr[][] = new char[20][20]; public static void main(String[] arg) throws Exception { for (int i = 0; i < 20; i++) { for (int j = 0; j < 20; j++) { arr[i][j] = ' '; } } System.out.println("1.Type S to draw a rectangle"); System.out.println("2.Type T to draw a Triangle"); System.out.println("3.Type D to Display"); System.out.println("4.Type Q to Quit"); Scanner s = new Scanner(System.in); String p = s.next(); if ("S".equals(p.toUpperCase())) { System.out.println("Enter Height"); int h = s.nextInt(); System.out.println("Enter Width"); int w = s.nextInt(); System.out.println("Enter character"); p = s.next(); System.out.println("Enter location on canvas (x and y coordinate)"); int x = s.nextInt(); int y = s.nextInt(); for (int i = x; i < x + w; i++) { for (int j = y; j < y + h; j++) { arr[i][j] = p.toCharArray()[0]; } } } p = s.next(); if ("D".equals(p.toUpperCase())) { for (int i = 0; i < 20; i++) { for (int j = 0; j < 20; j++) { System.out.print(arr[i][j]); } System.out.println(); } } }}
[解决办法]