/*1st - What I want to do is have the user input 2 numbers. Program should determine which is lowest and highest, then spit it out. Then I’m trying to have the user input a 3rd number that is equal to or in between the lowest number and highest number. This is the part where I get stuck. Any suggestions? I know it has to be some type of while loop.*/

import java.util.*;
class noob{
public static void main(String[] args) {

Scanner console = new Scanner (System.in);



System.out.println("Enter first number");
int a = console.nextInt();
System.out.println("Enter second number");
int b = console.nextInt();

int lowestNumber = lowest(a,b);
int highestNumber = highest(a,b);


System.out.println("The lowest number is " + lowestNumber);
System.out.println("The highest number is " + highestNumber);

System.out.println("Choose a number betweem " +lowestNumber+" and " +highestNumber+".");
int c = console.nextInt();




}

public static int lowest(int a, int b) {

  if (a > b) {
    return b;
  } else {
    return a;
  }
}

public static int highest(int a, int b) {

  if (a > b) {
  return a;
  } else {
    return b;
  }
}

}

Recommended Answers

All 6 Replies

You don't really need a lot of the things you have here.
I have modified what you have there slightly, take a look and see if you understand everything.

import java.util.Scanner;
class noob{


private static int highest,lowest,a,b,c;

public static void main(String[] args) {

noob nb = new noob(); // You should not make all your methods static to access them from main, you should create an instance of your class and call the method using classname.method()
Scanner console = new Scanner (System.in);

System.out.println("Enter first number");
a = console.nextInt();
System.out.println("Enter second number");
b = console.nextInt();

nb.findDifference(a,b);//calls this non-static method using a noob object


System.out.println("The lowest number is " + lowest);
System.out.println("The highest number is " + highest);

do{
System.out.println("Choose a number betweem " +lowest+" and " +highest+".");
c = console.nextInt();
if(c>highest || c < lowest)
{
System.out.println("hey thats not in between!!");

}
else{
System.out.println("Good Job!" + c +" is between "+lowest+" and "+highest);

}
}while(c>highest || c < lowest);




}
public void findDifference(int x, int y){

if(x > y){
highest = x;
lowest = y;}
else{
highest = y;
lowest=x;}

}


}

Also, if you didn't notice, if you want to call methods from main that are not static, you need to use the actual class to call it. noob nb = new noob(); effectively makes nb an instance of this class and can therefore access all methods. Then to call the methods use nb.method()....you should not change a methods scope/properties just because its the easiest thing to do, because more than likely, that is the wrong way to do it

Got it. Thanks. It was the while (c>highest || c < lowest) part that I was looking for. I'm not familiarized with the some of the syntax in your code but that part helped me out. Thanks.

Got it. Thanks. It was the while (c>highest || c < lowest) part that I was looking for. I'm not familiarized with the some of the syntax in your code but that part helped me out. Thanks.

read the bold that I edited into my post. It's pretty useful to know/realise.

what exactly does the "private static int highest,lowest,a,b,c;" at the top do??

is it just declaring all of the variables?

what exactly does the "private static int highest,lowest,a,b,c;" at the top do??

private sets the variables to have access only in this class.

When a variable is declared with the static keyword, its called a “class variable”. All instances share the same copy of the variable. A class variable can be accessed directly with the class, without the need to creat a instance.

Without the “static” keyword, it's called “instance variable”, and each instance of the class has its own copy of the variable.

and if you want to create multiple variables of the same type, you can just separate the variable names with , like the example. e.g. String name,address; is the same as
String name;
String address;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.