I see some errors in your code.
Your probably meant the mathematical term
90 <= angle <= 180, meaning angle in range 90-180.
if (angle >= 90 && angle <= 180) {
// do action
}
Answer from AlexWien on Stack OverflowI see some errors in your code.
Your probably meant the mathematical term
90 <= angle <= 180, meaning angle in range 90-180.
if (angle >= 90 && angle <= 180) {
// do action
}
You can use apache Range API. https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/Range.html
What you're trying to do is check if a number is >= 92 AND <= 100. That isn't done with a comma, like you have:
if ( pC >= 92, pC <= 100 ) {
System.out.print("\n You have an B");
}
Rather, the AND operator is && (and you said you want this range to be an A, rather than a B), which means your code would look like this:
if ( pC >= 92 && pC <= 100 ) {
System.out.print("\n You have an A");
}
I think what you are looking for is this:
If the point is between 92% and 100%:
if ( pC >= 92 && pC <= 100 ) {
System.out.print("\n You have an A");
}
If the point is out that range:
if ( pC < 92 ) {
System.out.print("\n You have an B");
}
Or, to the complete statement, you will want to make:
if ( pC >= 92 && pC <= 100 ) {
System.out.print("\n You have an A");
}
else if ( pC < 92 ){
System.out.print("\n You have an B");
}
else {
System.out.print("\n You have a point out of the curve");
}
public class Method1 {
Since this will be the name of the file too, (if you save it) I would recommend naming it something descriptive so that you don't have to open it in order to know what it hides.
public static float a, b, t;
public static Scanner scanner;
Try and declare as close to the use of the variables as possible. There is also no need for these to be globals, since they are only used in one place.
Next, read the question very carefully. It states that A and B are both integers, so a float is the wrong type to use. It may also give rounding errors, and comparing floats is trickier because of how floating point numbers are stored. 0.1 + 0.1 does not always equal 0.2, it may give 0.200000001, so this can lead to a bug.
Lastly, t is also going to be an integer. You can tell this is the case because it is a count of how many test cases there are, 2.3 testcases doesn't make sense.
HINT TOWARDS AN ERROR
System.out.println("Type your first Value please.");
Why is this here? Does the question ask for it in the output?
Now onto the code overview. Strictly speaking, your code doesn't work at all. You never actually took in all the input. If its input looks like
3
10 20
20 10
10 10
You scan 3, and 10. That is it. All the rest of the input is ignored. There is a reason it tells you how many test cases there will be, its important to use that information.
if (a == b) {
System.out.println('=');
}
if (a > b){
System.out.println('>');
}
if (a < b){
System.out.println("<");
}
This is the perfect time for if/else statements.
One good piece of logic here is if they aren't equal, and a isn't bigger than b, we know the answer to a < b without needing to check.
There are more things to mention, like moving the check if two numbers are equal to a method, and making use of the library of methods available, but get it working first, then post a follow up. Best of luck
Don't name anything Method1. It's never going to be a good name but in particular here you used it as the name of a class, which is extra confusing.
You should name a class after its use. Maybe something like OperatorChecker. A name should tell you what the class does, that makes it easier to understand when and how to use it.
One problem is that a ternary relational construct would introduce serious parser problems:
<expr> ::= <expr> <rel-op> <expr> |
... |
<expr> <rel-op> <expr> <rel-op> <expr>
When you try to express a grammar with those productions using a typical PGS, you'll find that there is a shift-reduce conflict at the point of the first <rel-op>. The parse needs to lookahead an arbitrary number of symbols to see if there is a second <rel-op> before it can decide whether the binary or ternary form has been used. In this case, you could not simply ignore the conflict because that would result in incorrect parses.
I'm not saying that this grammar is fatally ambiguous. But I think you'd need a backtracking parser to deal with it correctly. And that is a serious problem for a programming language where fast compilation is a major selling point.
Because that syntax simply isn't defined? Besides, x < y evaluates as a bool, so what does bool < int mean? It isn't really an overhead; besides, you could write a utility method if you really want - isBetween(10,x,20) - I wouldn't myself, but hey...
I highly doubt that a number will, at the same time, be greater than 21 and less than 6. You probably mean 26 instead of 6.
Because the value 21 is not less than 6
So when the line else if (number >= 21 && number < 6) { is evaluated, the program sees:
else if (21 >= 21 && 21 < 6) {
which becomes:
else if (true && false) {
which becomes
else if (false) {
If you change this line to say else if (number >= 21 && number > 6) { then you will see catch 4
NOTE: that would be the same as saying else if (number > 6) {
EDIT: based on the the title of your question it seems you are trying to check if number is between 6 and 21
if number can equal 6 or 21 that would look like:
else if(number <= 21 && number >= 6){
if number can not equal 6 or 21 that would look like:
else if(number < 21 && number > 6){
I'd do something like this:
int teenNb = 0;
int[] arr = {First, Second, Third};
for (int i : arr)
teenNb += (i < 20 && i > 10 ? 1 : 0);
switch (teenNb) {
case 1:
System.out.println("One of them is a teenager");
break;
case 2:
System.out.println("Two of them are teenagers");
break;
case 3:
System.out.println("All of them are teenagers");
break;
default:
System.out.println("None of them is a teenager");
break;
}
There may be more efficient or advanced solutions, but this works for me and is fairly simple and easy to understand.
It not easy to find the problem in your code, you can make a better solution by split your problem,
First create one method to check if one user is teenager or not, then user this function for each user and count how many of them are teenagers,
public static boolean isTeenager(int user) {
return user > 10 && user < 20;
}
public static void main(String[] args) {
int first = 0, second = 0, third = 0;
int[] users = {first, second, third};
int teenagersCount = 0;
for(int user : users)
if (isTeenager(user))
teenagersCount++;;
switch (teenagersCount) {
case 0: System.out.println("None of them are teenagers"); break;
case 1: System.out.println("One of them is teenager"); break;
case 2: System.out.println("Two of them are teenagers"); break;
default: System.out.println("All of them are teenagers"); break;
}
}