Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: System prints final statistics many times than needed

  1. #1
    Junior Member
    Join Date
    May 2014
    Location
    Romania
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default System prints final statistics many times than needed

    Since this is my first forum I'd like to say Hi! to all of you, it's good to find people with the same passion as me. I started learning Java like 2 weeks ago and it took me something until I learned about methods and stuff (I'm programming with Pascal at school and we don't use things like this). After I learned how to use them I said "oh cool, let's make a simple game". I made a heads or tails game but I'm getting a bug when the user says he doesn't want to play anymore. The statistics are printed as many times as the games played. I don't even know where to start fixing the bug, can you help me please?

    If you want to test the code, write" cap" " pajura" and when the program says "Vrei sa mai joci?" that means "do you wanna play another one?" and you can answer with "da"(yes) or "nu"(nu)

    Here is the code:

    import java.util.Scanner;
     
    public class cap_sau_pajura{
     
    	private static int user;
    	private static int pc;
    	private static String converted;
    	static int usermove;
    	private static int castiguri = 0;
    	private static int pierderi = 0;
     
     
    	static Scanner scan = new Scanner(System.in);
     
     
    	public static String convertor(int n){
    		if(n==0){
    			converted = "cap";
    		}
    		if(n==1){
    			converted = "pajura";
    		}
    		return converted;
    }
     
     
    	public static int pcMove(){
    		 int index = (Math.random()<0.5)?0:1;
    		 return index;
    }
     
     
    	public static int getMove(){
    		System.out.print("Cap sau Pajura?");
    		String userInput = scan.nextLine();
    		userInput = userInput.toUpperCase();
    		char firstLetter = userInput.charAt(0);
    		if(firstLetter == 'C' || firstLetter == 'P'){
    		switch(firstLetter){
    		case 'C':
    			return usermove=0;
    		case 'P':
    			return usermove=1;
    		}
    	}
     
    		return getMove();
    }
     
    	public void scor(){
     
    		int jocurijucate = castiguri+pierderi;
    		double procent = castiguri * 100/jocurijucate;
    		System.out.println("|| Jocuri Jucate: " + jocurijucate +" ||");
    		System.out.println("|| Jocuri Castigate: " + castiguri + " || ");
    		System.out.println("|| Jocuri Pierdute: "+ pierderi + " ||");
    		System.out.println("|| Procent castiguri: "+ procent + "% ||");	
    }
     
    	public void tryAgain(){
    		System.out.println();
    		System.out.print("Vrei sa joci din nou?");
    		String tryagain = scan.nextLine();
    		System.out.println();
    		tryagain = tryagain.toUpperCase();
    		char tryag = tryagain.charAt(0);
    		if(tryag == 'D'|| tryag == 'N'){
    			switch(tryag){
    			case 'D':
    				startGame();
    			case 'N':
    				scor();
    		}
    	}
    }
     
    	public void startGame(){
     
    		user = cap_sau_pajura.getMove();
    		pc = cap_sau_pajura.pcMove();
     
    		System.out.println();
    		System.out.println("Ai ales " + convertor(user));
    		System.out.println("S-a dat cu banul si a picat " + convertor(pc));
     
    		if(pc == user){
    			System.out.println("Ai castigat!");
    			castiguri++;
    		}
    		else{
    			System.out.println("Ai pierdut!");
    			pierderi++;
    		}
    		tryAgain();
     
    	}
     
    	public static void main(String args[]){
    		cap_sau_pajura game = new cap_sau_pajura();
    		game.startGame();
    	}
    }


  2. #2
    Junior Member
    Join Date
    May 2014
    Location
    Romania
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: System prints final statistics many times than needed

    I've figured out the solution.
    public void tryAgain(){
    		System.out.println();
    		System.out.print("Vrei sa joci din nou?");
    		String tryagain = scan.nextLine();
    		System.out.println();
    		tryagain = tryagain.toUpperCase();
    		char tryag = tryagain.charAt(0);
    		if(tryag == 'D'|| tryag == 'N'){
    			switch(tryag){
    			case 'D':
    				startGame();
    			case 'N':
    				scor();
    		}
    	}
    }

    is

    public void tryAgain(){
    		System.out.println();
    		System.out.print("Vrei sa joci din nou?");
    		String tryagain = scan.nextLine();
    		System.out.println();
    		tryagain = tryagain.toUpperCase();
    		char tryag = tryagain.charAt(0);
    		if(tryag == 'D'|| tryag == 'N'){
    			switch(tryag){
    			case 'D':
    				startGame();
                                    break;
    			case 'N':
    				scor();
                                   break;
    		}
    	}
    }

Similar Threads

  1. system.out prints twice in a small program
    By vinaysagar in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 12th, 2014, 07:36 AM
  2. Replies: 10
    Last Post: November 8th, 2012, 05:29 AM
  3. Replies: 2
    Last Post: November 7th, 2012, 09:45 PM
  4. [SOLVED] How do I get the one part of the needed output for quarterly sales-statistics?
    By SOG in forum What's Wrong With My Code?
    Replies: 32
    Last Post: June 28th, 2012, 10:04 AM
  5. Java Final Exam - Help needed
    By JavaBeginner12 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 16th, 2012, 07:54 PM