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(); } }