Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

neelvaka

macrumors newbie
Original poster
Sep 15, 2007
16
0
ok this is my current code
Code:
//I'm going to use these from now own. Help organize thoughts more for me.

import java.util.Scanner;
public class quadratic
{
	public static void main(String[] args)
{
		Scanner kb = new Scanner(System.in); //this is 4 user input
		int x = (int)(100 * Math.random()) + 1; 
		int inputnumber = 0; //this is the user input #
		char answer;
		int counter = 0;
		boolean tf = true;

			System.out.print("If you wnna play a game say yes otherwise say no: ");
			String answer2 = kb.nextLine();
			answer = answer2.charAt(0);

			while (tf == true)
{
				char answer3 = 'y'; //Compares yes and no answer from the user yepyep

				if (answer != answer3)
{
					System.out.println("Goodbye!");
					tf = false;
}
				else
{
					System.out.println("\nI am thinking of a number between 1 and 100. Try to guess it.\n"); //and the guessing game starts!

					while (x != inputnumber)
{
						System.out.print("What's your guess? ");
						inputnumber = kb.nextInt();

						if (x < inputnumber)
{
							System.out.println(inputnumber + " is too big!\n");
}

						else if (x > inputnumber)
{
							System.out.println(inputnumber + " is too small!\n");
}
						counter = counter + 1;
}
					if (counter == 1)
{
						System.out.println("You've got it in " + counter + " guesses. That was lucky!");
						System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
						break;
}
					else if (counter >= 2 && counter <= 4)
{
						System.out.println("You've got it in " + counter + " guesses. That was amazing!");
						System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
						break;
}
					else if (counter >= 5 && counter <= 6)
{
						System.out.println("You've got it in " + counter + " guesses. That was really good!");
						System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
						break;
}
					else if (counter == 7)
{
						System.out.println("You've got it in " + counter + " guesses. That was ok!");
						System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
						break;
}
					else if (counter >= 8 && counter <= 9)
{
						System.out.println("You've got it in " + counter + " guesses. That was pretty bad!");
						System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
						break;
}
					else
{
						System.out.println("You've got it in " + counter + " guesses. This is not your game!");
						System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
						break;
}
}
}
}
}

and it is right nothing wrong whatsoever. IT WORKS!

BUT

after reading my assignment carefully we werent supposed to use scanner but javaIO.* :eek::eek::eek:nd that is diff then what im used to :confused:

help me out ppl plz?

btw new to this forum and it rocks lol!
pretty much i need to use strings and readLines and stuff
but im really confused:confused:
any help would be great!


btw i am using ECLIPSE PLATFORM to run these scripts if that helps and im not allowed to use anything else
http://www.eclipse.org/
 

angelwatt

Moderator emeritus
Aug 16, 2005
7,852
9
USA
Well I suggest reading up on BufferedReader, InputStreamReader, and StringTokenizer to find a solution. I'd say more, but this is your assignment so you should making the effort. Good luck.
 

Doctor Q

Administrator
Staff member
Sep 19, 2002
40,077
8,340
Los Angeles
Is your task to implement your own methods equivalent to the nextLine and nextInt methods of the Scanner class?

If so, the starting point for doing that is to make sure you know what they do:
nextLine reads the InputStream (starting where it left off) until the next linebreak and returns what was read as a string.

nextInt reads the InputStream (starting where it left off), skipping whitespace, to find an integer (a sequence of digits with an optional leading + or -) and returning the int value.​
Does that make sense? Is that enough to get you started?
 

zimv20

macrumors 601
Jul 18, 2002
4,402
11
toronto
you should justify the curly braces with the code they go with. that's the second most ridiculous convention i've seen.
 

Texas04

macrumors 6502a
Jul 2, 2005
886
1
Texas
Java IO?

Does he want you to read in from a file?

And also remember formatting issues.

For the if statements and while and everything that needs brackets, do something like this.


Code:
while(thisVariable==thatVariable)
   {
          if(theWord=!thatWord)
          {
                   System.out.println("Do this");
          }
   }
   else
   {
          if(thisWord==thatWord)
          {    
                 System.out.prtinln("Do that");
          }    
   }

