在java中优化for循环

时间:2017-06-08 05:49:17

标签: java core

我正在尝试使用for循环打印方块。 以下代码可以即兴创作,只使用for循环而不是swing

来打印方块
<html>
<head>
  <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
  <script type="text/javascript">
  google.charts.load('current', {'packages':['corechart']});
  google.charts.setOnLoadCallback(drawChart);

  function drawChart() {
    var data = google.visualization.arrayToDataTable([
     ['Chart Date', 'LNW1', 'TNW1', 'LNW2', 'TNW2'],
     ['Jul-13', 12, 20, 0, 10,],
     ['Aug-13', 100, 200, 50, 10,],
     ['Sep-13', 120, 200, 0, 20,],
     ['Oct-13', 1200, 2000, 50, 50,],       
     ['Nov-13', 1200, 200, 200, 100,],      
     ['Dec-13', 1200, 20, 0, 0,]
     ]);

    var options = {
      title: 'Report TNW & LNW',
      vAxis: {title: 'none'},
      isStacked: false
    };

    var chart = new google.visualization.SteppedAreaChart(document.getElementById('chart_div'));

    chart.draw(data, options);

    var HideLNW1 = document.getElementById("HideLNW1");
    HideLNW1.onclick = function()
    {
    view = new google.visualization.DataView(data);
    view.hideColumns([1]); 
    chart.draw(view, options);
    }

    var HideTNW1 = document.getElementById("HideTNW1");
    HideTNW1.onclick = function()
    {
    view = new google.visualization.DataView(data);
    view.hideColumns([2]); 
    chart.draw(view, options);
    }

    var HideLNW2 = document.getElementById("HideLNW2");
    HideLNW2.onclick = function()
    {
    view = new google.visualization.DataView(data);
    view.hideColumns([3]); 
    chart.draw(view, options);
    }

    var HideTNW2 = document.getElementById("HideTNW2");
    HideTNW2.onclick = function()
    {
    view = new google.visualization.DataView(data);
    view.hideColumns([4]); 
    chart.draw(view, options);
    }


  }
</script>
</head>
<button type="button" id="HideLNW1"  >Hide LNW1</button>
<button type="button" id="HideTNW1"  >Hide TNW1</button>
<button type="button" id="HideLNW2"  >Hide LNW2</button>
<button type="button" id="HideTNW2"  >Hide TNW2</button>
<body>
<div id="chart_div" style="width: 1200px; height: 500px;"></div>
</body>

3 个答案:

答案 0 :(得分:0)

我不确定我当时是否理解,但你是在尝试打印没有摆动的方块?但是你正在使用system.out.print。它有点令人困惑,但如果它用于学习编程中的家庭作业,你可以用for循环制作一个正方形:

!=

但是如果你想制作矩形(因为你提到了swing)你应该检查java.awt.Rectangle2D和Rectangle: https://docs.oracle.com/javase/7/docs/api/java/awt/Rectangle.html

答案 1 :(得分:0)

怎么样:

public class TestProgram {
    public static void main(String[] args) {
        TestProgram test = new TestProgram();
        test.draw(15, 4);
    }

    public void draw(int x, int y) {
        System.out.println(generateLine(x, '.'));
        for (int i = 1; i< y-1; i++) {
            System.out.println("." + generateLine(x-2, ' ') + ".");
        }
        System.out.println(generateLine(x, '.'));
    }

    private String generateLine(int x, char character) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < x; i++) {
            stringBuilder.append(character);
        }
        return stringBuilder.toString();
    }
}

答案 2 :(得分:0)

你可以这样做:

void rectangleDraw(int x, int y) {

    String line1 = new String(new byte[x]).replaceAll("", ".");
    String line2 = "." + new String(new byte[x - 2]).replaceAll("", " ") + ".";

    System.out.println(line1);
    for (int i = x; i > y; i--) {
        System.out.println(line2);
    }

    System.out.println(line1);
}