java - 从java课本上画图

标签 java draw figures

我正在研究 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();
        }
    }



}

最佳答案

试试这个:(我手头没有编译器)

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();
}

如果有什么不明白的地方,请留言。大家洗耳恭听。

关于java - 从java课本上画图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32270807/

相关文章:

java - 通过传递URL参数在Webview中打开页面

Java tcp端口与eclipse连接

c# - 使用 SpriteBatch 在 XNA 中绘制矩形

c# - 如何在 WPF Canvas 上绘制网格线?

swift - 删除填充的 NSBezierPath

MATLAB - 动态调整图中 x 轴但不调整 y 轴的大小?

java - 根据数据库中的可用性插入/更新行

java - 配置 Spring Integration 聚合器以组合来自 RabbitMq 扇出交换的响应

Matlab - 关闭图形窗口

matlab - 在 Matlab 中运行脚本时关闭所有图形