在一个阵列传递一个字符串值Java中的方法是什么? [英] Passing value of a string in an array to a method in java?

查看:189
本文介绍了在一个阵列传递一个字符串值Java中的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您抽出宝贵的时间来阅读。很抱歉,如果我的问题是愚蠢的,我试图寻找,但不能完全弄清楚为什么我有这个问题。我试图测试一些code的另一个应用程序,但我有问题。也许我只是不明白阵列正常。

thank you for taking the time to read this. Sorry if my question is dumb, I tried searching but couldn't quite figure out why I'm having this problem. I was trying to test some code for another application, but I'm having issues. Perhaps I just don't understand arrays properly.

我有一个在名为移调类名为halfStepUp方法,用于测试目的如果给C和D#如果给D,应该返回C#。这是code:

I have a method named halfStepUp in a class named Transpose that, for testing purposes, should return "c#" if given "c" and "d#" if given "d". This is the code:

public class Transpose{
    public static String halfStepUp(String note){
        String n = null;
        if (note == "c") n = "c#";
        if (note == "d") n = "d"#;
        return n;
    }   
}

我有以下的code在我的主要方法:

I have the following code in my main method:

String [] scale = new String[2];
scale[0] = "c";
scale[1] = "d";

System.out.println(Transpose.halfStepUp(scale[0]));

这将打印空。我究竟做错了什么?
我知道方法的作品,因为如果我用

This prints "null." What am I doing wrong? I know the method works because if I use

System.out.println(Transpose.halfStepUp("c"));

它工作正常。该解决方案可能是令人尴尬的容易,但对于帮助搜索时,我无法找到一个好办法来字呢。
再次感谢您的阅读,任何答案都是极大的AP preciated!

It works fine. The solution is probably embarrassingly easy but I couldn't find a good way to word it when searching for help. Thanks again for reading, and any answers are greatly appreciated!

推荐答案

试试这个:(从评论编辑)

Try this instead: (edited from comments)

public class Transpose{
    public static String halfStepUp(String note){
        String n = null;
        if ("c".equals(note)) n = "c#"; //using .equals as a string comparison
        if ("d".equals(note)) n = "d#"; //not "d"#
        return n;
    }   
}

这篇关于在一个阵列传递一个字符串值Java中的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