使用Loop打印每月租金,门槛

时间:2014-06-29 21:07:43

标签: java loops threshold

使用循环打印数组中每月租金低于用户输入的阈值

这是我从讲师那里得到的一个问题,我需要帮助,因为我根本不知道从哪里开始。这是我完整的程序代码,这是最后一种情况。

import java.util.Scanner ;
public class jakeGrim {

  public static void  main(String[] args) {
                // Local variable

                int option;
                String squareFootage="";

                int noBed = 0;

                double totalSum =0;

                String propertyCode="";

                String propertyType="";

                String threshold="";


                Scanner input = new Scanner( System.in );
                Scanner user_input = new Scanner( System.in );
                double[] array = new double[12]; 

  do{
            // Display menu graphics
            System.out.println(" ");
            System.out.println("|  *****Rental Menu*******   |");
            System.out.println("|        1. Enter rental property Details     ");
            System.out.println("|        2. Enter monthly rent ( 12 Months )       ");
            System.out.println("|        3. Display Annual Rent");
            System.out.println("|        4. Display rental report       ");
            System.out.println("|        5. Display Monthly rents falling below a certain threshold       ");
            System.out.println(" ");
            System.out.println(" Please Select an option: ");
            option = input.nextInt();


  {

    switch (option) {


   case 1:


            System.out.println("Enter Rental Details: ");
            System.out.println("Property Code:            ");
            propertyCode = user_input.next();
            System.out.println("Property Type:            ");
            propertyType = user_input.next();
            System.out.println("Square Footage:           ");
            squareFootage = user_input.next();
            System.out.println("Number Of bedrooms        ");
            noBed = input.nextInt();
            break;


      case 2:

     {
         Scanner keyboardScanner = new Scanner(System.in); 

         for (int i = 0; i < 12; i++) {
          System.out.println("Enter Rental for month[" +( i +1)+ "]");
          array[i] = keyboardScanner.nextDouble();
}




        //So now, we need to do something with that array and sum up all the values in that array. 
        for (int i = 0; i < array.length; i++){
          System.out.println(array[i]);

         totalSum += array[i];
}
}




 break;


     case 3:
         System.out.println("The annual rent for propery code "+propertyCode+" is:  " +(totalSum));


break;

    case 4:

        System.out.println(" Property Code:      "+propertyCode);
        System.out.println(" Property Type:      "+propertyType);
        System.out.println(" Square Footage:     "+squareFootage);
        System.out.println(" Number of Bedrooms: "+noBed);
        System.out.println("");
        System.out.println("");
        for(int i = 0; i<12; i++)
            System.out.println("Rental for month " + (i+1) + " : " + array[i]);


    case 5:

         Scanner user_input = new Scanner( System.in );

        System.out.println("Enter the Rental Threshold: ");
        threshold = user_input.next();

        System.out.println("


    break;
    default:
        System.out.println("Invalid selection");
      break; 
     }
  }
}while (option!=0);
}
}

1 个答案:

答案 0 :(得分:0)

嗯,你的程序有几个缺陷,应该改进以提高性能。

  1. 切换前不需要大括号。所以,在切换语句之前删除大括号{

  2. 同样,案例2中不需要大括号{,而案例2的}陈述之前不需要break。你最好保持原样。

  3. 将以前的数据删除后,在case 5:中添加以下代码行。

    case 5:
       //  Scanner user_input = new Scanner( System.in );
    System.out.println("Enter the Rental Threshold: ");
    threshold = user_input.next();
    for(int i=0;i<array.length;i++){
    if(Integer.valueOf(threshold)>array[i])
    System.out.println("Month "+(i+1)+" has the rent falling below the threshold range as the rent is "+array[i]);
    }
    System.out.println("");
    break;
    
  4. 最后,您只需编辑待办事项条件,将其设为do{...}while(option>=1 && option<=5);

  5. 此外,事实上,请尝试正确缩进代码以使其更好!它会让其他人通过您的代码并真诚地帮助您!

  6. 正确的工作代码: -

           import java.util.Scanner;
           public class jakeGrim {
           public static void main(String[] args) {
           int option;
           String squareFootage="";
           int noBed = 0;
           double totalSum =0;
           String propertyCode="";
           String propertyType="";
           String threshold="";
           Scanner input = new Scanner( System.in );
           Scanner user_input = new Scanner( System.in );
           double[] array = new double[12]; 
           do{
            // Display menu graphics
            System.out.println(" ");
            System.out.println("|  *****Rental Menu*******   |");
            System.out.println("|        1. Enter rental property Details     ");
            System.out.println("|        2. Enter monthly rent ( 12 Months )       ");
            System.out.println("|        3. Display Annual Rent");
            System.out.println("|        4. Display rental report       ");
            System.out.println("|        5. Display Monthly rents falling below a certain threshold       ");
            System.out.println(" ");
            System.out.println(" Please Select an option: ");
            option = input.nextInt();
    
              switch (option){
            case 1:
            System.out.println("Enter Rental Details: ");
            System.out.println("Property Code:            ");
            propertyCode = user_input.next();
            System.out.println("Property Type:            ");
            propertyType = user_input.next();
            System.out.println("Square Footage:           ");
            squareFootage = user_input.next();
            System.out.println("Number Of bedrooms        ");
            noBed = input.nextInt();
            break;
    
            case 2:
    
            Scanner keyboardScanner = new Scanner(System.in); 
            for (int i = 0; i < 12; i++) {
            System.out.println("Enter Rental for month[" +( i +1)+ "]");
            array[i] = keyboardScanner.nextDouble();
             }
           //So now, we need to do something with that array and sum up all the values in that array. 
            for (int i = 0; i < array.length; i++){
             System.out.println(array[i]);
            totalSum += array[i];
            }
            break;
    
            case 3:
            System.out.println("The annual rent for propery code "+propertyCode+" is:  " +totalSum);
            break;
    
            case 4:
            System.out.println(" Property Code:      "+propertyCode);
            System.out.println(" Property Type:      "+propertyType);
            System.out.println(" Square Footage:     "+squareFootage);
            System.out.println(" Number of Bedrooms: "+noBed);
            System.out.println("");
            System.out.println("");
            for(int i = 0; i<12; i++)
            System.out.println("Rental for month " + (i+1) + " : " + array[i]);
            break;
    
            case 5:
            //    Scanner user_input = new Scanner( System.in );
            System.out.println("Enter the Rental Threshold: ");
            threshold = user_input.next();
            for(int i=0;i<array.length;i++){
            if(Integer.valueOf(threshold)>array[i])
            System.out.println("Month "+(i+1)+" has the rent falling below the threshold range as the rent is "+array[i]);
            }
            System.out.println("");
            break;
    
            default:
            System.out.println("Invalid selection");
            break; 
    
           }
           } while (option>=1 && option<=5);
         }
        }