简体   繁体   English

如何将扫描程序变量从main调用为另一种方法?

[英]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? 如何在比较方法中将价格或userRating输入调用到compare方法中?

You wouldn't use Scanner inside of the compare method. 您不会在compare方法内使用Scanner。 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. 您正在比较两个相机对象(应重命名为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
}

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? 如何在比较方法中将价格或userRating输入调用到compare方法中?

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. 假设您的Camera类(同样,将其从“ camera”重命名为“ Camera”以符合Java命名约定)同时具有getPrice()getUserRating()方法,则可以在camOne和camTwo上调用这些方法compare(...)方法内部的相机参数。 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. 如果需要比较双精度数,建议您使用Double.compare(double d1, double d2)方法;如果需要比较int,请使用Integer.compare(int i1, int i2)方法。


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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