What's new
  • Welcome to PHCorner Forums. Take a moment to Sign up and gain unlimited access and extra privileges that guests are not entitled to, such as: All that and more! Registration is quick, simple and absolutely free. Join our community today!

Java GUYS BADLY NEEDED HELP.

cjpostrano24

Enthusiast
Joined
Mar 18, 2015
Messages
114
Reaction score
35
Age
31
Pwedi po makahingi ng tulong about sa Java Programming. any program lg po na na gumagamit lg simple codes (array, for loops and methods only).

Maraming Thankyou sa makakatulong.
 

hase0278

Forum Veteran
, Last edited:
JackEnPoy na console. array lang gagamitin mo para sa rock, paper scissor na sagot ng computer + java.util.Random + java.until.Scanner para sa user input.

Ito sample:
Java:
import java.util.Scanner;
import java.util.Random;

public class JackEnPoyConsole {

    public static void main (String[] args) {
        String result = getHumanWeapon() + " vs " + getAIWeapon();
        
        if(result.equals("paper vs rock") || result.equals("scissor vs paper") || result.equals("rock vs scissor")){
            System.out.println ("You Win!");
        }else if(result.equals("paper vs paper") || result.equals("scissor vs scissor") || result.equals("rock vs rock")){
            System.out.println("Draw!");
        }else if(result.equals("rock vs paper") || result.equals("paper vs scissor") || result.equals("scissor vs rock")){
            System.out.println ("You lose!");
        }else{
            System.out.println ("Error on input has occured!");
        }

    }
    
    private static String getAIWeapon(){
        String[] aiAns = {"Rock", "Paper", "Scissor"};
        
        Random getAnsIndex = new Random();
        
        return aiAns[getAnsIndex.nextInt(3)].toLowerCase();
    }
    
    private static String getHumanWeapon(){
        Scanner getUserAns = new Scanner(System.in);
        String result;
        
        System.out.println ("Choose Your Weapon:");
        
        while(true){
            result = getUserAns.next();
            
            if(result.toLowerCase().equals("rock") || result.toLowerCase().equals("paper") || result.toLowerCase().equals("scissor")){
                break;
            }else{
                System.out.println ("Invalid input! Choose your weapon again!");
            }
        }
        return result.toLowerCase();
    }
}
 

Online statistics

Members online
2,524
Guests online
2,551
Total visitors
5,075

Forum statistics

Threads
850,947
Messages
15,076,272
Members
1,372,321
Top