正方形java代码怎么写,编程画正方形代码
编写一段代码来计算正方形的面积和周长?Java程序
import java.util.Scanner
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册、雅安服务器托管、营销软件、网站建设、新绛网站维护、网站推广。
public class abc {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("输入边长:");
double a = input.nextDouble();
double area = a*a;
double length=4*a;
System.out.println(area);
System.out.println(length);
java编写一个正方形类是Square,该类拥有:
IMPORT java.io.BufferedReader;IMPORT java.io.InputStreamReader;public class Square { public static void main(String[] args) { int r; try {BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); System.out.print("请输入要打印的行数:"); String str=in.readLine(); r=Integer.valueOf(str).intValue(); } catch(Exception e) { System.out.println("您输入的数据有误。"); return; } for (int i=1;i=5;i++) { for(int j=1;j=5;j++) { System.out.print("*"); } System.out.println(); } }}
用Java写一个函数 输出菱形和正方形 怎么写啊
按照你的要求编写的输出菱形和正方形的程序如下:
public class U {
public static void main(String[] args) {
//设置菱形和正方形大小
final int N=3;
//输出菱形
for (int i = 1; i = 2*N-1; i++) {
for (int j = 1; j = Math.abs(N - i); j++) {
System.out.print(" ");
}
for (int k = 1; k = N-Math.abs(i-N); k++) {
System.out.print("* ");
}
System.out.println();
}
System.out.println();
//输出正方形
for (int i = 1; i = N; i++) {
for (int j = 1; j = N; j++) {
if(j==1 || j==N || i==1 || i==N)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
运行结果:
*
* *
* * *
* *
*
***
* *
***
用java编写一个名为Square(正方形)的类,并按要求完成如下封装
随便写写 未测试
package com.leejiliang.HomeworkDemo2;
public class Test {
public static void main(String[] args) {
Square[] squares = new Square[10];
for (int i = 0, k = squares.length; i k; i++) {
squares[i] = new Square(i + 1);
}
System.out
.println("所有Square的总周长是:" + Square.getTotalPerimeter(squares));
Square sq1 = new Square(20);
Square sq2 = null;
try {
sq2 = (Square) sq1.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
System.out.println("sq1"
+ (sq1.compareTo(sq2) == 1 ? ""
: (sq1.compareTo(sq2) == 0 ? "=" : "")) + "sq2");
}
}
class Square implements Cloneable, ComparableSquare {
private double side;
public Square(double s1) {
this.side = s1;
}
public double getSide() {
return side;
}
public void setSide(double side) {
this.side = side;
}
@Override
public int compareTo(Square o) {
if (this.side o.getSide()) {
return 1;
} else if (this.side o.getSide()) {
return -1;
} else {
return 0;
}
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public double getPerimeter() {
return side * 4;
}
public static double getTotalPerimeter(Square[] squares) {
double totalPerimeter = 0.0;
for (int i = 0, k = squares.length; i k; i++) {
totalPerimeter += squares[i].getPerimeter();
}
return totalPerimeter;
}
}
写一个java程序,使其画一个正方形并改变它的颜色(每秒钟改变一次)。求完整代码谢谢
按照你的要求,编写的Java程序如下
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class F extends JFrame implements Runnable{
Color[] colors = {Color.red,Color.orange,Color.yellow,Color.green,Color.cyan,Color.blue,Color.magenta,Color.black};
int i=0;
F(){
setTitle("变色正方形");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public void paint(Graphics g){
g.setColor(colors[i]);
g.drawRect(100,100,200,200);
}
@Override
public void run() {
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(i7)
i++;
else
i=0;
repaint();
}
}
public static void main(String[] args) {
F f=new F();
Thread t=new Thread(f);
t.start();
}
}
运行结果
java怎么绘制一个正方形?
可使用Graphics 的fillRect绘制正方形,代码如下:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Rect extends JFrame{
Rect(){
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void paint(Graphics g){
g.setColor(Color.blue);
g.drawRect(80, 80, 50, 50);
g.fillRect(150, 150, 50, 50);
}
public static void main(String[] args) {
new Rect();
}
}
分享文章:正方形java代码怎么写,编程画正方形代码
当前链接:http://ybzwz.com/article/heohdp.html