簡體   English   中英

Java:聲明帶有字符串參數的方法

[英]Java: Declaring a method with a string parameter

到目前為止,我一直沒有對Google和SO進行過搜索。 可能是由於我缺乏知識,也使得很難正確地尋找答案。

從概念上講,我希望有一個方法,它期望一個字符串,然后在其代碼塊中使用該字符串來指導基本的System.out.println

import java.util.HashMap;

public class Restaurant {
    public static void main(String[] args) {
        HashMap<String, Integer> restaurantMenu = new HashMap<String, Integer>();

        restaurantMenu.put("Turkey Burger",13);
        restaurantMenu.put("Naan Pizza",11);
        restaurantMenu.put("Cranberry Kale Salad",10);

        seePrice("Naan Pizza");    

        /* I want to create a method called seePrice, which expects a string input 
        (which will ultimately be a name of one of the food items on the menu). 
        Then it goes on to use that string input in:
        System.out.println(restaurantMenu.get(stringinputgoeshere));

        So that I can simply type seePrice("Naan Pizza");
        and it will display the price in the console
        */

    }
}

經過許多不同的嘗試,我無法獲得最基本的東西來為我工作。 請原諒我是這樣的新手。 感謝所有幫助。

編輯為以下內容(請注意,添加了一些多余的項目,例如sqft線,是為了進一步簡化設置程序和獲取程序的做法)

    import java.util.HashMap;
public class Restaurant {

  //sqft section
  private double sqft = 0.0;
  public Restaurant(){
    sqft = 500.0;
                     }
  public Restaurant(double squareFeet){
        sqft = squareFeet;
                                      }
  public double getSqft(){
    return sqft;
                         }

  //Menu section
    private  HashMap<String, Double> restaurantMenu = new HashMap<String, Double>();
  public int setMenu(HashMap<String, Double> currentMenu){
    this.restaurantMenu = currentMenu;
    return 1;

                                                         }
  public HashMap<String, Double> getMenu(){
    return restaurantMenu;

                                                         }

  public static void seePrice (HashMap<String, Double> menu, String food){
    System.out.println(menu.get(food));
                                                                         }


  /////////Run Main Section Here  
    public static void main(String[] args) {

    Restaurant platefulOfCox = new Restaurant(2120.8);
    System.out.println("The square footage is "+platefulOfCox.getSqft());
    HashMap<String, Double> tempMenu = new HashMap <String, Double>();
      tempMenu.put("Pizza",10.99);
      tempMenu.put("Burger",5.95);
      tempMenu.put("Coke",1.99);
      platefulOfCox.setMenu(tempMenu);



      //Price Checker
      String foodItem = "Coke";
      seePrice(platefulOfCox.getMenu(),foodItem);

    }
}

您的問題是restaurantMenu的范圍。 您不能在main方法之外訪問它。 您需要將其聲明為類的成員,或者將其作為參數傳遞給seePrice方法。

import java.util.HashMap;
public class Restaurant {
    private static HashMap<String, Integer> restaurantMenu;


    public static void main(String[] args) {
        restaurantMenu = new HashMap<String, Integer>();
        ....

地圖restaurantMenu在任何方法seePrice(String)都不可見,因為它是main()方法的局部變量。 您有兩種選擇:

  1. 將地圖作為第二個參數傳遞給seePrice方法,以便它可以查找價格。
  2. restaurantMenu設置為Restaurant類的字段。

第二個選項有兩個子選項:將restaurantMenustatic字段(將seePrice()static方法)或將其設置為實例字段,並在main()方法中創建Restaurant的實例。 如果要使程序超越玩具示例,則將所有內容static會更容易一些,但從長遠來看,靈活性會大大降低。

您已到達那里。 只需將您的HashMap移到其自己的類(也許是public class Menu 就像是:

import java.util.Map;
import java.util.HashMap;

public class Menu {
    private final Map<String, Integer> restaurantMenu = new HashMap<String, Integer>();

    public Menu() {
        restaurantMenu.put("Turkey Burger",13);
        restaurantMenu.put("Naan Pizza",11);
        restaurantMenu.put("Cranberry Kale Salad",10);
    }

    public void seePrice(final String item) {
        System.out.println(restaurantMenu.get(item));
    }
}

這是一個非常簡單的實現。 考慮通過方法而不是在構造函數中添加菜單項,您可能需要在打印到sys out之前檢查傳遞給seePrice的項是否在菜單上。

編輯:從這里,在您的main final Menu menu = new Menu()簡單地創建一個final Menu menu = new Menu()並調用menu.seePrice(item)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM