A. java 畫星星
public class A {
public void drow(int n){//n表示高度(列印的行數)
for(int i=n;i>0;i--){
for(int j=i-1;j>0;j--){//先列印i-1個空格
System.out.print(" ") ;
}
System.out.print("*") ;//列印第一個*
for(int j=2*(n-i)-1;j>0;j--){//中間部分的空格
System.out.print(" ") ;
}
if(i!=n&&i!=1){//除了首尾兩行外每行中間的*
System.out.print("*") ;
}
for(int j=2*(i-1)-1;j>0;j--){//右面部分的空格
System.out.print(" ") ;
}
System.out.println("*") ;//最後一個*
}
}
public static void main(String[] args){
A a = new A() ;
a.drow(3) ;//這里列印一個三層的
}
}
B. java使用循環與字元串列印組合星星
這類問題要注意兩點:
1.要輸出多少行
2.每行要輸出的信息與行號的關系
你可以用如下兩例方式思考你的問題
例子一
*
**
***
//外層循環控制行數
for(int i = 1; i <= 3;i++)
{
//輸出每行與i的關系為正比,既是i個*號
for(int j = 1;j<=i;j++)
System.out.print("*");
//行結束
System.out.println();
}
例子二、
*###
**##
***#
//外層循環控制行數
for(int i = 1; i <= 3;i++)
{
int j = 1;
//輸出每行與i的關系為正比,既是i個*號
for(;j<=i;j++)
System.out.print("*");
//數出4-i個#
for(;j<=4;j++)
System.out.print("#");
//行結束
System.out.println();
}
以上例子沒有調試,不過大致思路就是這樣,希望對你有所幫助
知其然,必知其所以然
C. 實現星星閃動的java代碼
package panel;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JPanel;
import main.MainTank;
public class TipPanel extends JPanel implements Runnable{
/**
*
*/
private static final long serialVersionUID = 1L;
//偶數列印,畫面板
int time=0;
public void paintComponent(Graphics g){
super.paint(g);
g.fillRect(0, 0, MainTank.getWidthOfGame(), MainTank.getHeightOfGame());//繪制提示窗口
if (time%2==0){//偶數列印,畫面板,造成閃爍效果
g.setColor(Color.ORANGE);
Font font=new Font("華文楷體",Font.BOLD,30);
g.setFont(font);//選用字體
g.drawString("Ready", 140, 130);
}
}
@Override
public void run() {
while (true){
try{
Thread.sleep(250);
}catch (Exception e){
e.getMessage();
}
time++;//繪圖開關
this.repaint();
}
}
}//TipPanel
類似的,修改下就行
D. 如何用JAVA輸出如下的星星
按照你的要求編寫的Java程序如下:
importjava.util.Scanner;
publicclassTest8{
publicstaticvoidmain(String[]args){
Scannersc=newScanner(System.in);
System.out.println("請輸入一個正奇數:");
finalintN=sc.nextInt();
for(intm=1;m<=2*N;m+=2){
for(inti=1;i<=2*N;i+=2){
for(intk=1;k<=Math.abs(N-m);k+=2){
for(intj=1;j<=Math.abs(N-i);j+=2){
System.out.print("");
}
for(intj=1;j<=N-Math.abs(i-N);j++){
System.out.print("");
}
for(intj=1;j<=Math.abs(N-i);j+=2){
System.out.print("");
}
}
for(intk=1;k<=N-Math.abs(m-N);k++){
for(intj=1;j<=Math.abs(N-i);j+=2){
System.out.print("");
}
for(intj=1;j<=N-Math.abs(i-N);j++){
System.out.print("*");
}
for(intj=1;j<=Math.abs(N-i);j+=2){
System.out.print("");
}
}
System.out.println();
}
}
}
}
運行結果:
請輸入一個正奇數:
3
*
***
*
* * *
*********
* * *
*
***
*
E. 用Java 做一個星星圖案
這余氏段代碼你參考一下。可以運行的package common;public class test {
public static void main(String[] args){
Pentagram pen = new Pentagram(10,0,'','★');
Draw.printCanvas(pen.getPentagram());
}
}// Pentagram.java 五角星類
class Pentagram {
private final char FILL_CHAR; // 填充字元
private final char SPACE_CHAR; // 空檔字元
private final int R; // 五角星的外接圓半徑
private final float ROTATION; // 五角星逆時針旋轉角度
private final int X; // 用於生成畫圖數組
private final int Y; // 用於生成畫圖數組 /**
* 構造一個Pentagram對象
*
* @param radius
* 五角星的半徑
* @param rotation
* 五角星的逆時針旋轉度數
* @param spaceChar
* 畫布上空白處填充字元
* @param fillChar
* 畫布上線條部分填充字元
*/
public Pentagram(int radius, float rotation, char spaceChar, char fillChar) {
this.R = radius;
this.ROTATION = rotation;
this.FILL_CHAR = fillChar;
this.SPACE_CHAR = spaceChar;
this.X = 2 * R + 1;
this.Y = 2 * R + 1;
} public char[][] getPentagram() {
char[][] canvas = initCanvas();
Draw draw = new Draw(FILL_CHAR);
//余悄 設五角星的最右邊的一個點為 A,逆時針選取點 B~E
// 通過圓的極坐標公式可以得出:
// 得出以下各點的坐標
// A 點坐標(0.951R, 0.309R)
// B 點坐標(0, R)
// C 點坐標(-0.951R, 0.309R)
// D 點坐標(-0.588R, -0.809R)
// E 點坐標(0.588R, -0.809R)
// 畫線段CA
draw.drawLine(mcos(162) * R, msin(162) * R, mcos(18) * R, msin(18) * R,
canvas);
// 畫線段DA
draw.drawLine(mcos(234) * R, msin(234) * R, mcos(18) * R, msin(18) * R,
canvas);
// 畫線段CE
draw.drawLine(mcos(162) * R, msin(162) * R, mcos(306) * R, msin(306)
* R, canvas);
// 畫線段DB
draw.drawLine(mcos(234) * R, msin(234) * R, mcos(90) * R, msin(90) * R,
canvas);
// 畫線段BE
draw.drawLine(mcos(90) * R, msin(90) * R, mcos(306) * R, msin(306) * R,
canvas);
return canvas;
} // 在方形的字元數組中指定兩點畫線條
// 對圖豎毀渣形數組進行初始化,填充空格
private char[][] initCanvas() {
char[][] canvas = new char[Y][X];
for (int i = 0; i < Y; i++) {
for (int j = 0; j < X; j++) {
canvas[i][j] = SPACE_CHAR;
}
}
return canvas;
} // 根據角度求正弦值,保留兩位小數
private double msin(float a) {
return ((int) (Math.sin(Math.toRadians(a + ROTATION)) * 100)) / 100.0;
} // 根據角度求餘弦值,保留兩位小數
private double mcos(float a) {
return ((int) (Math.cos(Math.toRadians(a + ROTATION)) * 100)) / 100.0;
}
}// Draw.java 畫圖工具類
class Draw {
private char fillChar; public Draw(char fillChar) {
this.fillChar = fillChar;
} /**
* 根據兩個點畫線在二維字元數組上畫線
*
* @param x1
* @param y1
* @param x2
* @param y2
* @param canvas
*/
public void drawLine(double x1, double y1, double x2, double y2,
char[][] canvas) {
int radius = (canvas.length - 1) / 2;
// 從 x 方向進行填充
if (x1 > x2) {
double t = x1;
x1 = x2;
x2 = t;
t = y1;
y1 = y2;
y2 = t;
}
// 獲得直線方程的兩個系數
double a = (y1 - y2) / (x1 - x2);
double b = y1 - a * x1;
// 根據 x 方向的值求出 y 值,並填充圖形
for (int i = (int) Math.round(x1); i <= (int) Math.round(x2); i++) {
// 根據直線方程 y = ax + b,求 y
int y = (int) Math.round(a * i + b);
// 因為 y 和 i 算出來的結果有可能是負數,
// 為了採用數組來表示坐標,做了以下變換
// c[R][R] 即為坐標原點
// c[R][0..R] 為 x 方向的負半軸
// c[R][R+1..2*R] 為 x 方向的正半軸
// c[0..R][R] 為 y 方向的正半軸
// c[R+1..2*R][R] 為 y 方向的負半軸
int yy = radius - y;
int xx = radius + i;
yy = yy < 0 ? 0 : yy;
yy = yy > 2 * radius ? 2 * radius : yy;
xx = xx < 0 ? 0 : xx;
xx = xx > 2 * radius ? 2 * radius : xx;
canvas[yy][xx] = fillChar;
}
// 從 y 方向進行填充,便於減少間距問題產生的字元空檔
if (y1 > y2) {
double t = x1;
x1 = x2;
x2 = t;
t = y1;
y1 = y2;
y2 = t;
}
// 根據 y 方向的值求出 x 值,並填充圖形
for (int i = (int) Math.round(y1); i <= (int) Math.round(y2); i++) {
// 根據 x = (y - b) / a,求 x
int y = (int) Math.round((i - b) / a);
int yy = radius - i;
int xx = radius + y;
yy = yy < 0 ? 0 : yy;
yy = yy > 2 * radius ? 2 * radius : yy;
xx = xx < 0 ? 0 : xx;
xx = xx > 2 * radius ? 2 * radius : xx;
canvas[yy][xx] = fillChar;
}
} /**
* 將畫完圖之後的畫布輸出到控制台上
*
* @param canvas
*/
public static void printCanvas(char[][] canvas){
for (int i = 0; i < canvas.length; i++) {
for (int j = 0; j < canvas[i].length; j++) {
System.out.print(canvas[i][j]);
}
System.out.println();
}
}
}
F. 在java中編一個可以輸出星號金字形狀的程序,掛上詳解!重謝
用星號來列印金字塔,有一定的規律:
1、中心對齊;
2、從上往下,每一層的星星個數都是奇數,而且每一層星星數量可以用以下公式來計算:2*n-1;
3、每一層前面都會出現空位,前面的空位數為當前層數減1,即滿足公式:n-1
根據上述規律,我們得出如下演算法:
publicvoidtestKing(){
//定義金字塔層數
intn=7;
for(inti=1;i<=n;i++){
//第一層循環,列印出對應的層數
for(intk=1;k<=n-i;k++){
//本層循環,列印出當前層的空位
//空位數為當前層數減1,即滿足公式:n-1
System.out.print("");
}
for(intj=1;j<=2*i-1;j++){
//本層循環,列印出當前層的星星個數
//每一層的星星個數都是奇數,且數量可以用以下公式來計算:2*n-1
System.out.print("*");
}
//列印出一個換行
System.out.println();
}
}
結果示例:
*
***
*****
*******
*********
***********
*************