Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Java中的扫描仪输入获取最大值和最小值_Java_Max_Min - Fatal编程技术网

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

从Java中的扫描仪输入获取最大值和最小值,java,max,min,Java,Max,Min,在这个租赁程序中,我需要显示用户的最高和最低租金。该程序适用于其他方法,但它同时显示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");

在这个租赁程序中,我需要显示用户的最高和最低租金。该程序适用于其他方法,但它同时显示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循环,它计算最大租金,但不计算最小值。非常感谢您的帮助。

对于初学者,您正在从相同的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();
        }
    }
谢谢@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

我猜您正在覆盖3个客户的数据,在第一个for循环中,details=new CarRental;每次覆盖以前的数据时。尝试将其存储在列表中,检查所有数据是否正确存储,然后向我们展示查找最大值和最小值的逻辑,现在编写大量代码以供阅读和理解谢谢。我已删除声明详情=新的CarRental;并添加了一个do-while循环,但仍然不起作用。如果使用数组存储值,程序运行良好,但在这种情况下,我必须在没有数组的情况下工作。