怎么写java程序
正好今天不忙,就当回忆回忆历史吧,呵呵。
public class Animal {
private String name;
private int legs;
public Animal(){
this("AAA",4);
}
public Animal(String name, int legs){
this.name = name;
this.legs = legs;
}
public String getName() {
return name;
}
public int getLegs() {
return legs;
}
public void move(){
System.out.println(this.name+" Moving!!");
}
public void move(int n){
if(n<0)
return;
for (int i = 0; i < n; i++) {
System.out.println(this.name+" Moving!!");
}
}
}
public class Fish extends Animal {
public Fish(String name){
super(name,0);
}
public void move() {
System.out.println(getName()+" Swimming!!");
}
}
public class Bird extends Animal {
public Bird(String name){
super(name,2);
}
public void move() {
System.out.println(getName()+" Flying!!");
}
}
public class Zoo {
/**
* @param args
*/
public static void main(String[] args) {
Animal animal = new Animal();
Fish fish = new Fish("shayu");
Bird bird = new Bird("dayan");
}
}
2. 怎么写这个Java程序public class A
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int sum = 0;
for (int i = 0; i < 6; i++)
{
int j = in.nextInt();
if (max < j) max = j;
if (min > j) min = j;
sum += j;
}
double avg = sum / 6.;
System.out.println("max =" + max);
System.out.println("min =" + min);
System.out.println("sum =" + sum);
System.out.println("avg =" + avg);
}
}
3. 这个java程序怎么写public class Book {
private String name;//名称属性
private Integer page; //页数属性
public Book(String name,Integer page){//带参数的构造方法
this.name = name;
this.page = page;
}
//属性的get,set方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
//get,set方法结束
/**
* @param args
*/
public static void main(String[] args) {
Map map = new HashMap(); //定义MAP
for (int i = 0; i
4. 这样的编写JAVA程序怎么写例子1:
import java.io.*;
public class Demo1{
public static void main(String[] args) throws IOException{
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
String str=input.readLine();
int number=Integer.parseInt(str);
for(int i=3;i<number;i++)
{
if(isShu(i))
System.out.println(i);
}
}
private static boolean isShu(int number)
{
boolean flag=true;
for(int i=2;i<number;i++)
{
if(number%i==0)
{
flag=false;
break;
}
}
return flag;
}
}
例子2:
import java.util.*;
public class Demo2
{
public static void main(String[]args)
{
int[] num=new int[10];
for(int i=0;i<10;i++)
{
num[i]=((int)(Math.random()*100));
}
Arrays.sort(num);
System.out.println("最大值:"+num[num.length-1]);
System.out.println("最小值:"+num[0]);
}
}
例子3:
public class Demo3
{
public static void main(String[]args)
{
Scanner input=new Scanner(System.in);
System.out.println("请输入一个整数:");
String temp=input.nextLine();
String result="";
char[] c=temp.toCharArray();
for(int i=0;i<c.length;i++)
{
if(c[i]%2==0)
{
result+=String.valueOf(c[i]);
}
}
System.out.println("最后结果为:"+result);
}
}
5. JAVA程序怎么写正好今天不忙,就当回忆回忆历史吧,呵呵。
public class Animal { private String name; private int legs; public Animal(){ this("AAA",4); } public Animal(String name, int legs){ this.name = name; this.legs = legs; } public String getName() { return name; } public int getLegs() { return legs; } public void move(){ System.out.println(this.name+" Moving!!"); } public void move(int n){ if(n<0) return; for (int i = 0; i < n; i++) { System.out.println(this.name+" Moving!!"); } }}public class Fish extends Animal { public Fish(String name){ super(name,0); }public void move() { System.out.println(getName()+" Swimming!!"); }}public class Bird extends Animal { public Bird(String name){ super(name,2); } public void move() { System.out.println(getName()+" Flying!!"); }}public class Zoo { /** * @param args */ public static void main(String[] args) { Animal animal = new Animal(); Fish fish = new Fish("shayu"); Bird bird = new Bird("dayan"); }}。
6. 写一个简单的java程序import java.awt.Color; import java.awt.Container; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; public class CircleFrame { private Circle[] circles = null; private JFrame frame = null; final int circleNumber = 20; final int maxCircleDiameter = 50; private final Color contactColor = Color.BLACK; private final Color apartColor = Color.BLUE; public CircleFrame() { initFrame(); initCircle(); drawColor(); showCircle(); } private void showCircle() { JPanel panel = new MyPanel(); panel.setBounds(0, 0, 800, 600); Container container = frame.getContentPane(); container.setLayout(null); container.add(panel); } class MyPanel extends JPanel { private static final long serialVersionUID = 1L; public void paint(Graphics graphics) { for (int i = circles.length - 1; i >= 0; i--) { graphics.setColor(circles[i].color); final int diameter = circles[i].diameter; graphics.drawOval(circles[i].x - diameter, circles[i].y - diameter, diameter * 2, diameter * 2); } } } private void drawColor() { int delX, delY, sumDiameter; for (int i = circles.length - 1; i > 0; i--) { for (int j = i - 1; j >= 0; j--) { delX = circles[i].x - circles[j].x; delY = circles[i].y - circles[j].y; sumDiameter = circles[i].diameter + circles[j].diameter; if (delX * delX + delY * delY < sumDiameter * sumDiameter) { circles[i].color = contactColor; circles[j].color = contactColor; } } } } private void initCircle() { circles = new Circle[circleNumber]; int x, y, diameter; for (int i = 0; i < circles.length; i++) { x = (int)(Math.random() * frame.getWidth()); y = (int)(Math.random() * frame.getHeight()); diameter = (int)(Math.random() * maxCircleDiameter + 1); circles[i] = new Circle(x, y, diameter); circles[i].color = apartColor; } } private void initFrame() { frame = new JFrame("Draw Circle"); frame.setSize(800, 600); frame.setResizable(false); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void show() { frame.setVisible(true); } public static void main(String[] args) { CircleFrame tenCircle = new CircleFrame(); tenCircle.show(); } } class Circle { int x, y, diameter; Color color = null; public Circle(int x, int y, int diameter) { this.x = x; this.y = y; this.diameter = diameter; } public String toString() { return "[x=" + x + ", y=" + y + ", diameter=" + diameter + "], Color:" + color; } }。