King of fighters (Java simple small program) code example

  • 2021-07-10 19:47:03
  • OfStack

I just started to learn Java. After watching the video of Lao 9 Jun and typing the code according to his content, I feel quite fulfilled. After all, I just learned Java.


package helloasd;import java.util.*;
public class hellojava {
		public static void main(String[] args) {
			Scanner input = new Scanner(System.in);
			System.out.print(" Enter a name : ");
			// Users enter their own names 
			String username = input.next();
			String comname = " Ajie ";
			System.out.println(username + "vs" + comname);
			// Initialize both data 
			int hp1 = 100, hp2 = 100;       // Both sides HP  
			int attack1 = 0, attack2 = 0;
			
			// Use loop to simulate the battle process 
			while(hp1 > 0 && hp2 > 0) {
				attack1 = (int)(Math.random() * 1000) % 11 + 5; // Random attack power of both sides ( 10~15 ) 
				attack2 = (int)(Math.random() * 1000) % 11 + 5; 
				
				// Players attack first 
				hp2 -= attack1; // Player attacks, computer drops blood 
				System.out.println(comname + ": " + hp2);
				if(attack1 > 0 && attack1 <=5) {
					System.out.println(" Ajie is hit! ");
				}
				else if(attack1 > 5 && attack1 <=10) {
					System.out.println(" Ajie was heavily attacked! ");
				}
				else {
					System.out.println(" Ajie was killed 1 Hit! ");
				}
					
				// Display computer blood volume 
				
				hp1 -= attack2; // Computer attack, player drops blood 
				System.out.println(username + ": " + hp2);
				// Display player health 
				if(attack1 > 0 && attack1 <=5) {
					System.out.println(username + " Be " + comname + " Lucky attack 1 Down! ");
				}
				else if(attack1 > 5 && attack1 <=10) {
					System.out.println(username + " Encountered a strong attack! ");
				}
				else {
					System.out.println(username + " Be hit hard! ");
				}
				
				System.out.println("\n");
			}
			// Print results 
			System.out.println("\n");
			System.out.println("KO!");
			System.out.println(" Player name \t Blood volume ");
			System.out.println(username + "\t" + hp1);
			System.out.println(comname + "\t" + hp2);
			if(hp1 < 0) {
				System.out.println(" Ajie wins! ");
			}
			else {
				System.out.println(username + " Win! ");
			}
	}
}

Related articles: