game problem

i got problem on my code

Page 1 of 1

9 Replies - 4539 Views - Last Post: 13 March 2009 - 07:49 PM Rate Topic: -----

#1 rabbitvt   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 14-October 08

game problem

Posted 08 March 2009 - 02:53 AM

import java.util.Scanner;
import java.util.Random;

public class try1
{
public static void main(String[] args)
{

int answ1;
int answ2;
int answ3;
int num1,num2,num3;

Scanner keyboard=new Scanner(System.in);

Random randomNumbers=new Random();


num1=randomNumbers.nextInt(9);

num2=randomNumbers.nextInt(9);

num3=randomNumbers.nextInt(9);

System.out.println("The correct answer is " +num1+ " , " +num2+ " , " +num3);

System.out.println("enter your 1st guess");
answ1=keyboard.nextInt();

System.out.println("enter your 2nd guess");
answ2=keyboard.nextInt();

System.out.println("enter your 3rd guess");
answ3=keyboard.nextInt();

if(answ1==num1)
{

System.out.println("Fermi");
}

else if(answ1==num2||answ1==num3)
{
System.out.println("Pico");
}

else if(answ1 !=num1&&answ1 !=num2&&answ1 !=num3)
{
System.out.println("Nano");
}


if(answ2==num2)
{
System.out.println("Fermi");
}
else if(answ2==num2||answ2==num3)
{
System.out.println("Pico");
}

else if(answ2!=num1&&answ2!=num2&&answ2!=num3)
{
System.out.println("Nano");
}


if(answ3==num3)
{

System.out.println("Fermi");
}
else if(answ3==num1||answ3==num2)
{
System.out.println("Pico");
}

else if(answ3!=num1&&answ3!=num2&&answ3!=num3)
{
System.out.println("Nano");
}


}
}
 




the result got problem,
like i key in 0 0 0 and the answer is 0 7 7 but the hint just come out Fermi Pico

Is This A Good Question/Topic? 0
  • +

Replies To: game problem

#2 markhazlett9   User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 61
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: game problem

Posted 08 March 2009 - 03:02 AM

Please tell us more about the errors you are getting. When does the error happen? What are you doing when the error happens. Etc. Etc. We need more detail, not just a bunch of code....Cheers
Was This Post Helpful? 0
  • +
  • -

#3 rabbitvt   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 14-October 08

Re: game problem

Posted 09 March 2009 - 09:45 AM

import java.util.Scanner;
import java.util.Random;

public class try3
{
	public static void main(String[] args)
	{

		int answ1;
		int answ2;
		int answ3;
		int num1,num2,num3;
		String input;
		char repeat=0;
		int guessCorrect=0;
		int guessWrong=0;
		int time=0;

		Scanner keyboard=new Scanner(System.in);
		Random randomNumbers=new Random();


		do
		{
 		num1=randomNumbers.nextInt(9);
		num2=randomNumbers.nextInt(9);
		num3=randomNumbers.nextInt(9);

			for(guessCorrect=0;guessCorrect<=3;guessCorrect++)
			{
			System.out.println("The correct answer is " +num1+ " , " +num2+ " , " +num3);

	   		System.out.println("Enter your 1st guess");
			answ1=keyboard.nextInt();

			System.out.println("Enter your 2nd guess");
			answ2=keyboard.nextInt();

			System.out.println("Enter your 3rd guess");
			answ3=keyboard.nextInt();

		//---------------------------------------------------------------------------------------

	   		 System.out.println("\ntesting 1st");

	   		if(answ1==num1)
	   		{
				System.out.println("Fermi");
				guessCorrect ++;

			}
				else if(answ1==num2||answ1==num3)
				{
					System.out.println("Pico");
					guessWrong ++;
				}

			else
			{
				System.out.println("Nano");
				guessWrong ++;
			}

			System.out.println("testing 2nd");

			if(answ2==num2)
			{
				System.out.println("Fermi");
				guessCorrect ++;

			}
				else if(answ2==num2||answ2==num3)
				{
					System.out.println("Pico");
					guessWrong ++;
				}

			else
			{
				System.out.println("Nano");
				guessWrong ++;
			}

			System.out.println("testing 3rd");

			if(answ3==num3)
			{
				System.out.println("Fermi");
				guessCorrect ++;

			}
				else if(answ3==num1||answ3==num2)
				{
					System.out.println("Pico");
					guessWrong ++;
				}

			else
			{
				System.out.println("Nano");
				guessWrong ++;
			}

		System.out.println("Do you want to try again?");
		System.out.println("Enter yes or no: ");
		input=keyboard.next();
		repeat=input.charAt(0);

		System.out.println("The correct answer is "+num1+","+num2+","+num3);

		System.out.println("Correct = "+guessCorrect+ "\nWrong = "+guessWrong);

			}
		}
		while(repeat=='Y'||repeat=='y');

	}
}











