【问题标题】:Use Loop to print out each monthly rent, Threshold [closed]使用循环打印出每个月的租金,阈值 [关闭]
【发布时间】:2014-08-20 05:51:01
【问题描述】:

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

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

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

【问题讨论】:

  • 把你的问题解释清楚...
  • 如果您“根本不知道从哪里开始”,那么您的问题不适合 Stack Overflow。
  • @BlorgBeard 这不是一个帮助编程时需要它的人的网站吗?是否是编码新手?
  • @JakeGrim 您应该尝试更好地解释为什么您的程序目前无法运行(您期望什么输出?您会得到什么?)和narrow down the code to the section that does not work as expected instead of posting 150 lines of code
  • 不,杰克,不是。这是一个供“专业和狂热的程序员”提出和回答具体编程相关问题的网站。请查看tour。特别是,“不要问......你还没有试图找到答案的问题(展示你的作品!)”

标签: java loops threshold


【解决方案1】:

嗯,你的程序有几个缺陷,应该改进以获得更好的性能。

  1. switch之前不需要大括号,所以,删除switch语句之前的大括号{

  2. 同样,在 case 2 中不需要大括号 {,在 case-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条件,设置为do{...}while(option&gt;=1 &amp;&amp; option&lt;=5);

  5. 另外,事实上,尽量适当地缩进你的代码,让它有更好的感觉!会让别人看懂你的代码,真心帮助你!

正确的工作代码:-

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

【讨论】:

  • @JakeGrim-既然我已经给了你答案,请在验证其正确性后投票并接受它,否则下次没有帮助!
  • "现在我已经给了你答案" --> 现在真的,你认为这在学术环境中是可以接受的吗?更重要的是,您认为通过 for 修复他的代码对 OP 有什么帮助吗?此外,您可以更好地解释您的更改,而不是说“做这个、这个和这个”。当您只是被告知要进行更改而没有被告知为什么时,很难学习。不过实话说。 不要为他们做任何人的工作。这对他们没有帮助,而且让人们对 Stack Overflow 的用途产生了错误的认识。
  • @user3580294-抱歉,谢谢提醒。这是我给出的最后一个答案,因为 OP 真的很关心这个问题。另见他的 cmets,他的问题也涉及代码。但是,没有真正的帮助从下次开始!
  • “真正关心这个问题”不是为某人工作的理由。无论他们有多担心,他们都有责任做好自己的工作。是的,我读过他的 cmets,是的,他的问题有代码。这并不意味着您应该为他提供复制/粘贴的代码。不过,这是一个很好的意图。
  • 好的@user3580294,我会从下一次开始处理这些事情!顺便说一句,这几乎不是我为属于这一类的人提供的第二次或第三次帮助。但是,从下一次开始,我会照顾 SO Professionals 的声誉。谢谢。
猜你喜欢
  • 1970-01-01
  • 2014-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
  • 1970-01-01
相关资源
最近更新 更多