簡體   English   中英

如何將此字符串與數組進行比較?

[英]How do I compare this string to my array?

我這里有一個小程序,該程序實質上接受通過用戶輸入輸入的服務,然后將其與並行數組關聯以確定該服務的價格。 我在比較第一個String(小)和String數組(subs)時遇到麻煩,該數組應該采用輸入的前3個字符。 我不知道為什么它不起作用,但是據我所知,第一個for循環已正確設置。 即使我只輸入“機油”之類的內容,它也會帶我到“抱歉-無效服務”行。

import java.util.Arrays;

import javax.swing.JOptionPane;

public class CarCareChoice2
{
    public static void main(String[] args)
    {
        String[] services = {"oil change", "tire rotation", "battery check",    "brake inspection"};
        int[] prices = {25, 22, 15, 5};
        String[] subs = {"oil, tir, bat, bra"};
        String ordered;
        boolean choice = false;
        boolean choice2 = false;
        String strserv;
        int cost = 0;
        strserv = JOptionPane.showInputDialog(null, "Enter the service you'd like today."
            + "\n 1: oil change" + "\n2: tire rotation" +
              "\n3: battery check" + "\n4: brake inspection");
        String small = strserv.substring(0,3);

        for (int x = 0; x < subs.length; ++x)
        {
            if(subs.equals(small))
            {
                choice = true;
                cost = prices[x];
            }
        }

        for (int x = 0; x < services.length; ++x)
        {
            if(strserv.length() == services[x].length())
            {
                choice = true;
                cost = prices[x];

                //  System.out.println(" " + strserv.charAt(0));
                //  System.out.println(" " + strserv.substring(0, 3));
                //  String data = strserv.substring(0, 3);
            }
        }

        if(choice)
            JOptionPane.showMessageDialog(null, "The price for "
                + strserv + " is $" + cost);
        else
            JOptionPane.showMessageDialog(null, 
                "Sorry - invalid service");
    }
}

問題在這里:

for (int x = 0; x < subs.length; ++x)
{
    if(subs.equals(small)) //<--- subs and small can never be equal!
    {
        choice = true;
        cost = prices[x];
    }
}

由於您有一個循環,將x從0循環到subs數組的最后一個索引,因此應使用x 檢查索引x處的元素是否等於small

if(subs[x].equals(small))

如果它們相等,則不再需要繼續循環,因此建議您在此處添加一個break 整個過程將是這樣的:

for (int x = 0; x < subs.length; ++x)
{
    if(subs[x].equals(small)) //<--- subs and small can never be equal!
    {
        choice = true;
        cost = prices[x];
        break;
    }
}

另外,您可以使用此方法快速了解元素的索引:

int indexOfChoice = Arrays.asList(subs).indexOf(small);
if (indexOfChoice != -1) { // it returns -1 if not found
    choice = true
    cost = prices[indexOfChoice];
}

編輯:

我只是注意到您的數組被錯誤地聲明:

String[] subs = {"oil, tir, bat, bra"};

它應該是:

String[] subs = {"oil", "tir", "bat", "bra"};

完整代碼在這里:

    String[] services = {"oil change", "tire rotation", "battery check",    "brake inspection"};
    int[] prices = {25, 22, 15, 5};
    String[] subs = {"oil", "tir", "bat", "bra"};
    String ordered;
    boolean choice = false;
    boolean choice2 = false;
    String strserv;
    int cost = 0;
    strserv = JOptionPane.showInputDialog(null, "Enter the service you'd like today."
            + "\n 1: oil change" + "\n2: tire rotation" +
            "\n3: battery check" + "\n4: brake inspection");
    String small = strserv.substring(0,3);

    int indexOfChoice = Arrays.asList(subs).indexOf(small);
    if (indexOfChoice != -1) { // it returns -1 if not found
        choice = true;
        cost = prices[indexOfChoice];
    }

    for (int x = 0; x < services.length; ++x)
    {
        if(strserv.length() == services[x].length())
        {
            choice = true;
            cost = prices[x];

            //  System.out.println(" " + strserv.charAt(0));
            //  System.out.println(" " + strserv.substring(0, 3));
            //  String data = strserv.substring(0, 3);
        }
    }

    if(choice)
        JOptionPane.showMessageDialog(null, "The price for "
                + strserv + " is $" + cost);
    else
        JOptionPane.showMessageDialog(null,
                "Sorry - invalid service");
}

您可能打算這樣做:

    if(subs[x].equals(small))

除了Maurice已經寫過的內容之外,您還必須使用以下方法訪問數組的各個元素

subs[x].equals(small)

在您的比較中,由於您對subs數組的定義,您的代碼仍然無法使用。 如果仔細觀察,即使是字符串數組,也可以看到它僅包含一個元素。

你寫了

String[] subs = {"oil, tir, bat, bra"}

在此數組中,您只有一個字符串,即“ oil,tir,bat,bra”。

你可能想寫的是

String[] subs = {"oil", "tir", "bat", "bra"}

請注意,每個元素都必須用雙引號引起來。 經過測試,它應該可以工作。

除此之外,您可以考慮通過對服務數組中的每個元素進行子字符串化來以編程方式創建subs數組。

暫無
暫無

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

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