java程式怎麼寫
- 綜合知識
- 關注:2.7W次
正好今天不忙,就當回憶回憶歷史吧,呵呵。
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 編寫程式package java06_12;import java.util.Scanner;public class PrintTriangle { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("請輸入一個奇數.."); while (true) { Scanner in = new Scanner(System.in); try { int num = in.nextInt();// 獲得輸入的數字 if(num > 0 && (num % 2) == 1) {//如果輸入的數字大32313133353236313431303231363533e59b9ee7ad9431333262363034於0且是奇數 for(int i = 1; i <= num; i += 2) { printNum(i, num);//按1,3,5。
的順序列印資料 } break;//列印完退出 } else { System.out.println("請輸入一個奇數,謝謝!"); continue;//如果不是大於0的奇數,重新輸入 } } catch (Exception e) { System.out.println("請輸入一個奇數,謝謝!"); continue;// 如果不是數字,重新輸入 } } } public static void printNum(int num, int max) { String str = ""; for(int j = 1; j <= (max - num) / 2; j++) { str += " ";//在數字前面加空格 } for(int i = 1; i <= num; i++) { str += num;//數字 } for(int j = 1; j <= (max - num) / 2; j++) { str += " ";//數字後面的空格 } System.out.println(str); }}寫的只要是奇數都行..如果要1-9的加個判斷就行了哈..。
3. 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"); }}。
4. 這個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
5. java程式編寫public class Calculate{ public static void main(String[] args){ int x = Integer.parseInt(args[0]); if(x<0){System.out.println("y="+(2*x-1));} else if(x==0){System.out.println("y=-1");} else if(x>0){System.out.println("y="+(3*x-1));} }}。
6. 用java編寫程式/** * 學生類 * @author kingcs * */class Student{ // 成員變數學號(學號為最終變數) public final String stuNo = "No10000001"; // 年齡 public int stuAge; // 姓名 public String stuName; // 班級 public String stuClass; // 學校 public School stuSchool; // 構造方法以完成對學生物件的初始化 public Student(){ this.stuAge = 22; this.stuName = "張三"; this.stuClass = "三(1)班"; School sch = new School(); sch.schName = "北京一中"; this.stuSchool = sch; } // 新增成員方法getInfo() public void getInfo(){ System.out.println("學生編號:" + this.stuNo + " 學生姓名:" + this.stuName + " 學生年齡:" + this.stuAge + " 所在班級:" + this.stuClass + " 所在學校:" + this.stuSchool.schName); }}/** * 學校類 * @author kingcs * */class School{ // 學校名稱 public String schName;}/** * 測試類 * @author kingcs * */public class Test_student_school { // main方法 public static void main(String[] args) { // 建立一個學生物件 Student stu = new Student(); // 並驗證學生類中所有方法 stu.getInfo(); }}// 結果學生編號:No10000001 學生姓名:張三 學生年齡:22 所在班級:三(1)班 所在學校:北京一中你要的可能是這樣的,希望能夠幫助你。
7. java 程式編寫 import java.util.Scanner;public class TestOut { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("請輸入一個單詞:"); String InputWord =input.next(); for(int i=0;i
- 文章版權屬於文章作者所有,轉載請註明 https://shqsg.com/zonghezhishi/p53lv2.html