从Java中的扫描仪输入获取最大值和最小值

时间:2015-04-21 21:46:09

标签: java max min

在此租赁计划中,我需要向用户显示最高和最低租金。该程序适用于其他方法,但它显示最大值和最小值的最大值。这是我写的代码:

import java.util.Scanner; // program uses Scanner

public class CarRentalTest {
    public static void main(String[] args) {
        System.out.println("Welcome to Rental Portal");

        Scanner input = new Scanner(System.in); // create Scanner to obtain
                                                // input from command window

        CarRental details = new CarRental();
        int N = 8; // Total number of passengers
        String summary =
                  "\t\t\t Summary of Car Rentals \t\n"
                + "\t\t\t========================================\n"
                + "\tName \t\t Days \t\t Special Offer \t\t Charge \n";
        double maxRent = 0.0;
        double minRent = 0.0;
        double temp = 0.0;
        int count = 0;
        int count1 = 0;
        int days = 0;
        String high = "";
        String low = "";

        for (int i = 0; i <= 2; i++) {
            details = new CarRental();
            System.out.println("Enter the name of Passenger: ");
            details.passenger = input.next(); // read customer name
            details.setName(details.passenger);
            System.out.println("Enter Number of days you wish to rent a car: ");
            details.days = input.nextInt(); // read number of days
            if (days > 365) {
                System.out.print("\nNumber of Days must not exceed 365");
                System.out.print("\nPlease enter number of days again: ");
                details.days = input.nextInt();
                details.setDays(details.days);
            }
            details.getRent();
            System.out.println("Do you want to use the special offer?");
            details.userResponse = input.next();
            System.out.println("The total amount of rent for "
                    + details.getName() + "  is: $" + details.getRent());
            summary = summary + "\t" + details.getName() + " \t\t "
                    + details.getDays() + " \t\t   " + details.userResponse
                    + " \t\t\t $" + details.getRent() + "\n";
            System.out
                    .println("-----------------------------------------------");
        }
        System.out.println(summary + "\n");
        System.out.println("-------------------------------------------------");
        for (int i = 0; i < N; i++) {
            do {

                details.cost = details.getRent();
                if (minRent > details.cost)
                    minRent = details.cost;
                if (maxRent < details.cost)
                    maxRent = details.cost;
            } while (details.cost == 0);
        }
        System.out.println("The customer spending most rental is" + high + "  "
                + maxRent);
        System.out.println("The customer spending least rental is" + low + " "
                + minRent);
        System.out.println();
        for (int i = 0; i < N; i++) {
            if (details.days < 7) {
                count = count + 1;
            } else {
                count1 = count1 + 1;
            }
        }
        System.out.print("The rental days < 7: ");
        for (int j = 0; j < count; j++) {
            System.out.print("*");
        }
        System.out.println();
        System.out.print("The rental days >= 7: ");
        for (int j = 0; j < count1; j++) {
            System.out.print("*");
        }
    }
}// end method main

我添加了一个do-while循环,它计算最大租金但不计算最小值。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

对于初学者,您正在从相同的details.getRent()方法中检索minRent和maxRent的值....因此它们是相同的......

for (int i = 0; i < N; i++) {
        if (maxRent < details.getRent()) {
            maxRent = details.getRent();
            high = details.getName();
        }
        if (i == 0) {
            temp = details.getRent();
        }
        if (temp >= details.getRent()) {
            minRent = details.getRent();
            temp = details.getRent();
            low = details.getName();
        }
        if (maxRent < details.getRent()) {
            maxRent = details.getRent();
            high = details.getName();
        }
    }

答案 1 :(得分:0)

感谢 @ TheJavaCoder16 代码是完全正确的,除了有问题的for循环放在摘要下面。这里是更正的代码。

    import java.util.Scanner; // program uses Scanner
    public class CarRentalTest {
    public static void main( String[] args )
    {
        System.out.println("\t\t ******Welcome to Rental Portal******");
        Scanner input = new Scanner( System.in );
        CarRental details=new CarRental(); // create a CarRental object
        int N=3; // Total number of passengers
        String summary="\t\t\t Summary of Car Rentals \t\n"+"\t\t========================================\n"+"\tName \t\t Days \t\t Special Offer \t\t Charge \n";
        double maxRent=0.0;//to display maximum rent value
        double minRent=0.0; //To display minimum rent value
double temp=0.0; // A temp variable to compare minimum and maximum rent values
  int count1=0; // Variable to count days 
  int count2=0; //Variable to count days
  String high=""; // Displays user with highest rent 
  String low=""; // Displays user with least rent

  for(int i=0;i<N;i++){ //Reading data from Scanner and Calculating rent

      System.out.println( "Enter the name of Passenger: " ); // prompt for input
      details.passenger = input.next(); // read customer name
      details.setName(details.passenger); //set passenger name

      System.out.println( "Enter Number of days you wish to rent a car: " ); // prompt for input
      details.days = input.nextInt(); // read number of days
          if(details.days>365){
           // if user enters invalid number of days ask for entering again
           System.out.print("\nNumber of Days must not exceed 365");
           System.out.print("\nPlease enter number of days again: ");
           details.days=input.nextInt();
           details.setDays(details.days);
          }
      details.getRent(); //calculate rent
      System.out.println( "Do you want to use the special offer?" ); // prompt for input
      details.userResponse = input.next();// read user response and based on that calculate rent
      System.out.println("The total amount of rent for " +details.getName()+ "  is: $"+details.getRent());
      summary=summary+"\t"+details.getName()+" \t\t "+details.getDays()+" \t\t   "+details.userResponse+" \t\t\t $"+details.getRent()+"\n";

      System.out.println("-----------------------------------------------");

    //Calculate the maximum and minimum rent values
      // and the associated passenger names
      for(int j=0;j<=N;j++){ 
        if(maxRent < details.getRent()){
            maxRent = details.getRent();
            high = details.getName();
        }

        if(i==0){
            temp = details.getRent() ;
        }
        if(temp >= details.getRent()){
            minRent = details.getRent();
            temp = details.getRent();
            low = details.getName();
        }

        if(maxRent < details.getRent()){
            maxRent = details.getRent();
            high = details.getName();
        }
    }
  }
System.out.println(summary+"\n");
System.out.println("-------------------------------------------------");
System.out.println("The customer spending most rental is "   +high+ "  "+maxRent);
System.out.println("The customer spending least rental is "   +low+ " "+minRent);
System.out.println();
}
}//end method main