i want yser guess fermi fermi fermi then reset game to new random number and guess again.but i cant do that,it will count nano,pico,fermi nano,nano,fermi fermi,pico,nano count this as 3 also then reset,i dont wan this,but i dunno how to modify my loop,i think the loop also cause my program cant exit althou already enter "no".
thanks.
Was This Post Helpful? 0
  • +
  • -

#4 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: game problem

Posted 09 March 2009 - 01:58 PM

		System.out.println("Do you want to try again?");
		System.out.println("Enter yes or no: ");
		input=keyboard.next();
		repeat=input.charAt(0);


that is kinda sketchy, do it using a boolean.

		boolean again = true;
		System.out.println("Do you want to try again?");
		System.out.print("Enter yes or no: ");
		input=keyboard.nextLine();
		if(input.equalsIgnoreCase("no")
			  boolean again = false;



Then change the condition for the do..while loop to

do{
   ...All your code...
}while(again == true);


Was This Post Helpful? 0
  • +
  • -

#5 rabbitvt   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 14-October 08

Re: game problem

Posted 09 March 2009 - 06:29 PM

it doesn't work also.
after key in no also won't exit.
and the loop still got same problem...
Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman   User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: game problem

Posted 09 March 2009 - 07:45 PM

alright, i didnt understand anything...can you please explain in a little detail what this program should do and what kind of output are you looking for
Was This Post Helpful? 0
  • +
  • -

#7 rabbitvt   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 14-October 08

Re: game problem

Posted 09 March 2009 - 09:19 PM

i wan if the user guess correct the 3 random number,the hint will come out fermi fermi fermi and this count correct for 1 time,if the hint come out fermi,pico,nano or nano,pico,fermi this is count as wrong.
and if user is correct all the 3 number guess,then the game will reset new random number for user to play again,until user want to quite the game,and will display again the result how many time get correct or wrong.
Was This Post Helpful? 0
  • +
  • -

#8 rabbitvt   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 14-October 08

Re: game problem

Posted 09 March 2009 - 11:12 PM

how to do so that after get fermi,fermi,fermi mean one time guess then just can reset for new random number???
Was This Post Helpful? 0
  • +
  • -

#9 rabbitvt   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 14-October 08

Re: game problem

Posted 13 March 2009 - 10:57 AM

i want to do one copy method because the question need so,but i have not ant idea so i just want to copy the guessCorrect result but kept got error state that "new" cannot be find.
i dunno how to solve it...

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

/**
  *	This is the gameFermi class.
  */


public class gameFermi
{
	private int answer1,answer2,answer3;
	private int number1,number2,number3;
	private String input;
	private char repeat;
	private int guessCorrect,guessWrong,time;

	Random randomNumbers=new Random();

	/**
	  * This constructor sets all the starting to null.
	  */
	public gameFermi()
	{
		answer1=0;
		answer2=0;
		answer3=0;
		number1=0;
		number2=0;
		number3=0;
		input=null;
		repeat=0;
		guessCorrect=0;
		guessWrong=0;
		time=0;
	}


	public gameFermi(int ans1,int ans2,int ans3,int num1,int num2,int num3)
	{
		answer1=ans1;
		answer2=ans2;
		answer3=ans3;
		number1=num1;
		number2=num2;
		number3=num3;
	}


	public gameFermi(String i,char r)
	{
		input=i;
		repeat=r;
	}

	public gameFermi(int gc,int gw,int t)
	{
		guessCorrect=gc;
		guessWrong=gw;
		time=t;
	}

	/**
	  * The setAnswer1 method stores a value in the answer1 field.
	  * @param ans1 The value to store in answer1.
	  */

	public void setAnswer1(int ans1)
	{
		answer1=ans1;
	}


	/**
	  * The getAnswer1 method returns a user 1st answer.
	  * @return The value in the answer1 field.
	  */

	public int getAnswer1()
	{
		return answer1;
	}


	/**
	  * The setAnswer2 method stores a value in the answer2 field.
	  * @param ans2 The value to store in answer2.
	  */

	public void setAnswer2(int ans2)
	{
		answer2=ans2;
	}


	/**
	  * The getAnswer2 method returns a user 2nd answer.
	  * @return The value in the answer2 field.
	  */

	public int getAnswer2()
	{
		return answer2;
	}


	/**
	  * The setAnswer3 method stores a value in the answer3 field.
	  * @param ans3 The value to store in answer3.
	  */

	public void setAnswer3(int ans3)
	{
		answer3=ans3;
	}


	/**
	  * The getAnswer3 method returns a user 3rd answer.
	  * @return The value in the answer3 field.
	  */

	public int getAnswer3()
	{
		return answer3;
	}


	/**
	  * The setNumber1 method stores a value in the number1 field.
	  * @param num1 The value to store in number1.
	  */

	public void setNumber1(int num1)
	{
		number1=num1;
	}


	/**
	  * The getNumber1 method returns a random number.
	  * @return The value in the number1 field.
	  */

	public int getNumber1()
	{
		return number1;
	}


	/**
	  * The setNumber2 method stores a value in the number2 field.
	  * @param num2 The value to store in number2.
	  */

	public void setNumber2(int num2)
	{
		number2=num2;
	}


	/**
	  * The getNumber2 method returns a random number.
	  * @return The value in the number2 field.
	  */

	public int getNumber2()
	{
		return number2;
	}


	/**
	  * The setNumber3 method stores a value in the number3 field.
	  * @param num3 The value to store in number3.
	  */

	public void setNumber3(int num3)
	{
		number3=num3;
	}


	/**
	  * The getNumber3 method returns a random number.
	  * @return The value in the number3 field.
	  */

	public int getNumber3()
	{
		return number3;
	}


	/**
	  * The setInput method stores a value in the input field.
	  * @param i The value to store in input.
	  */

	public void setInput(String i)
	{
		input=i;
	}


	/**
	  * The getInput method returns the input user key in.
	  * @return The value in the input field.
	  */

	public String getInput()
	{
		return input;
	}


	/**
	  * The setRepeat method stores a value in the repeat field.
	  * @param r The value to store in repeat.
	  */

	public void setRepeat(char r)
	{
		repeat=r;
	}


	/**
	  * The getRepeat method returns the input for user to repeat the game.
	  * @return The value in the repeat field.
	  */

	public char getRepeat()
	{
		return repeat;
	}


	/**
	  * The start method use to give user brief instruction about the game.
	  */

	public void start()
	{
		JOptionPane.showMessageDialog(null,"Welcome To The Fermi Game ");
		JOptionPane.showMessageDialog(null,"Game instruction: "+
										"\n1. Key in 3 number you guess."+
										"\n2. Get a hint."+
										"\n	Fermi=> correct answer."+
										"\n	Pico => answer correct,but different position."+
										"\n	Nano => wrong answer."+
										"\n3. Enter yes or no to continue your game until you win the game with 3 "+
										"\n	   correct answer with the hint fermi,fermi,fermi."+
										"\n4. Enter yes or no to contniue with new game or else exit the game."+
										"\n5. Enjoy the game!");
	}



	/**
	  * The displyReuqire method returns the answer key in by user whether
	  * they want to continue with new game or not.
	  * @return The answer key in by user.
	  */

	public char displyRequire()
	{
		//Require whether user want continue with new game.
		input=JOptionPane.showInputDialog("Do you want to play again?"+"\nEnter yes or no:");
		repeat=input.charAt(0);		//Get the first char to continue or else to exit.
		return repeat;
	}


	/**
	  * The displyRequire1 method returns the answer key in by user whether
	  * they want to continue with the same game or not after the have wrong tries.
	  * @return The answer key in by user.
	  */

	public char displyRequire1()
	{
		//Reuqire whether user want continue the guess after wrong tries.
		input=JOptionPane.showInputDialog("Do you want to try again?"+"\nEnter yes or no :");
		repeat=input.charAt(0);	//Get the first char to repeat or else to exit.
		return repeat;
	}


	/**
	  * The displyRandom method returns the 1st random number.
	  * @return The 1st random number.
	  */

	public int displyRandom()
	{
		//random number for 0-9
		number1=randomNumbers.nextInt(9);
		return number1;
	}

	/**
	  * The displyRandom1 method returns the 2nd random number.
	  * @return The 2nd random number.
	  */

	public int displyRandom1()
	{
		//random number for 0-9
		number2=randomNumbers.nextInt(9);
		return number2;
	}


	/**
	  * The displyRandom2 method returns the 3rd random number.
	  * @return The 3rd random number.
	  */

	public int displyRandom2()
	{
		//random number for 0-9
		number3=randomNumbers.nextInt(9);
		return number3;
	}


	/**
	  * The toString method returns the result of game by passing the
	  * value from main.
	  * @return The guessCorrect,guessWrong and win result.
	  */

	public String toString(int guessCorrect,int guessWrong,int time)
	{
		//create a string describing the result.
		String str="Guess Correct = " +guessCorrect+
					"\nGuess Wrong = "+guessWrong+
					"\nWins = "+time+" time(s)";

		//return the string.
		return str;
	}//end method toString


	public gameFermi copy()
	{
		//create a new gamFermi object and initialize it
		//with the same data held by the calling object.
		gameFermi copyObject=new gameFermi(guessCorrect);

		//return a reference to the new object.
		return copyObject;
	}

}





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

/**
  * This prgram is the simple Fermi game.
  */

public class gameFermiDemo
{
	public static void main(String[] args)
	{

		//Create a gameFermi object and assign its
		//address to the fermi variable.
		gameFermi fermi=new gameFermi();




		int answ1, answ2, answ3;	//three number jey in by user
		int num1,num2,num3;			//three random number
		String input;				//for reading input
		char repeat='y';			//initialize the repeat for user to continue game
		int guessCorrect=0;			//initialize the sum of correct guess
		int guessWrong=0;			//initialize the sum of wrong guess
		int time=0;					//initialize the time of game user win
		int win=0;					//initialize to get user correct all three guess and sum as one win
		int game=0;					//initialize to sum up how many time of game user have play
		String s1="Hello";			//sotre the string to s1

		Random randomNumbers=new Random();

		JOptionPane.showMessageDialog(null,s1);


		//Call start method.
		fermi.start();

		//Call displyRandom method and return the random number.
	   	num1=fermi.displyRandom();
	   	num2=fermi.displyRandom1();
	   	num3=fermi.displyRandom2();
		game++;

		gameFermi fermi1=new gameFermi();

		do
		{

			JOptionPane.showMessageDialog(null,"Games "+game+"\nGuess the number <1-9>");

			//User key in the 3 guess number.
			input=JOptionPane.showInputDialog(null,"Enter your 1st guess");
			answ1=Integer.parseInt(input);

			fermi.setAnswer1(answ1);

			input=JOptionPane.showInputDialog(null,"Enter your 2nd guess");
			answ2=Integer.parseInt(input);

			fermi.setAnswer2(answ2);

			input=JOptionPane.showInputDialog(null,"Enter your 3rd guess");
			answ3=Integer.parseInt(input);


			fermi.setAnswer3(answ3);

			win=0;


			//Display the hint and sum up the correct,wrong and win.
			if(answ1==num1)
			{
				JOptionPane.showMessageDialog(null,"Your 1st answer is "+answ1+"\n=> Fermi");
				win++;
				guessCorrect ++;
			}
			else if(answ1==num2||answ1==num3)
			{
				JOptionPane.showMessageDialog(null,"Your 1st answer is "+answ1+"\n=> Pico");
				guessWrong ++;
			}
			else
			{
				JOptionPane.showMessageDialog(null,"Your 1st answer is "+answ1+"\n=> Nano");
				guessWrong ++;
			}

			if(answ2==num2)
			{
				JOptionPane.showMessageDialog(null,"Your 2nd answer is "+answ2+"\n=> Fermi");
				win++;
				guessCorrect ++;
			}
			else if(answ2==num1||answ2==num3)
			{
				JOptionPane.showMessageDialog(null,"Your 2nd answer is "+answ2+"\n=> Pico");
				guessWrong ++;
			}
			else
			{
				JOptionPane.showMessageDialog(null,"Your 2nd answer is "+answ2+"\n=> Nano");
				guessWrong ++;
			}

			if(answ3==num3)
			{
				JOptionPane.showMessageDialog(null,"Your 3rd answer is "+answ3+"\n=> Fermi");
				win++;
				guessCorrect ++;
			}
			else if(answ3==num1||answ3==num2)
			{
				JOptionPane.showMessageDialog(null,"Your 3rd answer is "+answ3+"\n=> Pico");
				guessWrong ++;
			}
		   	else
			{
				JOptionPane.showMessageDialog(null,"Your 3rd answer is "+answ3+"\n=> Nano");
				guessWrong ++;
			}

			//If user get 3 correct in one time guess,random a new 3 number.
			if (win==3)
			{
				JOptionPane.showMessageDialog(null,"Congratulations!"+"\nYou Win!");

				  //Call displyRandom method and return the random number.
				  num1=fermi.displyRandom();
				  num2=fermi.displyRandom1();
				  num3=fermi.displyRandom2();

				game++;
				time++;

				//Call displyRequire method and return the require statement
				//and answer whether user want continue with new game.
				repeat=fermi.displyRequire();
			}
			else
			{
				//Call displyRequire1 method and return the require statement and answer
				//whether user want continue to guess after wrong tries.
				repeat=fermi.displyRequire1();
			}


			//Display result if user want to exit the game.
			if(repeat=='n'||repeat=='N')
			{

				JOptionPane.showMessageDialog(null,fermi.toString(guessCorrect,guessWrong,time));
				JOptionPane.showMessageDialog(null,"Thank for your support!");

			}


		}

		while(repeat=='Y'||repeat=='y');
		fermi.setRepeat(repeat);


		//test for equality
		if(s1.equals("Hello"))
			System.out.println("Both are the same.");	//true
		else
			System.out.println("Both are different.");	//false

			fermi=fermi1.copy();

			System.out.println("result1: \n" +fermi);
			System.out.println();
			System.out.println("result2: \n"+fermi1);


		if(fermi==fermi1)
			{
				System.out.println("Same object");
			}
		else
			{
				System.out.println("Different");
			}



		//End the program.
		System.exit(0);

	}

}



hope can get some help .
thanks.
Was This Post Helpful? 0
  • +
  • -

#10 rabbitvt   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 14-October 08

Re: game problem

Posted 13 March 2009 - 07:49 PM

i just want to create a simply copy method,no people can give me a help?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1