简体   繁体   中英

How do you call a scanner variable from main into another method?

public static void main(String[] args) {

    //Scanner declaration and other stuff
    //...
    System.out.println("Enter price in $ (0.00 to 1000.00)");
    int price1 = scan.nextInt();
    scan.nextLine();
    System.out.println("Enter user rating (0 to 5)");
    int userRating1 = scan.nextInt();
    scan.nextLine();

    System.out.println("Enter price in $ (0.00 to 1000.00)");
    int price2 = scan.nextInt();
    scan.nextLine();
    System.out.println("Enter user rating (0 to 5)");
    int userRating2 = scan.nextInt();
    scan.nextLine();

}

public static void compare(camera camOne, camera camTwo) {
    int value1 = camOne.computeValue();
    int value2 = camTwo.computeValue();
    /*
     * if(camOne.computeValue() == camTwo.computeValue() && userRating1 ==
     * userRating2)
     */
}

How would I be able to call the price or the userRating inputs into the compare method, where I would like to compare the values?

You wouldn't use Scanner inside of the compare method. You're comparing two camera objects (which should be renamed Camera), and the Camera objects that you wish to compare should be fully formed before entering the method, and so there should be no need to use a Scanner inside the method.

public static void main(String[] args) {

    //Scanner declaration and other stuff
    //...
    System.out.println("Enter price in $ (0.00 to 1000.00)");
    int price1 = scan.nextInt();
    scan.nextLine();
    System.out.println("Enter user rating (0 to 5)");
    int userRating1 = scan.nextInt();
    scan.nextLine();

    System.out.println("Enter price in $ (0.00 to 1000.00)");
    int price2 = scan.nextInt();
    scan.nextLine();
    System.out.println("Enter user rating (0 to 5)");
    int userRating2 = scan.nextInt();
    scan.nextLine();

    // not sure what constructor Camera has
    Camera camera1 = new Camera(....);
    Camera camera2 = new Camera(....);

    int result = compare(camera1, camera2);

    System.out.println("Result is: " + result);

}

// shouldn't this return an int? also camera should be renamed Camera
public static int compare(Camera camOne, Camera camTwo) {
    int value1 = camOne.computeValue();
    int value2 = camTwo.computeValue();
    /*
     * if(camOne.computeValue() == camTwo.computeValue() && userRating1 ==
     * userRating2)
     */

    int result = Integer.compare(value1, value2);

    return result; // 0 for the same, 1 for first one > second, -1 for opposite
}

Also, you ask:

How would I be able to call the price or the userRating inputs into the compare method, where I would like to compare the values?

Assuming that your Camera class (again, rename it from "camera" to "Camera" to comply with Java naming conventions) has both a getPrice() and a getUserRating() method, then you'd call those methods on your camOne and camTwo Camera parameters inside of the compare(...) method. If you need to compare doubles, I suggest that you use the Double.compare(double d1, double d2) method, and if you need to compare ints, then use the Integer.compare(int i1, int i2) method.


For example, using a Comparator,

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class TestCamera {
   public static void main(String[] args) {
      ArrayList<MyCamera> cameraList = new ArrayList<>();

      // We'll pretent to use Scanner here to get values

      cameraList.add(new MyCamera("Sony", 8, 250.00));
      cameraList.add(new MyCamera("Olympus", 7, 450.0));
      cameraList.add(new MyCamera("Nikon", 10, 400.0));
      cameraList.add(new MyCamera("Fuji", 7, 450.50));

      System.out.println("Pre-sorted list:");
      for (MyCamera myCamera : cameraList) {
         System.out.println(myCamera);
      }
      System.out.println();      
      System.out.println("Post-sorted list:");
      Collections.sort(cameraList, new MyCameraComparator(false));
      for (MyCamera myCamera : cameraList) {
         System.out.println(myCamera);
      }

   }
}

class MyCamera {
   private int rating;
   private double cost;
   private String name;

   public MyCamera(String name, int rating, double cost) {
      this.name = name;
      this.rating = rating;
      this.cost = cost;
   }

   public String getName() {
      return name;
   }

   public int getRating() {
      return rating;
   }

   public double getCost() {
      return cost;
   }

   @Override
   public String toString() {
      return "MyCamera [rating=" + rating + ", cost=" + cost + ", name=" + name
            + "]";
   }

}

class MyCameraComparator implements Comparator<MyCamera> {
   private boolean lowestToHighest = true;

   public MyCameraComparator() {
      // default constructor
   }

   public MyCameraComparator(boolean lowestToHighest) {
      this.lowestToHighest = lowestToHighest;
   }

   @Override
   public int compare(MyCamera cam1, MyCamera cam2) {
      int finalResult = 0;
      int ratingValue = Integer.compare(cam1.getRating(), cam2.getRating());
      if (ratingValue != 0) {
         finalResult = ratingValue;
      } else {
         finalResult = Double.compare(cam1.getCost(), cam2.getCost());
      }

      if (lowestToHighest) {
         return finalResult;
      } else {
         return -finalResult;
      }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM