Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 检查字符串中的一组字母是否为变量的有效方法?_Java_String_Dictionary_Variables_String Literals - Fatal编程技术网

Java 检查字符串中的一组字母是否为变量的有效方法?

Java 检查字符串中的一组字母是否为变量的有效方法?,java,string,dictionary,variables,string-literals,Java,String,Dictionary,Variables,String Literals,我正试图写一个代码来打印音阶。我无法使用结构的列表或数组类型(学校原因)。以下是我目前掌握的情况: public class Musicarum { public static void main(String[] args) { Scanner input = new Scanner(System.in); double buildNum = 0.01; //*******************************

我正试图写一个代码来打印音阶。我无法使用结构的列表或数组类型(学校原因)。以下是我目前掌握的情况:

public class Musicarum {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double buildNum = 0.01;
        
        //**************************************************************************************
        int a = 1, bb = 2, b = 3, c = 4, db = 4, d = 5, eb = 6, e = 7, f = 8, gb = 9, ab = 10;
        //**************************************************************************************
然后进一步在代码中,一旦你选择了你想去的地方,它应该打印一个刻度。现在我在大音阶上:

if(answer.equals("scales")) {
                System.out.println("Would you like to see a major or a minor scale?");
                String scale = input.nextLine();
                scale = scale.toLowerCase();
                if(scale.equals("major"));
                System.out.println("What Scale would you like to see?");
                scale = input.nextLine();
                System.out.println("The scale you would like to see is made of the notes: " + scale.charAt(0));
}}

我目前的问题是在上面的最后一行,我打印的规模。我想知道是否可以检查字符串
scale
,看看里面的单词是否对应于我预定义的变量。然后我希望能够使用分配给变量的数值。我尝试过使用用户键入的第一个字符的unicode值,但我不知道如何使用它来匹配我的一个int变量。我也考虑过建立类似于python字典的东西,但我现在完全不知道如何做到这一点。有什么帮助吗?尝试在不使用4000个嵌套if语句和157个变量的情况下实现这一点。

Java不是一种动态语言,因此获取用户字符串并将其“翻译”为变量名(尽管可能)是极其繁重的(也是一个非常高级的主题)

对您的问题的直接回答是使用Map结构(Map是Java与python的dicts的等价物)。一个简单的例子:

Map<String, Integer> scaleByName = new HashMap<>();
scaleByName.put("a", 1); 
scaleByName.put("bb", 2); ...
Map scaleByName=newhashmap();
scaleByName.put(“a”,1);
scaleByName.put(“bb”,2)。。。

然后您可以使用类似于
scaleByName.get(someStringComingFromUserInput)
的调用将字符串键映射到其整数值。有很多东西告诉你如何使用地图。

谢谢你%1000你是个圣人。我会查看地图,看看我是否能解决这个问题,并且可能会在我做(或不做)的时候编辑我的帖子。谢谢你的快速接受。但请注意:请不要再回来,然后改变问题的主题。如果你在路上遇到了另一个问题,那么首先做研究,如果没有帮助,请在这里随意写另一个新问题。