蝴蝶文形成
****** ******
***** *****
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
***** *****
****** ******
cmd里显示这种形式的蝴蝶文怎么写啊?显示的时候是用无限循环显示“*”增加,等摁回车的时候停止继续扩展
小弟初学java 请高手多多指点。先谢谢各位了。
[解决办法]
ButterFlyStar.java
/**
*
* @author zdjray
*/
public class ButterFlyStar {
public void print() throws InterruptedException {
boolean flag = true;
int count = 6;
while (true) {
for (int i = 0; i < 6 - count; i++) {
System.out.print( " ");
}
for (int i = 0; i < count; i++) {
System.out.print( "* ");
}
System.out.print( "\t ");
for (int i = 0; i < count; i++) {
System.out.print( "* ");
}
System.out.println();
count += flag ? (-1) : 1;
if (count > = 6) {
flag = true;
}
if (count <= 0) {
flag = false;
}
Thread.sleep(300);
}
}
}
Main.java
/**
*
* @author zdjray
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws InterruptedException {
// TODO code application logic here
ButterFlyStar star = new ButterFlyStar();
star.print();
}
}
命令行运行时,要停止直接ctrl+c就可以了
至于回车的部分自己实现吧:)
[解决办法]
试试这个是不是你要得,在eclipse下运行时,把光标放到控制台上,然后点回车就可以了.
在CMD上没试,你试试吧.
import java.io.IOException;
public class Butterfly extends Thread{
public static int marker = 0;//停止标记
private int count = 1;//记录*数
public void run() {
System.out.print( '* ');
while(true){
if(marker == 10){
this.stop();
}
count++;
System.out.print( '* ');
try {
Thread.sleep(200);
} catch (InterruptedException e) {
System.out.println( "//Thread.sleep(200);抛出异常! ");
e.printStackTrace();
}
}
}
//输出一串 "* "
public void printStarLine(int count){
for(int i = 0 ;i < count ; i++)
System.out.print( '* ');
}
//控制输出
public void printButterfly(){
if(count/2 != 0){
count++;
}
for(int i = 0 ;i < count/2 + 1 ;i++){
printStarLine(count - i*2);
System.out.print( ' ');
printStarLine(count - i*2);
System.out.println();
}
System.out.println();
for(int i = count/2 + 1 ;i > = 0 ;i--){
printStarLine(count - i*2);
System.out.print( ' ');
printStarLine(count - i*2);
System.out.println();
}
}
public static void main(String[] args) {
Butterfly bf = new Butterfly();
bf.start();
try {
marker = (int)System.in.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bf.printButterfly();
}
}