java正方形源代码 java求正方形的面积

求java 输出正方形(用*号)的代码!public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("请输入正方形的大?。?);
int line = s.nextInt();; // 正方形的边长
for (int i = 1; i = line; i) { // 行
for (int j = 1; j = line; j) { // 列
if (i == 1 || i == line) { // 如果是第一行或者是最后一行则输出星号
System.out.print("* ");
} else if (j == 1 || j == line) { // 如果是第一列或者是最后一列则输出星号
System.out.print("* ");
} else {
System.out.print(""); // 如果不是第一行、最后一行、第一列、最后一列则输出空格
}
}
System.out.print("\n"); // 换行
}
}
写一个java程序 , 使其画一个正方形并改变它的颜色(每秒钟改变一次) 。求完整代码谢谢按照java正方形源代码你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程序第一个要求的第(4),是计算圆形面积还是计算正方形面积java正方形源代码?
我按计算正方形面积给java正方形源代码你做的…… 注意:我默认你的学号为01,你可以自己修改的!
SquareFrame.java完整代码: 注意后面还有Square01.java的代码呢?。。。?
import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class SquareFrame extends Frame implements ActionListener {
public static void main(String[] args) {
SquareFrame sFrame = new SquareFrame();
}
Label lb;
Button bt;
TextField tf;
TextArea ta;
public int sum = 0, number = 0;
SquareFrame() {
【java正方形源代码 java求正方形的面积】lb = new Label("请输入正方形的边长:");
lb.setBounds(20, 50, 120, 20);
tf = new TextField("");
tf.setBounds(150, 50, 80, 20);
bt = new Button("确定");
bt.setBounds(20, 80, 220, 40);
ta=new TextArea();
ta.setBounds(20, 130, 220, 130);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
add(lb);
add(bt);
add(tf);
add(ta);
bt.addActionListener(this);
this.setLayout(null);
this.setSize(300, 300);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == bt) {
try {
number = Integer.parseInt(tf.getText());
} catch (Exception ep) {
ta.setText("对不起,你输入的数据有误\n请输入正整数?。?);
number=0;
}
if(number!=0){
Square01 sq = new Square01();
ta.setText("结果如下:\n默认边长为20的正方形\n周长是:" sq.getLength() "\n面积是:" sq.getArea());
sq.setSide(number);
ta.appendText("\n你输入的边长为" number "的正方形\n周长是:" sq.getLength() "\n面积是:" sq.getArea());
}
}
}
}
#################################################
Square01.java 完整代码:
public class Square01 {
public int side;
public Square01() {
side=20;
}
public int getSide(){
return side;
}
public void setSide(int side){
this.side=side;
}
public int getLength(){
return 4*side;
}
public int getArea(){
return side*side;
}
}
注:有问题的话再问……
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("请输入要打印java正方形源代码的行数:"); String str=in.readLine(); r=Integer.valueOf(str).intValue(); } catch(Exception e) { System.out.println("您输入java正方形源代码的数据有误 。"); return; } for (int i=1;i=5;i) { for(int j=1;j=5;j) { System.out.print("*"); } System.out.println(); } }}
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正方形源代码的介绍就聊到这里吧 , 感谢你花时间阅读本站内容,更多关于java求正方形的面积、java正方形源代码的信息别忘了在本站进行查找喔 。

    推荐阅读