my code have problem

what have happen to my code?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

37 Replies - 1920 Views - Last Post: 19 February 2009 - 05:15 AM Rate Topic: -----

#1 rabbitvt  Icon User is offline

  • D.I.C Head

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

my code have problem

Post icon  Posted 10 February 2009 - 06:19 PM

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

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

int num;	//random number
int guess;	//number guess by user
int diff;	//absolute value of num-guess
char repeat;//whether user want try again or not
String input;//to hold input


Scanner keyboard=new Scanner(System.in);

Random randomNumbers=new Random();

num=randomNumbers.nextInt(100);

System.out.println("Please enter the number you guess : ");
guess=keyboard.nextInt();

diff=num-guess;

if(diff==0)
	System.out.println("Your answer is correct!");
else if(diff>=50)
	System.out.println("Your answer is still very high apart");
else if(diff>=30&&diff<50)
	System.out.println("Your answer is high");
else if(diff>=15&&diff<30)
	System.out.println("Your answer is moderate high");
else if(diff>0&&diff<15)
	System.out.println("Your answer is somewhat high");
	System.out.println("Do you wan to try again?");
	System.out.print("Enter Y or N:");
	input=keyboard.nextLine();
	repeat=input.charAt(0);
}
}

This post has been edited by rabbitvt: 10 February 2009 - 07:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: my code have problem

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

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

Re: my code have problem

Posted 10 February 2009 - 06:20 PM

what problems are you having?, can you please elaborate more
Was This Post Helpful? 0
  • +
  • -

#3 rabbitvt  Icon User is offline

  • D.I.C Head

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

Re: my code have problem

Posted 10 February 2009 - 06:32 PM

whn execute there say exception n thread "main"java.lang.StringIndexOutOfBoundException:String index out of range: 0
at java.lang.String.charAt<String.java:687>
at Assignt1.main<Sgggnt1.java:40>


what that mean???this show out at prompt window...
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3108
  • View blog
  • Posts: 19,146
  • Joined: 14-September 07

Re: my code have problem

Posted 10 February 2009 - 06:36 PM

It isn't waiting to get input, so input isn't initialized yet and trying to access the element at 0 is throwing the error.

More specifically nextLine() skips to the nextLine and therefore input isn't gathered. I'd use next():

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

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

int num; //random number
int guess; //number guess by user
int diff; //absolute value of num-guess
char repeat;//whether user want try again or not
String input;//to hold input


Scanner keyboard=new Scanner(System.in);

Random randomNumbers=new Random();

num=randomNumbers.nextInt(100);

System.out.println("Please enter the number you guess : ");
guess=keyboard.nextInt();

diff=num-guess;

if(diff==0)
System.out.println("Your answer is correct!");
else if(diff>=50)
System.out.println("Your answer is still very high apart");
else if(diff>=30&&diff<50)
System.out.println("Your answer is high");
else if(diff>=15&&diff<30)
System.out.println("Your answer is moderate high");
else if(diff>0&&diff<15)
System.out.println("Your answer is somewhat high");
System.out.println("Do you wan to try again?");
System.out.print("Enter Y or N:");
input=keyboard.next(); //here
repeat=input.charAt(0);
System.out.println(repeat); //showing it returns Y
}
}



edited because i used the word therefore too many times :)

This post has been edited by KYA: 10 February 2009 - 06:48 PM

Was This Post Helpful? 0
  • +
  • -

#5 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: my code have problem

Posted 10 February 2009 - 06:44 PM

Modified title to be more descriptive of the problem.
Was This Post Helpful? 0
  • +
  • -

#6 rabbitvt  Icon User is offline

  • D.I.C Head

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

Re: my code have problem

Posted 10 February 2009 - 07:36 PM

if i wan user jz can try only 5 time and i do like this correct or not???

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

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

int num; //random number
int guess; //number guess by user
int diff; //absolute value of num-guess
char repeat;//whether user want try again or not
String input;//to hold input
int a=0;
int x[]={0,0,0,0,0};

Scanner keyboard=new Scanner(System.in);

do
{
if(a>=4) //by add this correct or not?

x[a]=1; //by add this correct or not?

Random randomNumbers=new Random();

num=randomNumbers.nextInt(100);

System.out.println("Please enter the number you guess : ");
guess=keyboard.nextInt();

diff=num-guess;

if(diff==0)
System.out.println("Your answer is correct!");
else if(diff>=50)
System.out.println("Your answer is still very high apart");
else if(diff>=30&&diff<50)
System.out.println("Your answer is high");
else if(diff>=15&&diff<30)
System.out.println("Your answer is moderate high");
else if(diff>0&&diff<15)
System.out.println("Your answer is somewhat high");
System.out.println("Do you wan to try again?");
System.out.print("Enter Y or N:");
input=keyboard.next();
repeat=input.charAt(0);
a++; //by add this correct or not?

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

}
}
Was This Post Helpful? 0
  • +
  • -

#7 rabbitvt  Icon User is offline

  • D.I.C Head

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

Re: my code have problem

Posted 10 February 2009 - 08:05 PM

after user have tried 5 times then i want the programme to direct exit what do i add???
Was This Post Helpful? 0
  • +
  • -

