【问题标题】:about java and array search in a method and return data关于java和数组在方法中搜索并返回数据
【发布时间】:2012-11-04 01:08:53
【问题描述】:

这是我到目前为止所做的程序。我应该要求收银员输入价格,如果是宠物,则输入 y 或 n。如果有 5 个或更多项目,则应该有一种方法来计算折扣。除了折扣方法的返回数据之外,我拥有的程序正在运行。

错误是68:error; cannot return a value from method whose result type is void.

我很困惑为什么数据无效。如果我取出return discount;语句,那么程序编译不会出错。

import javax.swing.JOptionPane;

public class Assignment4
{
    public static void main (String[] args) 
    {
        double[] prices = new double[1000];
        boolean[] isPet = new boolean[1000];
        double enterPrice = 0;
        int i = 0;
        String yesPet = "Y";
        int nItems = 0;
        do
        {
            String input = JOptionPane.showInputDialog("Enter the price for the item: ");
            enterPrice = Integer.parseInt (input);

            prices[i] = enterPrice;

            String petOrNo = JOptionPane.showInputDialog("Is this item a pet? Enter Y for pet and N for not pet.");

            if (petOrNo.equalsIgnoreCase(yesPet))
            {
                isPet[i] = true;
            }
            else
            {
                isPet[i] = false;
            }
            i = i+1;
            nItems = nItems + 1;
        } while (enterPrice != -1);
        //System.out.println(nItems);
    }

    public static void discount(double[] prices, boolean[] isPet, int nItems)
    {
        boolean found = false;
        double[] discount = new double[nItems];

        if (nItems > 6)
        {
            for (int i = 0; i < nItems; i++)
            {
                if (isPet[i] = true)
                {
                    found = true;
                    break;
                }
            }

            if (found = true)
            {
                for (int x = 0; x < nItems; x++)
                {
                    if (isPet[x] = false)
                    {
                        int n = 0;
                        prices[x] = discount[n];
                        n = n + 1;
                    }
                }
            }
        }
        return discount;
    }
}

【问题讨论】:

    标签: java arrays methods


    【解决方案1】:

    discount 方法需要返回一个double 数组。改变

    public static void discount(double[] prices, boolean[] isPet, int nItems) {
    

    public static double[] discount(double[] prices, boolean[] isPet, int nItems) {
    

    没有为discount 数组中的任何条目分配任何值,因此每个值都是0.0

    【讨论】:

    • ty 这么多尝试了,它成功了...要求我做这个程序的人告诉我所需的方法是 public static void discount...所以给定了
    【解决方案2】:
    public static void discount(double[] prices, boolean[] isPet, int nItems)
    

    应替换为:

    public static double[] discount(double[] prices, boolean[] isPet, int nItems)
    

    顺便说一句,discount 永远不会被填充,它会返回一个空数组。

    【讨论】:

      【解决方案3】:

      方法签名是您和 Java 之间的编译时承诺 - 您承诺返回您指定的类型。方法签名中的void 暗示你不会返回任何东西,但你会返回一些东西。这违反了承诺,因此出现了错误。

      您必须将方法签名更改为仅返回 double[] 以履行对编译器的承诺。

      discount 也从未真正填充...赋值是从右到左关联的。所以,prices[x] = discount[n] 语句可能不会像你期望的那样。

      【讨论】:

      • 你能指出我在哪里可以阅读更多关于如何执行价格 [x] = 折扣 [n] 声明并使其按照我想要的方式工作吗?
      • 哇,nvm 我刚刚意识到你说的话......一直在想错误的方式......谢谢你指出这一点。
      猜你喜欢
      • 1970-01-01
      • 2023-04-09
      • 2021-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-20
      • 2023-03-06
      • 2013-08-29
      相关资源
      最近更新 更多