Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Java
import java.util.Scanner;
import java.util.Random;

public class ja9 {
    public static void main(String[] args) {
        // stone paper sizzors GAME

        Scanner s = new Scanner(System.in);
        Random r = new Random();
        int count_user = 0, count_coumputer = 0;
        for (int k = 1; k <= 3; k++) {
            System.out.println("* lets play a game * ");
            System.out.println("1-->stone\n2-->paper\n3-->sizzors");
            int userinput;
            System.out.println("enter the number:-");
            userinput = s.nextInt();
            s.nextLine();
            if (userinput == 1) {
                System.out.println("you have choosen stone");
            } 
            else if (userinput == 2) {
                System.out.println("you have choosen paper");
            } 
            else{
                System.out.println("you have choosen sizzors");
            }

            int coumputerinput = r.nextInt(3);
            if (coumputerinput == 1) {
                System.out.println("coumputer has choosen stone");
            } 
            else if (coumputerinput == 2) {
                System.out.println("coumpter has choosen paper");
            } 
            else {
                System.out.println("coumpuer has choosen sizzor");
            }

            if (userinput <= 3) {
                if (userinput == coumputerinput) {
                    System.out.println("DRAW!!");
                } 
                else if ((userinput == 1 && coumputerinput == 3) || 
                           (userinput == 2 && coumputerinput == 1)|| 
                           (userinput == 3 && coumputerinput == 2)) {
                    System.out.println("YOU WON !! YEAH");
                    count_user++;
                } 
                else {
                    System.out.println("computer wons!!");
                    count_coumputer++;
                }

            } else {
                System.out.println("WRONG INPUT!!");
            }
        }
        System.out.print("\n\n");
        System.out.println("you have won " + count_user + " times");
        System.out.println("computer have won " + count_coumputer + " times");
        s.close();
    }

}


What I have tried:

here the code runs 3 times and gives the score as well ..draw is of zero points and in winning the points count is done here I want to be perfect in all runs ..if you can help me correct by doing the least minimum changes in this java code and send me the correct code. sometimes hereafter when the same choice is chosen by both user and computer the and should be a draw but the answer comes computer wins!! please look at it once and please help me.thanku
Posted
Updated 8-Apr-21 3:39am
v2

Change from
Quote:
int coumputerinput = r.nextInt(3);
to
Java
int coumputerinput = r.nextInt(3) + 1;


because r.nextInt(3) may return either 0, or 1, or 2. See Random (Java Platform SE 8 )[^].
 
Share this answer
 
Comments
vishal kumawat 8-Apr-21 9:38am    
thanks @CPallini now it works perfectly
import java.util.Scanner;
import java.util.Random;

public class ja9 {
    public static void main(String[] args) {
        // stone paper sizzors GAME

        Scanner s = new Scanner(System.in);
        Random r = new Random();
        int count_user = 0, count_coumputer = 0;
        for (int k = 1; k <= 3; k++) {
            System.out.println("*** lets play a game *** ");
            System.out.println("1-->stone\n2-->paper\n3-->sizzors");
            int userinput;
            System.out.println("enter the number:-");
            userinput = s.nextInt();
            s.nextLine();
            if (userinput == 1) {
                System.out.println("you have choosen stone");
            } 
            else if (userinput == 2) {
                System.out.println("you have choosen paper");
            } 
            else{
                System.out.println("you have choosen sizzors");
            }

            int coumputerinput = r.nextInt(3)+1;
            if (coumputerinput == 1) {
                System.out.println("coumputer has choosen stone");
            } 
            else if (coumputerinput == 2) {
                System.out.println("coumpter has choosen paper");
            } 
            else {
                System.out.println("coumpuer has choosen sizzor");
            }

            if (userinput <= 3) {
                if (userinput == coumputerinput) {
                    System.out.println("DRAW!!");
                } 
                else if ((userinput == 1 && coumputerinput == 3) || 
                           (userinput == 2 && coumputerinput == 1)|| 
                           (userinput == 3 && coumputerinput == 2)) {
                    System.out.println("YOU WON !! YEAH");
                    count_user++;
                } 
                else {
                    System.out.println("computer wons!!");
                    count_coumputer++;
                }

            } else {
                System.out.println("WRONG INPUT!!");
            }
        }
        System.out.print("\n\n");
        System.out.println("you have won " + count_user + " times");
        System.out.println("computer have won " + count_coumputer + " times");
        s.close();
    }

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900