如何将一个扫描变量从main调用到另一个方法?

时间:2015-06-27 15:25:31

标签: java

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)
     */
}

我如何将价格或userRating输入调用到compare方法中,我想在哪里比较这些值?

1 个答案:

答案 0 :(得分:1)

您不会在比较方法中使用Scanner。您正在比较两个相机对象(应该重命名为Camera),并且在进入方法之前应该完全形成您想要比较的Camera对象,因此不需要在方法中使用扫描仪。

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
}

另外,你问:

  

我如何将价格或userRating输入调用到compare方法中,我想在哪里比较这些值?

假设您的Camera类(再次将其从“camera”重命名为“Camera”以符合Java命名约定)同时使用getPrice()getUserRating()方法,那么您将调用在compare(...)方法内的camOne和camTwo Camera参数上的那些方法。如果您需要比较双打,我建议您使用Double.compare(double d1, double d2)方法,如果需要比较整数,请使用Integer.compare(int i1, int i2)方法。

例如,使用比较器,

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;
      }
   }
}