Write a program that prompts the user for a bearing from 0 to 359 and prints the corresponding letter of the compass point nearest to the bearing. Bearings exactly halfway between two compass points should produce either N or S (never E or W). For example: Input of 73 is between N and E, but is closer to E, so the program should output E. Input of 45 is exactly halfway between N and E, so the program should output N.
So far this is what I have in dr. java. It will run, but it is not returning N S E and W for the entered values } public static void main (String [] args) { char direction; double userValue; userValue = 0-359; if (userValue >= 315 && userValue <= 359 || userValue >=0 && userValue <= 45) direction = 'N'; if (userValue >= 46 && userValue <= 134) direction = 'E'; if (userValue >= 135 && userValue <= 225) direction = 'S'; if (userValue>= 226 && userValue <= 314) direction = 'W'; else direction = '?'; } }
I'm assuming you have a class header above the method header? ie: public class <name>
yes
Also the variable userValue has to have a single value- are you just giving it a range for readability?
yeah, the user is supposed to enter a value 0 - 359 and the program should return 'N' 'S' 'E' or 'W'
ok- the proper char value should be assigned to direction- if you do System.out.println(direction); after your else statement, it should print out properly?
ok so, after each else statement?
So, in your entire block of if statements, only one of them will execute- this depends on what the the user inputs in the variable userValue. After all of your code ( but before the 2 closing bracuts) you know that the right value is assigned to direction. Now, you just need to display the direction- which requires one extra line of code. To display a variable, you have to follow this syntax: System.out.println(enter variable here); so, doing that after your code, and before the 2 closing bracuts, and it should display the proper value.
so in the place where you put (enter variable here) I would put (direction) ?
correct- also, within those same bracuts, you can use that syntax to display whatever you want.. such as--- System.out.println("Hello World"); this will display the famous 'hello world' to he user. But to display a viariable in that statement, you don't need the quotation marks- so to display your variable, you can do-- System.out.println(direction); -- To combine the variable and some text of your own, you can make the program more user friendly-- by doing something like-- System.out.println(userValue + " Degrees Points " + direction + " On a Compass.");
the + operator (an operator performs an operation), in this case, is needed to display both text in quotation marks, and the variables -- it connects them.
Thank you!!! I just tried that and my program compiled and everything but for some reason, when I enter the value, it still just returns that exact value instead of the letter for the corresponding direction. So when I run it is says: run In -359.0 Degrees Points ? On a Compass. > 227 227
Is this normal?
Do you think userValue = 0-359; may be causing me a problem?
no- I found the issue
If you type in 230 in your program, it should return 'W', right?
No actually it still returns 230 which is strange.
is your last line of code like this: System.out.println(direction); ?
I tried it like this: System.out.println(userValue + " Degrees Points " + direction + " On a Compass."); what you suggested
The issue is with your else statement-- here is the logic flow of your program:: if (in this range) direction = N if(in this range) direction = E ect... if(in this range) direction = W else direction = ? So-- the last 2 statements are an if- else statement. so if you enter a West value, it will be west, and if you don't, it will always be equal to what's in the else statement. Even if you enter a North value, or a East value, the else statement of direction = ? will always execute
To make a block of if statements- where only one executes, and the rest are ignored, you need slightly different syntax. an else statement will always execute if the ONE if statement before it is false. So you want to make it so that the moment one of the directions are true, it will skip the last else statement. if - else - if statement is what you should have. In this type of statement, (the compiler will always execute the code from top to bottom) the first one that is true will execute, then the rest are ignored.
To use this type of statement- the syntax is as follows: if(userValue is in N range) direction = N else if(userValue is in E range) direction = E else if(userValue is in S range) direction = S else if(userValue is in W range) direction = W else direction = ?
Let me try that
lemme know if it makes sense too lol. It's important that you understand the why behind the logic
yes, I understand what you were explaining about the if else statements, thank you :) !!! but I just tried that and am still getting the same value returned so I am thinking there might be something else wrong with it
well, hit me up with your new syntax- I'll take a look at it
& good, I'm glad it makes sense
Here is my complete syntax for this program: import java.io.*; import java.text.*; public class In { static InputStreamReader r = new InputStreamReader(System.in); static BufferedReader br = new BufferedReader(r); // Read a String from standard system input public static String getString() { try { return br.readLine(); } catch (Exception e) { return ""; } } // Read a Number as a String from standard system input // Return the Number public static Number getNumber() { String numberString = getString(); try { numberString = numberString.trim().toUpperCase(); return NumberFormat.getInstance().parse(numberString ); } catch (Exception e) { // If any exception occurs, just return zero return new Integer(0); } } // Read an int from standard system input public static int getInt () { return getNumber().intValue(); } // Read a long from standard system input public static long getLong () { return getNumber().longValue(); } // Read a float from standard system input public static float getFloat () { return getNumber().floatValue(); } // Read a double from standard system input public static double getDouble () { return getNumber().doubleValue(); } // Read a char from standard system input public static char getChar () { String s = getString(); if (s.length() >= 1) return s.charAt(0); else return '\n'; } public static void main (String [] args) { char direction; double userValue; userValue = 0-359; if (userValue >= 315 && userValue <= 359 || userValue >=0 && userValue <= 45) direction = 'N'; else if (userValue >= 46 && userValue <= 134) direction = 'E'; else if (userValue >= 135 && userValue <= 225) direction = 'S'; else if (userValue>= 226 && userValue <= 314) direction = 'W'; else direction = '?'; System.out.println(userValue + " Degrees Points " + direction + " On a Compass."); } }
Trying the bottom section of your program, it works perfectly for me-- which means that something outside of it or something in the method header as it relates to the whole program is wrong. But this syntax alone works: I didn't change anything- except for the class header which is the same as my program's name. public class dog { public static void main (String [] args) { char direction; double userValue; userValue = 44; if (userValue >= 315 && userValue <= 359 || userValue >=0 && userValue <= 45) direction = 'N'; else if (userValue >= 46 && userValue <= 134) direction = 'E'; else if (userValue >= 135 && userValue <= 225) direction = 'S'; else if (userValue>= 226 && userValue <= 314) direction = 'W'; else direction = '?'; System.out.println(userValue + " Degrees Points " + direction + " On a Compass."); } }
I'd have a closer look at the other sections of your program, but I'd hardly know any of that syntax lol. I'm learning an entire java book.. only a couple hundred pages in so far.
Lol the first part of the syntax is an "In" file which just came from a site and makes the program work but starting with the public static void line and down is the code that I actually added.
Don't you have an IDE like eclipse / netbeans / jgrasp to write your programs? Or is this some website for schoolwork
yeah I have Dr. Java and I am using the eclipse compiler
I think it might have something to do with me having done the assignment statement for direction = 0-359; and maybe I need a print statement up there or something prompting the user to enter a value?!
oh you weren't changing that value each time you ran the program? if you ran it with userValue = 0-359; literally, it would always return the same thing. it would always start the program with userValue = -359; because 0-(minus)359 = -359
Ya, try changing that value to 22 or 55 or 232 or 355 and see how it works-- ie: userValue = 22;
if you want a random value assigned to userValue in that range- I can show you that as well
OHHHH THANK YOU !!!!!!!! THAT WORKS ! :):):)
AWESOME!
lol
:D
Thank you so much for your help! My teacher could not even explain as well as you did ;)
ya, most of the teachers I've had don't even like their job lol. They just throw textbooks at you and say learn this ect.. If you don't like something, or don't understand where the student is coming from, you can't teach effectively-- thanks for the complement- see ya sciencewiz!
see ya! And thanks :D
Join our real-time social learning platform and learn together with your friends!