If you use the tab function it will look a lot nicer then mine on these forums :).
 

btaussie

macrumors member
Jun 11, 2007
63
0
Say thanks :p

//Here you go mate, you owe me one :)

import java.io.*;
public class Quadratic
{
public static void main(String[] args)
{
BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
int x = (int)(100 * Math.random()) + 1;
int inputnumber = 0;
char answer;
int counter = 0;
boolean tf = true;

System.out.print("If you wanna play a game say yes otherwise say no: ");
String answer = kb.readLine().charAt(0);
//answer = answer2.charAt(0);

while (tf == true)
{
char answer3 = 'y'; //Compares yes and no answer from the user yepyep

if (answer != answer3)
{
System.out.println("Goodbye!");
tf = false;
}
else
{
System.out.println("\nI am thinking of a number between 1 and 100. Try to guess it.\n"); //and the guessing game starts!

while (x != inputnumber)
{
System.out.print("What's your guess? ");
inputnumber = Integer.parseInt(kb.readLine());

if (x < inputnumber)
{
System.out.println(inputnumber + " is too big!\n");
}

else if (x > inputnumber)
{
System.out.println(inputnumber + " is too small!\n");
}
counter = counter + 1;
}
if (counter == 1)
{
System.out.println("You've got it in " + counter + " guesses. That was lucky!");
System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
break;
}
else if (counter >= 2 && counter <= 4)
{
System.out.println("You've got it in " + counter + " guesses. That was amazing!");
System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
break;
}
else if (counter >= 5 && counter <= 6)
{
System.out.println("You've got it in " + counter + " guesses. That was really good!");
System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
break;
}
else if (counter == 7)
{
System.out.println("You've got it in " + counter + " guesses. That was ok!");
System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
break;
}
else if (counter >= 8 && counter <= 9)
{
System.out.println("You've got it in " + counter + " guesses. That was pretty bad!");
System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
break;
}
else
{
System.out.println("You've got it in " + counter + " guesses. This is not your game!");
System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
break;
}
}
}
}

//I'll leave you to format your code correctly but if you are using popular Java editors like Eclipse/NetBeans, you can use automatic settings that will format your code for you! Google it :)
 

btaussie

macrumors member
Jun 11, 2007
63
0
Forgot to add, does your code need to capture exceptions? Specifically IOExceptions, NumberFormatExceptions etc....
 

Doctor Q

Administrator
Staff member
Sep 19, 2002
40,077
8,340
Los Angeles
A caution

We prefer to have members give advice, not complete solutions, to those doing homework, but it's up to the individuals involved.

Be aware that we've had cases where professors or other students visited online forums and identified students who used code written for them by others.
 

btaussie

macrumors member
Jun 11, 2007
63
0
We prefer to have members give advice, not complete solutions, to those doing homework, but it's up to the individuals involved.

Be aware that we've had cases where professors or other students visited online forums and identified students who used code written for them by others.


That's fair enough, but if you look at the code, I changed three lines of code, that's it.

Enjoy!
 

garethlewis2

macrumors 6502
Dec 6, 2006
277
1
This guy deserves to fail his course and even his degree. If the first thing he thinks of when confronted by a problem is to automatically post on a forum rather than trying to look through his books or at programming examples, can you imagine how useless this guy would be in a job.
 

btaussie

macrumors member
Jun 11, 2007
63
0
This guy deserves to fail his course and even his degree. If the first thing he thinks of when confronted by a problem is to automatically post on a forum rather than trying to look through his books or at programming examples, can you imagine how useless this guy would be in a job.

Dude if you have any idea about Java Programming you can tell this guy has still got a hell of a lot to learn about programming in Java before you could make an assumption that "...useless this guy would be in a job." My bet is this guy is in his first semester of programming at college/university. Still got a long way to go. Although I don't believe the guy deserves to fail his course and even his degree, I do believe consulting notes, texts and even websites are the way to go before posting 'automatically' on forums.

That's what helped me get my degree and is sure helping me complete my Masters at the moment.

Cheers.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.