从For循环调用数组

时间:2014-06-28 20:31:57

标签: java arrays

我正在学习java,我需要帮助调用数组,而它在For循环中,这是我的数组代码。 请求整个代码,对不起如果它的草率或编码错误

    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="";

                Scanner input = new Scanner( System.in );
                Scanner user_input = new Scanner( System.in );

  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); 
         double[] array = new double[12]; 
         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;
           default:
             System.out.println("Invalid selection");
             break; 
            }
         }
       }while (option!=0);
       }
       }

现在我需要在不同的情况下显示12个不同的用户输入数字,因为这都是切换方法的一部分。有没有办法做到这一点,我尝试过非常简单的代码,如

          System.out.println("Rental for month 1: "+i(1));

但它说&#34;找不到符号方法i(int)&#34;

我只想宣布他们,有什么办法吗?

2 个答案:

答案 0 :(得分:2)

试试这个 - &gt; 这将打印每个月的租金..

for(int i = 0; i < 12; i++)
    System.out.println("Rental for month " + (i+1) + " : " + array[i]);

应该在代码中再做一次编辑..

     double[] array = new double[12]; 

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

答案 1 :(得分:0)

System.out.println("Rental for month 1: "+i(1));

这里i是什么?看起来像你没有的方法。您必须在循环中执行以下操作

System.out.println("Rental for month " + (i+1) + " : " + array[i]);