Results 1 to 4 of 4
  1. #1
    rayda is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default why the number will not increase?

    Java Code:
    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;
    	}
    
    
    
    }
    Java Code:
    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 does't increase even if i play it for more than 1 time?..
    please help.
    tq.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Can you ask your question more specifically. You just post two classes here and who know regarding what code you are talking here.

  3. #3
    rayda is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default

    sorry about it..
    this is a program that ask user to enter 3 numbers,If the digit guessed for a given position is correct, then the reply is Fermi. If the digit guessed for a given position is in a different position, the reply is Pico. If the digit guessed for a given position does not match any of these three digits, then the reply is Nano. then i need to show the number of total play, total win and total lose after each game.
    but my problem is, if i didnt get 3 correct,the number of total play is always 1,total of win is always zero and total lose is always 1, even when i play it for the 3rd or 4th time..
    how can i change my coding so that after the 2nd time i play, the number of total play will be two and so on.

  4. #4
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    i find that you construct fermi value everytime when result method call.
    you do not "save" any info to used by next result method call

Similar Threads

  1. increase pixel size
    By rosh72851 in forum New To Java
    Replies: 9
    Last Post: 07-31-2012, 07:30 PM
  2. Increase signature length?
    By xcallmejudasx in forum Suggestions & Feedback
    Replies: 3
    Last Post: 12-18-2008, 06:04 PM
  3. Replies: 1
    Last Post: 11-03-2008, 02:38 PM
  4. Replies: 3
    Last Post: 09-27-2008, 04:46 AM
  5. gridbaglayout: increase/decrease size of components.
    By newtojava7 in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 07:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •