類與對象實驗報告

1、編寫一個名爲“複數”的類,包含複數的實部和虛部(數據成員),以及複數之間的基本算術運算:加、減(方法成員),並要求複數加減運算,程序運行中能對給定的複數進行運算,並打印運算的結果。
1)面向過程

import java.util.*;

//實現複數的雙目運算
//一個名爲“複數”的類,包含複數的實部和虛部(數據成員),以及複數之間的基本算術運算:加、減(方法成員),並要求複數加減運算,程序運行中能對給定的複數進行運算,並打印運算的結果。
//面向過程編程
public class complex1{
	static int r1,r2; //定義實數部分
	static int v1,v2;//定義虛數部分
	static int sr,sv;//定義運算後的實部和虛部
	static void add() {
		//定義複數運算加法方法
		sr=r1+r2;
		sv=v1+v2;
	}
	
	static void sub() {
		//定義複數運算減法方法
		sr=r1-r2;
		sv=v1-v2;
	}
	 static void output(int sr,int sv) {
		 //定義輸出方法
		System.out.printf("The result is %d+%di",sr,sv);
		
	}
	 public static void main(String args[]) {
		 Scanner reader=new Scanner(System.in);
		 System.out.println("please input the real of first complex:");
		 r1=reader.nextInt();
		 System.out.println("please input the image of first complex:");
		 v1=reader.nextInt();
		 System.out.println("please input the real of second complex:");
		 r2=reader.nextInt();
		 System.out.println("please input the image of second complex:");
		 v2=reader.nextInt();
		 int i; //定義運算方向變量
		 System.out.printf("Which operation do you want to perform\n1.add\t2.sub\n");
		 do {
			 i=reader.nextInt();
			 if(i==1)
				 add();
			 else if (i==2) 
				 sub();
			 else
				 System.out.println("Choose the wrong, please choose again.");
			 
		 }while(i!=1&&i!=2);
		 output(sr,sv);
		 reader.close(); //關閉數據流
		
	 }
}

在這裏插入圖片描述
2)面向對象

import java.util.Scanner;
//一個名爲“複數”的類,包含複數的實部和虛部(數據成員),以及複數之間的基本算術運算:加、減(方法成員),並要求複數加減運算,程序運行中能對給定的複數進行運算,並打印運算的結果。
//面向對象編程
public class Complex {
    double real;//實部和虛部
    double image;
    Complex(){  //編寫無參構造方法
        this.real=0.0;
        this.image=0.0;
    }
    Complex(double real,double image){ //編寫有參構造方法,給實部和虛部賦值
        this.real=real;
        this.image=image;
    }
    void add(Complex c1, Complex c2){ //編寫加法參數爲對象類型
        this.real=c1.real+c2.real;
        this.image=c1.image+c2.image;
        System.out.println(this.real+"+"+this.image+"i");
    }
    public void sub(Complex c1,Complex c2){
        this.real=c1.real-c2.real;
        this.image=c1.image-c2.image;
        System.out.println(this.real+"+"+this.image+"i");
    }

    public static void main(String[] args){
        System.out.println("請輸入第一個實部和虛部:");
        Scanner a=new Scanner(System.in);
        Complex c1=new Complex(a.nextDouble(),a.nextDouble());//創建對象並給第一個複數賦值
        System.out.println("請第二個輸入實部和虛部:");
        Complex c2=new Complex(a.nextDouble(),a.nextDouble());
        Complex c3=new Complex();
        int i; //定義運算方向變量
		System.out.printf("Which operation do you want to perform\n1.add\t2.sub\n");
		do {
			 i=a.nextInt();
			 if(i==1) {
				 System.out.print("兩複數之和爲:");
			        c3.add(c1,c2);// 也可以c1或c2.add(c1,c2)一樣的 這裏主要是學習一下這個方法  下同
			 }
				
			 else if (i==2) {
				 System.out.print("兩複數之差爲:");
			        c3.sub(c1,c2);
			 }
			 else
				 System.out.println("Choose the wrong, please choose again.");
			 
		 }while(i!=1&&i!=2);
       a.close();
       //c.close();
    }
}

在這裏插入圖片描述
2、用類來描述遊戲角色的交手次數、經驗值、生命值之間的關係,並斷定角色決鬥的勝負。
1)、兩遊戲角色決鬥。角色1交手次數+1,生命值-1,經驗值+2;角色2交手次數+1,生命值-2,經驗值+3。經驗值每增加50時,生命值+1;生命值<0判爲負。生命值初始爲1000,經驗值初始爲0。
2)、給定二個不同的角色,判定交手的勝負關係。
3)、實驗報告給出決鬥結果和交手次數
4)、實驗報告給出所有源代碼。

//定義玩家類
class player{
	//下列變量爲生命值、經驗值、減生命值、加成經驗值、獎勵生命值、計數
	int HP,EXP,HPC,EXPC,award,count=0;
	//定義戰鬥結果變量
	boolean fail=false;
	//構造方法
	player(int HP,int EXP,int HPC,int EXPC,int award){
		this.HP=HP;
		this.EXP=EXP;
		this.HPC=HPC;
		this.EXPC=EXPC;
		this.award=award;
	}
	//定義戰鬥過程方法
	public boolean fight(){
		HP-=HPC; //生命值損耗
		EXP+=EXPC; //經驗加成
		if(EXP>=50){ //生命值加成
			HP+=award;
			EXP-=50;
		}
		count++;  //記錄戰鬥次數
		if(HP<=0) //判斷戰鬥結果爲負
		fail=true;
		return fail;
	}
}
//定義遊戲主類
public class fightgame{
	public static void main(String[] args){
		player p1=new player(1000,0,1,2,1);//創建玩家一 並初始化數據
		player p2=new player(1000,0,2,3,1);//創建玩家二 並初始化數據
		while(!p1.fight()&&!p2.fight()){
			//戰鬥開始
			p1.fight();
			p2.fight();

		}
		//宣佈結果
		if(p1.fail&&!p2.fail)
			System.out.println("p2勝利,p1失敗,共戰鬥"+p1.count+"次");
		else if(!p1.fail&&p2.fail)
			System.out.println("p1勝利,p2失敗,共戰鬥"+p1.count+"次");
		else 
			System.out.println("平局,共戰鬥"+p1.count+"次");
	}
}


在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章