簡體   English   中英

我如何使用 epsilon 的聲明值來檢查容許值?

[英]how can i use the declared value of epsilon to check the tolerated values?

我想使用 epsilon 來檢查用戶輸入的重量是否可以從 15.50 - epsilon 到 15.50 + epsilon 的小值和從 25.25 - epsilon 到 25.25 + epsilon 的大值

    import java.util.Scanner;

public class ShippedItem {
    private int size; 
    private double weight; 
    public static final int SMALL = 1;
    private static final double SMALL_WEIGHT = 9.25; 
    public static final int BIG = 2;
    private static final double BIG_WEIGHT = 15.75; 
    private static final double EPSILON = 0.01;
    public ShippedItem() {
        this(SMALL, SMALL_WEIGHT);
    }

    
    public ShippedItem(int size, double weight) {
        this.size = size;
        this.weight = weight;
    }
    
    
    public int getSize() {
        return size;
    }
    
    
    public void setSize(int size) {
        this.size = size;
    }
    
    
    public double getWeight() {
        return weight;
    }
    
    
    public void setWeight(double weight) {
        this.weight = weight;
    }


    public boolean isShippedItemAcceptable() {
        boolean result = false;
        
        if(size == SMALL) return Math.abs(SMALL_WEIGHT - weight) <= EPSILON;
         if (size == SMALL) return Math.abs(SMALL_WEIGHT + weight) <= EPSILON;
         else
        if(size == BIG) return Math.abs(BIG_WEIGHT - weight) <= EPSILON;
        if(size == BIG) return Math.abs(BIG_WEIGHT + weight) <= EPSILON;
        return result;
    }
    
     




public static class User {
    
    private Scanner keyboard = new Scanner(System.in);
    

    public int inputInteger() {
        int value = keyboard.nextInt();
        keyboard.nextLine();
        return value;
    }
    

    public int inputInteger(String message) {
        System.out.println(message);
        int value = inputInteger();
        return value;
    }
    
    
    public double inputDouble() {
        double value = keyboard.nextDouble();
        keyboard.nextLine();
        return value;   
    }
    

    public double inputDouble(String message) {
        System.out.println(message);
        double value = inputDouble();
        return value;
    }





    public static void main(String[] args) {
        ShippedItem item = new ShippedItem();
        User user = new User();
    
        int itemSize;
        boolean result;
        double itemWeight;
        int acceptable = 0;
        int unacceptable = 0;
        int size = 0;
    
        
        
        
        System.out.println("enter bag size:");
        System.out.println("1 for regular");
        System.out.println("2 for large");
        Scanner keyboard = new Scanner(System.in);
        itemSize = keyboard.nextInt();

        
    System.out.println("enter weight:");
    itemWeight = keyboard.nextDouble(); 
    item.setWeight(itemWeight);
    result = item.isShippedItemAcceptable();
    System.out.println(result);
    
    System.out.println(item.isShippedItemAcceptable() ? "acceptable" : "unacceptable");
    }
}

}

它只適用於小物品,但對於大物品,它的輸出對於所有重量都是不可接受的

檢查重量與尺寸可接受的重量之間的絕對差異是否小於 epsilon。

if(size == SMALL) return Math.abs(SMALL_WEIGHT - weight) <= EPSILON;
if(size == BIG) return Math.abs(BIG_WEIGHT - weight) <= EPSILON;
return false; // maybe throw Exception for invalid size

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM