从java课本中画图
Draw figure from java textbook
我正在研究来自 Reges、Stuart 和 Martin Stepp 的第 2 章自检问题。构建 Java 程序:返璞归真方法。我试图获得下面的输出与我的代码。我正在尝试确定 line
到 !
到 \
到 /
的关系以及计算 for loops
所需的数学。这不是作业,我也不需要答案,方向或指导是我所寻求的。
!!!!!!!!!!!!!!!!!!!!!!
\!!!!!!!!!!!!!!!!!!//
\\!!!!!!!!!!!!!!////
\\\!!!!!!!!!!//////
\\\\!!!!!!////////
\\\\\!!//////////
我现在的代码是:
/**
* Created on 8/28/15.
* Following Reges, Stuart, and Martin Stepp. Building Java Programs: A Back to Basics Approach.
* Chapter 2 Self-Check Problems
*/
public class Ch2_SelfCheckProblems {
public static void main(String[] args) {
question34();
}
public static void question34(){
/**
* Table
* Line 1 ! = 22 \ = 0 / = 0
* Line 2 ! = 18 \ = 2 / = 2
* Line 3 ! = 14 \ = 4 / = 4
* Line 4 ! = 10 \ = 6 / = 6
* Line 5 ! = 6 \ = 8 / = 8
* Line 6 ! = 2 \ = 10 / = 10
*/
for (int line = 1; line <= 6; line++){
for (int i = 1; i <= 22; i++){
// for (int j = 1; j <= (line - 1); j++){
// System.out.print("\");
// }
System.out.print("!");
}
System.out.println();
}
}
}
答案直接在您的代码中写了 "table" 的评论中,您在代码中写了符号的计数。
for循环能够以任何步长增加和减少
for(int i = 22 ; i>2; i=i-4) 例如
希望这不是太多。
试试这个:(我手边没有编译器)
for (int line = 1; line <= 6; line++){
for(int i = 1; i<=line-1; i++) {
System.out.print("\");
}
for (int i = 1; i <= 22 - 4*(line-1); i++){
System.out.print("!");
}
for(int i = 1; i<=line-1; i++) {
System.out.print("//");
}
System.out.println();
}
有什么不懂的可以留言。洗耳恭听。
这完全取决于您希望打印的行数。
如果你有 x
行(这里是 6),那么你可以打印你想要的,如下:
int lines = 6;
for (int i = lines; i > 0; i--) { //start from the top line (6), finish at the lowest line (1)
//print backslashes
for (int back = 0; back < (lines-i)*2; back++) { //lines-i is the difference from the top line. add two extra slashes at each new line
System.out.print("\");
}
//print !s
for (int up = 0; up < (i*4)-2; up++) {
System.out.print("!");
}
//print slashes (as many as the backslashes)
for (int forw = 0; forw < (lines-i)*2; forw++) {
System.out.print("/");
}
System.out.println();
}
如果您总是想要 6 行,那么只需跳过 int lines = 6;
语句并将所有地方的 lines
替换为 6
。
因此,在第一行,您打印 4*x-2
'!'s and 0 '\'s and '/'s。
在第二行少打印 4 个 '!'s and 2 more '\'s and '/'s。
...
在最后一行打印 2 '!'s and (x-1)*2
'\'s and '/'s.
一般来说,当给定 x
行时,您正在寻找的关系如下:
At line i
, counting from 1 (lowest) to x
(top), print:
'\': (x-i)*2 times
'!': (i*4)-2 times
'/': (x-i)*2 times
/**
* Created on 8/28/15.
* Following Reges, Stuart, and Martin Stepp. Building Java Programs: A Back to Basics Approach. 3rd Edition.
* Chapter 2 Self-Check Problems Question 34 & 35
*/
public class Ch2_SelfCheckProblems {
public static final int SIZE = 4;
public static void main(String[] args) {
System.out.println("Practice:");
question34();
System.out.println("Partially correct:");
q34();
System.out.println("Solution, scalable with constant:");
solution34();
}
/**
* Table 6 lines; CONSTANT = 6
* Line 1 ! = 22 \ = 0 / = 0
* Line 2 ! = 18 \ = 2 / = 2
* Line 3 ! = 14 \ = 4 / = 4
* Line 4 ! = 10 \ = 6 / = 6
* Line 5 ! = 6 \ = 8 / = 8
* Line 6 ! = 2 \ = 10 / = 10
*
* Table 4 lines; CONSTANT = 4
* Line 1 ! = 14 \ = 0 / = 0
* Line 2 ! = 10 \ = 2 / = 2
* Line 3 ! = 6 \ = 4 / = 4
* Line 4 ! = 2 \ = 6 / = 6
*/
public static void question34(){
for (int line = 1; line <= 6; line++){
for (int i = 1; i <= (22-2*(line-1)); i++){
// for (int j = 1; j <= (line - 1); j++){
// System.out.print("\");
// }
System.out.print("!");
}
System.out.println();
}
}
public static void q34(){
for (int line = 1; line <= 6; line++){
for(int i = 1; i<=line-1; i++) {
System.out.print("\\");
}
for (int i = 1; i <= 22 -(4*(line-1)); i++){
System.out.print("!");
}
for(int i = 1; i<=line-1; i++) {
System.out.print("//");
}
System.out.println();
}
}
public static void solution34(){
for (int line = 1; line <= SIZE; line++){
for(int i = 1; i<=((2 * line) - 2); i++){
System.out.print("\");
}
for (int i = 1; i <= ( -4 * line + ( 4 * SIZE + 2 ) ); i++){
System.out.print("!");
}
for(int i = 1; i<= ((2 * line) - 2); i++){
System.out.print("/");
}
System.out.println();
}
}
}
我正在研究来自 Reges、Stuart 和 Martin Stepp 的第 2 章自检问题。构建 Java 程序:返璞归真方法。我试图获得下面的输出与我的代码。我正在尝试确定 line
到 !
到 \
到 /
的关系以及计算 for loops
所需的数学。这不是作业,我也不需要答案,方向或指导是我所寻求的。
!!!!!!!!!!!!!!!!!!!!!!
\!!!!!!!!!!!!!!!!!!//
\\!!!!!!!!!!!!!!////
\\\!!!!!!!!!!//////
\\\\!!!!!!////////
\\\\\!!//////////
我现在的代码是:
/**
* Created on 8/28/15.
* Following Reges, Stuart, and Martin Stepp. Building Java Programs: A Back to Basics Approach.
* Chapter 2 Self-Check Problems
*/
public class Ch2_SelfCheckProblems {
public static void main(String[] args) {
question34();
}
public static void question34(){
/**
* Table
* Line 1 ! = 22 \ = 0 / = 0
* Line 2 ! = 18 \ = 2 / = 2
* Line 3 ! = 14 \ = 4 / = 4
* Line 4 ! = 10 \ = 6 / = 6
* Line 5 ! = 6 \ = 8 / = 8
* Line 6 ! = 2 \ = 10 / = 10
*/
for (int line = 1; line <= 6; line++){
for (int i = 1; i <= 22; i++){
// for (int j = 1; j <= (line - 1); j++){
// System.out.print("\");
// }
System.out.print("!");
}
System.out.println();
}
}
}
答案直接在您的代码中写了 "table" 的评论中,您在代码中写了符号的计数。 for循环能够以任何步长增加和减少
for(int i = 22 ; i>2; i=i-4) 例如
希望这不是太多。
试试这个:(我手边没有编译器)
for (int line = 1; line <= 6; line++){
for(int i = 1; i<=line-1; i++) {
System.out.print("\");
}
for (int i = 1; i <= 22 - 4*(line-1); i++){
System.out.print("!");
}
for(int i = 1; i<=line-1; i++) {
System.out.print("//");
}
System.out.println();
}
有什么不懂的可以留言。洗耳恭听。
这完全取决于您希望打印的行数。
如果你有 x
行(这里是 6),那么你可以打印你想要的,如下:
int lines = 6;
for (int i = lines; i > 0; i--) { //start from the top line (6), finish at the lowest line (1)
//print backslashes
for (int back = 0; back < (lines-i)*2; back++) { //lines-i is the difference from the top line. add two extra slashes at each new line
System.out.print("\");
}
//print !s
for (int up = 0; up < (i*4)-2; up++) {
System.out.print("!");
}
//print slashes (as many as the backslashes)
for (int forw = 0; forw < (lines-i)*2; forw++) {
System.out.print("/");
}
System.out.println();
}
如果您总是想要 6 行,那么只需跳过 int lines = 6;
语句并将所有地方的 lines
替换为 6
。
因此,在第一行,您打印 4*x-2
'!'s and 0 '\'s and '/'s。
在第二行少打印 4 个 '!'s and 2 more '\'s and '/'s。
...
在最后一行打印 2 '!'s and (x-1)*2
'\'s and '/'s.
一般来说,当给定 x
行时,您正在寻找的关系如下:
At line
i
, counting from 1 (lowest) tox
(top), print:
'\': (x-i)*2 times
'!': (i*4)-2 times
'/': (x-i)*2 times
/**
* Created on 8/28/15.
* Following Reges, Stuart, and Martin Stepp. Building Java Programs: A Back to Basics Approach. 3rd Edition.
* Chapter 2 Self-Check Problems Question 34 & 35
*/
public class Ch2_SelfCheckProblems {
public static final int SIZE = 4;
public static void main(String[] args) {
System.out.println("Practice:");
question34();
System.out.println("Partially correct:");
q34();
System.out.println("Solution, scalable with constant:");
solution34();
}
/**
* Table 6 lines; CONSTANT = 6
* Line 1 ! = 22 \ = 0 / = 0
* Line 2 ! = 18 \ = 2 / = 2
* Line 3 ! = 14 \ = 4 / = 4
* Line 4 ! = 10 \ = 6 / = 6
* Line 5 ! = 6 \ = 8 / = 8
* Line 6 ! = 2 \ = 10 / = 10
*
* Table 4 lines; CONSTANT = 4
* Line 1 ! = 14 \ = 0 / = 0
* Line 2 ! = 10 \ = 2 / = 2
* Line 3 ! = 6 \ = 4 / = 4
* Line 4 ! = 2 \ = 6 / = 6
*/
public static void question34(){
for (int line = 1; line <= 6; line++){
for (int i = 1; i <= (22-2*(line-1)); i++){
// for (int j = 1; j <= (line - 1); j++){
// System.out.print("\");
// }
System.out.print("!");
}
System.out.println();
}
}
public static void q34(){
for (int line = 1; line <= 6; line++){
for(int i = 1; i<=line-1; i++) {
System.out.print("\\");
}
for (int i = 1; i <= 22 -(4*(line-1)); i++){
System.out.print("!");
}
for(int i = 1; i<=line-1; i++) {
System.out.print("//");
}
System.out.println();
}
}
public static void solution34(){
for (int line = 1; line <= SIZE; line++){
for(int i = 1; i<=((2 * line) - 2); i++){
System.out.print("\");
}
for (int i = 1; i <= ( -4 * line + ( 4 * SIZE + 2 ) ); i++){
System.out.print("!");
}
for(int i = 1; i<= ((2 * line) - 2); i++){
System.out.print("/");
}
System.out.println();
}
}
}