package week7;
import java.util.Scanner; public class IsBetween {
public static void main(String[] args) {
int num1 = 0;
int num2 = 0;
int num3 = 0;
boolean valid = true;
Scanner sc = new Scanner(System.in);
System.out.print("Please enter the low number:");
num1 = sc.nextInt();
System.out.print("Please enter the number to be tested (the between number):");
num2 = sc.nextInt();
System.out.print("Please enter the high number:");
num3 = sc.nextInt();
}
public static boolean inrange(int num1,int num2,int num3) {
boolean inrange = false;
if (num2 < num1 );
if (num2 > num3);
return inrange;
the last codes do not execute why?
CodePudding user response:
the method inrange is never called.
there is a difference between declaring a method and using it.
in your case you declared a method inrange that at the moment will always return false but I assume later on you plan to make it do the comparison.
you have to actually call it to use it. after
num3 = sc.nextInt();
you can add
boolean result = inrange(num1, num2, num3);
so I think where you are going wrong is thinking that declaring inrange with num1, num2, num3 as the same names as the variables are named creates a link. it does not. You can actually name them differently
public static boolean inrange(int first, int second, int third) {
// implementation goes here
return false;
}
and call them with
num3 = sc.nextInt();
inrange(num1, num2, num3);
and num1 will be put into the variable first, num2 will be put into the variable second.
the parameternames for inrange are only important for the duration of the inrange method. They get created as little containers in which you can dump the values you want to work with and inside the { } of the inrange method they are available but outside of the method they will not be visible. so you can actually have multiple methods use the same variable names but they don't overlap/get in eachothers way.
public static int add3(int number) {
return number 3;
}
public static int sub3(int number) {
return number - 3;
}
int age = 5;
// when you call these:
System.out.println(add3(age)); // prints 8
System.out.println(sub3(age)); // prints 2
In the beginning when you are learning it might be easy to view variables as boxes that you put values into. when you call a method you drop the contents of the age bucket(which contains 5) into the 'number' bucket of the add3 method. then you start the machine. similarly when you call the sub3 method (which is separate entirely) you dump the value of the bucket (which is still 5) into the number bucket of sub3.
whatever label is on the bucket only matters inside the { } that they are attached to. in the case of your inrange it is the inrange method block. In the case of the variables inside main it is the main method block.
CodePudding user response:
In addition to not calling inrange
, its implementation is incorrect.
public static boolean inrange(int num1,int num2,int num3) {
boolean inrange = false;
if (num2 < num1 );
if (num2 > num3);
return inrange;
}
Your if
statements do nothing, because they do not contain a statement.
An if statement can be written:
if (x == 1)
System.out.println("true");
So the body of the if statement is a single statement.
That is not the same as:
if (x == 1);
System.out.println("true");
Where the extra ;
gives the if statement an empty body. The println will always be executed in this case.
Because this can be tricky to get right, you should always write an if statement (or while, for etc.) with a block for a body:
if (x == 1) {
System.out.println("true");
}
CodePudding user response:
you have writen the wrong code, right implementation is:
public static void main(String[] args) {
int num1 = 0;
int num2 = 0;
int num3 = 0;
boolean valid = true;
Scanner sc = new Scanner(System.in);
System.out.print("Please enter the low number:");
num1 = sc.nextInt();
System.out.print("Please enter the number to be tested (the between number):");
num2 = sc.nextInt();
System.out.print("Please enter the high number:");
num3 = sc.nextInt();
System.out.println(inrange(num1,num2,num3);}
public static boolean inrange(int num1,int num2,int num3) {
if (num2 > num1 && num2 < num3);
return true;
return false;}