如何使用多种方法创建一个类,然后将值传递回主方法

时间:2018-11-07 22:54:44

标签: java arrays methods

我正在尝试创建一个包含一些方法的类。我试图仅将用户输入的值传递给方法。然后,运行每种方法,并在main方法中打印值。这就是我所拥有的。

public class HotelRating{
   private int hotel = 0;
   private int years = 0;
   int [][] Ratingarray = new int [hotel][years];

   public HotelRating(){
   }


   public HotelRating(int hotel, int years){
   this.hotel = hotel;
   this.years = years;
        for (int row = 0; row < Ratingarray.length; row++){
         for (int column = 0; column < Ratingarray[row].length; column++){
            Ratingarray[row][column] = (int) (Math.random() * 5 + 1);
         }
      }
   }

   public String fiveStarsHotels(){
      String result = "";
      System.out.print("Five stars hotels indices: ");
      System.out.println();
      for (int row = 0; row < Ratingarray.length; row++){
         for (int column = 0; column < Ratingarray[row].length; column++){
            switch (Ratingarray[row][column]){
               case 1:
                  break;
               case 2:
                  break;
               case 3:
                  break;
               case 4:
                  break;
               case 5:
                  result = (row + 1 + ",");
            }
         }
      }
      return result;
   }

  }

这是我尝试在主方法中调用此方法的方式:

import java.util.*;
public class TestHotelRating{
   public static void main (String[] args){
         Scanner input = new Scanner(System.in);

         System.out.println ("Please enter the amount of hotels: ");
         int hotel = input.nextInt();

         System.out.println ("Please enter the number of years: ");
         int years = input.nextInt();

         HotelRating myObject = new HotelRating (hotel, years);

         System.out.println(myObject.fiveStarsHotels());
      }
   }
}

当我调用该方法时,它什么也不返回。我不确定为什么。

0 个答案:

没有答案