public class fermi
{
	private int[] guess;
	private int[] random;
	private int total;
	private int win;
	private int lose;

	public fermi(int[] gs, int[] rn)
	{
		guess=new int[gs.length];
		for(int i=0;i<gs.length;i++)
		guess[i]=gs[i];

		random=new int[rn.length];
		for(int j=0;j<rn.length;j++)
		guess[j]=gs[j];
	}

	public fermi()
	{
		total=0;
		win=0;
		lose=0;
	}

	public int totalPlay()
	{
		return total+=1;
	}

	public int totalWin()
	{
		return win+=1;
	}

	public int totalLose()
	{
		return lose+=1;
	}
	public String toString()
	{
		String str="Total play is "+total+"\nTotal win is "+win+"\nTotal lose is "+lose;
		return str;
	}



}
import javax.swing.JOptionPane;
import java.util.Random;

public class FermiGame
{
	public static void main(String [] args)
	{
		JOptionPane.showMessageDialog(null,"Hello!!"+"\nWelcome to Fermi ^o^!");
		JOptionPane.showMessageDialog(null,"There is only one simple rule: Guess 3 numbers in between 0-9"
											+"\nand in the correct position to match with the three Secret Numbers"
											+"\nYour aim is to try to get three Fermi."
											+"\n If you get Nano,that's means your guess does not match any of the secret numbers."
											+"\n Pico means your guess is correct,but not in the correct position.");
		JOptionPane.showMessageDialog(null,"Are you ready to play?");

		valid();
	}

	public static void valid()
	{
		String input;
		char ch;
		input=JOptionPane.showInputDialog("Enter any alphabert to start play or 'z' to quit:");
		ch=input.charAt(0);
		while(!Character.isLetter(ch))
		{
			input=JOptionPane.showInputDialog("!!You are only allow to enter alphabert!!"+
												"\nPlease re-enter any alphabert to start play or 'z' to quit:");
			ch=input.charAt(0);
		}

		if(ch=='z')
		{
			quit();
		}
		else
		start();
	}

	public static void start()
	{
		char ch;
		String input;
		final int Num_Guess=3;
		int [] guess=new int [Num_Guess];
		final int Num_Random=3;
		int [] random=new int [Num_Random];
		Random ran=new Random();
		for(int j=0; j<random.length; j++)
		{
			random[j]=ran.nextInt(10);
		}
		for(int i=0; i<guess.length; i++)
		{
			input=JOptionPane.showInputDialog("Enter your guess"+(i+1)+"(0-9):");
			ch=input.charAt(0);
			while(!Character.isDigit(ch))
			{
				input=JOptionPane.showInputDialog("!!This is not an integer!!"+
													"\nPlease re-enter your guess"+(i+1)+"(0-9):");
				ch=input.charAt(0);
			}
				guess[i]=Integer.parseInt(input);

				while(guess[i]<0 || guess[i]>9)
				{
					input=JOptionPane.showInputDialog("!!Invalid number!!"+
														"\nYou can only enter integer in between 0-9."+
														"\nPlease re-enter your guess "+(i+1)+"(0-9):");
					ch=input.charAt(0);
					while(!Character.isDigit(ch))
					{
						input=JOptionPane.showInputDialog("!!This is not an integer!!"+
															"\nPlease re-enter your guess"+(i+1)+"(0-9):");
						ch=input.charAt(0);
					}
					guess[i]=Integer.parseInt(input);
					}
				}


			fermi give=new fermi(guess,random);
			result(guess,random);
		}

	public static void result(int []guess,int []random)
	{
		fermi value=new fermi();
		for(int k=0;k<3;k++)
		{
			if(guess[k]==random[0])
			{
				JOptionPane.showMessageDialog(null,guess[k]+"=FERMI (^0^)");
			}
			else if(guess[k]==random[1] || guess[k]==random[2])
			{
				JOptionPane.showMessageDialog(null,guess[k]+"=PICO (^-^)");
			}
			else
			JOptionPane.showMessageDialog(null,guess[k]+"=NANO (x_x)");
		}
		if(guess[0]==random[0] && guess[1]==random[1] && guess[2]==random[2])
		value.totalWin();
		else
		value.totalLose();

		value.totalPlay();
		JOptionPane.showMessageDialog(null,value);

		valid();
	}

	public static void quit()
	{

		JOptionPane.showMessageDialog(null,"Come and play again!"+
											"\nBye Bye!");
	}


}

why the number of total play,total win and total lose doesnt increase even if i play it for more than 1 time?
help please.
tq.

Thats because of this:

fermi value=new fermi();

Everytime you call the result method you create a new fermi instance and assign it's reference to the same value variable so that the previous total play, total win and total lose get washed off.

In order retain the previous results you can modify your code in such a way that the same fermi instance is referred everytime a player plays. This is simple to implement, instead of declaring the value variable in the result method just declare for once and keep passing it to the result method everytime you call it.

sorry..but i am still a bit blur..
where should i declare the

fermi value=new fermi;

if not in the result method?

It should be in that part of your code which doesn't get called in loop. As of now I don't see any loops in your program which would keep asking the the user whether he wants to keep playing or if he wants to quit. I really do not know how in your code you are achieving multiple runs or whether you are even achieving it or not. The result method is something that would be called always passing the guesses of the user to be evaluated, so the result method is certainly not where you would want to put the declaration.

I suggest you change you code in a way first, so that it actually is kinda menu driven wherein after each cycle of guesses you ask the user whether he wants to continue. If he does you cycle/loop through the same process of collecting guesses and figuring out whether he is right or wrong and counting the wins and losses and plays. When finally he wants to quit you show him how he has fared in the entire session by showing him the values of the plays, wins and losses.

This article has been dead for over six months. Start a new discussion instead.