Java - 使用数组返回构造函数方法

时间:2014-05-08 03:03:03

标签: java arrays

我有2个文件,其中一个请求用户输入,其中一个根据用户输入返回数组值。我在返回我的阵列时遇到了问题。我只收到每个数组中的特定元素,尽管我想要返回整个内容。你看到我失踪的是什么吗?感谢。

  // Filename SalonReport.java

  import java.util.*;
  import javax.swing.*;
  import java.util.Scanner;
  import java.util.Arrays;

  public class SalonReport {
  public static void main(String[] args) {
  int x, y, sortSelect, view;
  boolean repeat = false;
  boolean loop = false;
  int z = 1;


  Scanner input = new Scanner(System.in); 

  System.out.print("Welcome to Erik's Hair Salon");
  System.out.println();

  //Using do-while to assign selection
  do
     if(z > 0)
     {
  //Welcoming message
        Service description = new Service();
        Service price = new Service();
        Service minutes = new Service();

        System.out.println();
        System.out.println("We offer the following services at our salon:");
        System.out.println();
        System.out.println("Service" + "\t" + "\t" + "Price" + "\t"  + "Minutes");
        System.out.println("************************************");
           for (x = 0; x < 6; x++) {
              for (y = 0; y < 3; y++) {
                 System.out.print(description.getService() + "\t" + "\t");
              }
              System.out.println();
           }

  //User input message

        System.out.println("How do you wish to sort our table of services?");
        System.out.println();
        System.out.println("To sort by Service Description, enter 1 >>");
        System.out.println("To sort by Price, enter 2 >>");
        System.out.println("To sort by Minutes, enter 3 >>");
        System.out.println();
        System.out.print("To exit, enter 0 >> ");
           sortSelect = input.nextInt();

           if(sortSelect == 1)
           {
           System.out.println();
           System.out.println("Service" + "\t" + "\t" + "Price" + "\t"  + "Minutes");
           System.out.println("************************************");
           for (x = 0; x < 6; x++) {
              for (y = 0; y < 3; y++) {
                 System.out.print(description.getService() + "\t" + "\t");
                 }
              System.out.println();
              repeat = true;
              }
           }
           else if(sortSelect == 2)
           {
           System.out.println();
           System.out.println("Price" + "\t" + "\t" + "Service" + "\t"  + "Minutes");
           System.out.println("************************************");
              for (x = 0; x < 6; x++) {
                 for (y = 0; y < 3; y++) {
                    System.out.print(price.getPrice() + "\t" + "\t");
                 }
              System.out.println();
              repeat = true;
              }
           }
           else if(sortSelect == 3)
           {
           System.out.println();
           System.out.println("Minutes" + "\t" + "\t" + "Service" + "\t"  + "Price");
           System.out.println("************************************");
              for (x = 0; x < 6; x++) {
                 for (y = 0; y < 3; y++) {
                    System.out.print(minutes.getTime() + "\t" + "\t");
                 }
              System.out.println();
              repeat = true;
              }
           }
           else if(sortSelect == 0)
                    {
                    System.out.println();
                    System.out.println("Goodbye!");
                    break;
                    }

        System.out.println();
        System.out.println("Do you wish to view our services again?");
        System.out.println("Enter 1 for Yes >>");
        System.out.print("Enter 0 for No >> ");
           view = input.nextInt();
              if(view == 1)
                 {
                 loop = true;
                 repeat = true;
                 }
                 else if(view == 0)
                    {
                    loop = false;
                    repeat = false;
                    System.out.println();
                    System.out.println("Goodbye!");
                    break;
                    }
        }
        else
           {
           repeat = false;
           System.out.println();
           System.out.println("Goodbye!");
              break;
           }
     while(repeat = true);
  }
  }


  // Filename Service.java

  import java.util.*;
  import javax.swing.*;
  import java.util.Scanner;


  public class Service {

  private String[][] sortByDesc = {{"Cut", "$8.00", "15"}, {"Mani.", "$18.00", "30"}, {"Perm.",       
  "$18.00", "35"}, {"Shampoo", "$4.00", "10"}, {"Style", "$48.00", "55"}, {"Trim", "$6.00",       
  "5"}};
  private String[][] sortByPrice = {{"$4.00", "Shampoo", "10"}, {"$6.00", "Trim", "5"}, 
  {"$8.00", "Cut", "15"}, {"$18.00", "Mani.", "30"}, {"$18.00","Perm", "35"}, {"$48.00", 
  "Style", "55"}};
  private String[][] sortByTime = {{"5", "Trim", "$6.00"}, {"10", "Shampoo", "$4.00"}, {"15",       
  "Cut", "$8.00"}, {"30", "Mani.", "$18.00"}, {"35","Perm", "$18.00"}, {"55", "Style", 
  "$48.00"}};



   public String getService() {
       return sortByDesc[5][2];
   }

   public String getPrice() {
       return sortByPrice[5][2];
   }

   public String getTime() {
       return sortByTime[5][2];
   }
  }

1 个答案:

答案 0 :(得分:0)

   public String getService() {
       return sortByDesc[5][2];
   }

仅返回位置5/2上的元素。如果你想返回整个数组,它应该像

    public String [][] getService() {
       return sortByDesc;
   }