#8 rabbitvt  Icon User is offline

  • D.I.C Head

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

Re: my code have problem

Posted 12 February 2009 - 10:38 PM

can i use do while loop to loop back the game after finish 5 time guess???
Was This Post Helpful? 0
  • +
  • -

#9 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

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

Re: my code have problem

Posted 12 February 2009 - 10:44 PM

yes you will need a loop for that
Was This Post Helpful? 0
  • +
  • -

#10 rabbitvt  Icon User is offline

  • D.I.C Head

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

Re: my code have problem

Posted 14 February 2009 - 12:34 AM

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

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

int num; //random number
int guess; //number guess by user
int diff; //absolute value of num-guess
char repeat;//whether user want try again or not
String input;//to hold input
int guessCorrect = 0;
int guessWrong = 0;
int counter=0;


Scanner keyboard=new Scanner(System.in);

Random randomNumbers=new Random();

num=randomNumbers.nextInt(100);

do
{

for(counter = 0 ; counter < 5 ; counter ++)
{

System.out.println("Please enter the number you guess : ");
guess=keyboard.nextInt();

diff=Math.abs(num-guess);
System.out.println("the answer is " +num);
if(diff==0)
{
System.out.println("Your answer is correct!");
guessCorrect ++;//for calculate the correct answer user guess
}
else if(diff>=50)
{
System.out.println("Your answer is still very high apart");
guessWrong ++; //for calculate the wrong answer user guess
}
else if(diff>=30&&diff<50)
{
System.out.println("Your answer is high");
guessWrong ++; //for calculate the wrong answer user guess
}
else if(diff>=15&&diff<30)
{
System.out.println("Your answer is moderate high");
guessWrong ++; //for calculate the wrong answer user guess
}
else if(diff>0&&diff<15)
{
System.out.println("Your answer is somewhat high");
guessWrong ++; //for calculate the wrong answer user guess
}

//only display when use try 5 times..
if(counter == 4)
{
System.out.println("correct = "+guessCorrect );
System.out.println("wrong = "+guessWrong );
}
}
System.out.println("the answer is " +num);
System.out.println("Do you wan to repeat?");
System.out.println("Enter yes or no");
input=keyboard.next();
repeat=input.charAt(0);

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

System.out.println("the answer is " +num);

System.out.println("thanks for support!");


}
}




what do i modify so that when i guess the correct number the code wont ask user to guess again but direct exit???
Was This Post Helpful? 0
  • +
  • -

#11 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

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

Re: my code have problem

Posted 14 February 2009 - 01:08 AM

insert a break statement
import java.util.Scanner;
import java.util.Random;

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

		int num; //random number
		int guess; //number guess by user
		int diff; //absolute value of num-guess
		char repeat;//whether user want try again or not
		String input;//to hold input
		int guessCorrect = 0;
		int guessWrong = 0;
		int counter=0;

	
		Scanner keyboard=new Scanner(System.in);

		Random randomNumbers=new Random();

		num=randomNumbers.nextInt(100);

		do
		{
	
			for(counter = 0; counter < 5; counter ++)
			{

				System.out.println("Please enter the number you guess : ");
				guess=keyboard.nextInt();

				diff=Math.abs(num-guess);
				System.out.println("the answer is " +num);
				if(diff==0)
				{
					System.out.println("Your answer is correct!");
					guessCorrect ++;//for calculate the correct answer user guess
					break;//will exit the loop when the guess is right
				}
				else if(diff>=50)
				{
					System.out.println("Your answer is still very high apart");
					guessWrong ++; //for calculate the wrong answer user guess
				}
				else if(diff>=30&&diff<50)
				{
					System.out.println("Your answer is high");
					guessWrong ++; //for calculate the wrong answer user guess
				}
				else if(diff>=15&&diff<30)
				{
					System.out.println("Your answer is moderate high");
					guessWrong ++; //for calculate the wrong answer user guess
				}
				else if(diff>0&&diff<15)	
				{
					System.out.println("Your answer is somewhat high");
					guessWrong ++; //for calculate the wrong answer user guess
				}

					//only display when use try 5 times..
				if(counter == 4)
				{
					System.out.println("correct = "+guessCorrect );
					System.out.println("wrong = "+guessWrong );
				}
			}
			System.out.println("the answer is " +num);
			System.out.println("Do you wan to repeat?");
			System.out.println("Enter yes or no");
			input=keyboard.next();
			repeat=input.charAt(0);

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

		System.out.println("the answer is " +num);

		System.out.println("thanks for support!");


	}
}


was that what you were looking for?
Was This Post Helpful? 0
  • +
  • -

#12 rabbitvt  Icon User is offline

  • D.I.C Head

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

Re: my code have problem

Posted 14 February 2009 - 04:54 PM

wat i wan is,when the user guess the correct answer then the program will exit and ask the user if want continue the game?if user want continue then the random number will change to another answer for user to guess,but now my program is didn't change to the new random number,but still the same number.
Was This Post Helpful? 0
  • +
  • -

#13 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

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

Re: my code have problem

Posted 14 February 2009 - 04:58 PM

