Need Java Help

Users who are viewing this thread

chargerz43

Knight
So I'm not good with loops in Java. This loop keeps repeating itself into an infinite loop. Any tips on how to fix it?
Once again I'm not good with Java so don't judge  :razz:

Code:
import java.util.Scanner;
public class NumberGuess
{
	public static void main(String[] args)
	{

		System.out.println("Think of a whole number(1-100)");
		int range = 100;
		int min = 0;
		int max = 100;
		int average;
		int number;
		int x = 0;
		int guess;


		while (x  0);
		{
			max = guess;
			guess = (min + max) / 2
			System.out.print("Is your number... " + guess );
			System.out.print("?");
			System.out.println("1) Yes");
			System.out.println("2) No");
			
			int choice = reader.nextInt();
			switch(choice)
			{
     			case 1: x = 1;
					break;
				case 2: x = 0;
					break;
				default:
					System.out.println("Wat?");
					break;
			}
		}



		System.out.print("I win! Your number is ");
		System.out.print(guess);
		System.out.println("!");


		}



}
}
 
I'd like to say, your case/break system for escaping the loop is awful. Break should be used only when absolutely necessary.
 
Lerber said:
I'd like to say, your case/break system for escaping the loop is awful. Break should be used only when absolutely necessary.

The break is for escaping the case/switch not the loop. If you don't have the break you will execute all the code for the cases below, even if your choice doesn't match the case. All the case really does is tell the program where to start reading code from, it doesn't say anything about stopping when it gets to end of one case or the start of the next.  :roll:
 
I'm well aware. Considering you have essentially two (three) possibilities, the case/switch is fairly superfluous anyway, when an if-then-else structure would suffice.
 
Bad habits die hard. Breaks are far less efficient (as are cases) when writing code. I'm in 300+ level CS courses and I've never used a case/switch structure. In general, I just think it's a better habit to learn to use if/else-ifs as they're more efficient and avoid breaks, which are, in general, a poor coding practice.
 
Not sure how you can consider breaks and cases less efficient, surely it all depends on the task you are trying to perform and the programmers familiarity with cases?

I consider breaks to be lazy programming, used when the programmer can't think, or be bothered to think, of a more elegant solution. Not sure how you can use efficiency as an argument against breaks when they are often the easiest way to do something, although perhaps not the best.

As for cases, all cases can be written in an if/else structure. but there are times when cases make the code more readable, and therefore maintainable. Imagine a scenario where you have 20 different options, a case would be far easier to read than 20 if/elses. I'm not trying to say you should always try to use cases instead of if/else, just that a good programmer would be aware of them and use them if there were clear benefits rather than just disregarding them. Right tools for the job sort of thing, always worth having a Phillips screwdriver in your belt even if a flat head will screw in most screws.
 
Randomly bored today digging up an old tree but... supposedly switch is more efficient when there are more cases as the compiler can do some optimization, but honestly for the coding most people are going to end up doing in their life, maintainability should be number one, cause you are going to have to change it, and requirements are going to be moving all over the place, and the only person it's going to F is yourself later on down the line.

I find that my code reads better in general to if/else stuff with like < 4 branches and switch statement the rest, though if using enums the switch statement works really nice with that. Though in reality seems to be just a style choice. I could easily say in 10 years of writing code I've written < 10 while loops, but in school I used them all the time. I just happen to like the for style and most of my loops are just foreach's in a collection anyhow.

I don't know why the break hate is out there, it has uses, like say you have a linear search on something that'll never be more than 10 items, just break right after you find it. There is always that trade off between cost, time, and quality.

Doing stuff elegantly can actually be quite detrimental when you suddenly have a new case that breaks the clever little format you've had for everything else, leaving you to dirty hacks. I guess personally I kind of run on my 'casual efficiency', kinda get stuff done, because honestly most people care about the time it takes you over everything else, but if you run full speed and don't make it fit together nicely everyone suffers, so the best pace I've found is that kinda low cruise, if its taking too long to figure out something nice, **** it, put the hack in and move on.
 
Back
Top Bottom