move the random number generator into the do while look
import java.util.Scanner;
import java.util.Random;

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

		int num; //random number
		int guess; //number guess by user
		int diff; //absolute value of num-guess
		char repeat;//whether user want try again or not
		String input;//to hold input
		int guessCorrect = 0;
		int guessWrong = 0;
		int counter=0;

   
		Scanner keyboard=new Scanner(System.in);

		Random randomNumbers=new Random();

		

		do
		{
		num=randomNumbers.nextInt(100);
   
			for(counter = 0; counter < 5; counter ++)
			{

				System.out.println("Please enter the number you guess : ");
				guess=keyboard.nextInt();

				diff=Math.abs(num-guess);
				System.out.println("the answer is " +num);
				if(diff==0)
				{
					System.out.println("Your answer is correct!");
					guessCorrect ++;//for calculate the correct answer user guess
					break;//will exit the loop when the guess is right
				}
				else if(diff>=50)
				{
					System.out.println("Your answer is still very high apart");
					guessWrong ++; //for calculate the wrong answer user guess
				}
				else if(diff>=30&&diff<50)
				{
					System.out.println("Your answer is high");
					guessWrong ++; //for calculate the wrong answer user guess
				}
				else if(diff>=15&&diff<30)
				{
					System.out.println("Your answer is moderate high");
					guessWrong ++; //for calculate the wrong answer user guess
				}
				else if(diff>0&&diff<15)	
				{
					System.out.println("Your answer is somewhat high");
					guessWrong ++; //for calculate the wrong answer user guess
				}

					//only display when use try 5 times..
				if(counter == 4)
				{
					System.out.println("correct = "+guessCorrect );
					System.out.println("wrong = "+guessWrong );
				}
			}
			System.out.println("the answer is " +num);
			System.out.println("Do you wan to repeat?");
			System.out.println("Enter yes or no");
			input=keyboard.next();
			repeat=input.charAt(0);

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

		System.out.println("the answer is " +num);

		System.out.println("thanks for support!");


	}
}


this will make it to generate a new random number each time
Was This Post Helpful? 0
  • +
  • -

#14 rabbitvt  Icon User is offline

  • D.I.C Head

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

Re: my code have problem

Posted 14 February 2009 - 07:51 PM

ok.thanks very much,actually i have try like that before,but i move the " Random randomNumbers=new Random();"inside together,so i cant get what i want.thanks.
now i will trying to create some method for my code.
if got problem i will ask again.
thanks a lot for help.
Was This Post Helpful? 0
  • +
  • -

#15 rabbitvt  Icon User is offline

  • D.I.C Head

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

Re: my code have problem

Posted 14 February 2009 - 08:22 PM

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

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

		int num; 				//random number
		int guess; 				//number guess by user
		int diff; 				//absolute value of num-guess
		char repeat;			//whether user want try again or not
		String input;			//to hold input
		int guessCorrect = 0;	//to sum up how many times get correct
		int guessWrong = 0;		//to sum up how many times get wrong
		int counter=0;			//for the guess 5 times


		Scanner keyboard=new Scanner(System.in);

		Random randomNumbers=new Random();



		do
		{
			num=randomNumbers.nextInt(100);

			for(counter = 0; counter < 5; counter ++)
			{

				System.out.println("Please enter the number you guess : ");
				guess=keyboard.nextInt();

				diff=Math.abs(num-guess);
				System.out.println("the answer is " +num);
				if(diff==0)
					System.out.println("Your answer is correct!");
					guessCorrect ++;		//for calculate the correct answer user guess
					break;					//will exit the loop when the guess is right

				else if(diff>=50&&guess>num)
					System.out.println("Your answer is still very high apart");
					guessWrong ++;

				else if(diff>=50&&guess<num)
					System.out.println("Your answer is still very low apart");
					guessWrong ++;		//for calculate the wrong answer user guess


				else if(diff>=30&&diff<50)
					if(guess>num)
						System.out.println("Your answer is high");
						guessWrong ++;
					else
						System.out.println("Your answer is low");
						guessWrong ++; //for calculate the wrong answer user guess


				else if(diff>=15&&diff<30)

					if(guess>num)
						System.out.println("Your answer is moderate high");
						guessWrong ++;
					else
						System.out.println("Your answer is moderate low");
					guessWrong ++;//for calculate the wrong answer user guess

				else if(diff>0&&diff<15)

					if(guess>num)
						System.out.println("Your answer is somewhat high");
						guessWrong ++;
					else
						System.out.println("Your answer is somewhat low");
						guessWrong ++; //for calculate the wrong answer user guess


					//only display when use try 5 times..
				if(counter == 4)
				{
					System.out.println("correct = "+guessCorrect );
					System.out.println("wrong = "+guessWrong );
				}
			}
			System.out.println("the answer is " +num);
			System.out.println("Do you wan to repeat?");
			System.out.println("Enter yes or no");
			input=keyboard.next();
			repeat=input.charAt(0);

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

		System.out.println("the answer is " +num);

		System.out.println("thanks for support!");


	}
}






i got 60 errors after i add the "low " part, is it the problem is bacause my curly bracket or what else???

Mod Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3