在一个字符串中高效检查一组字母是否为变量的方法是什么?

huangapple go评论64阅读模式
英文:

efficient way to check if a group of letters in a string is a variable?

问题

我正在尝试编写一个打印音乐音阶的代码。出于学校的原因,我不能使用列表或数组类型的结构。以下是我目前的代码部分:

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 值,但我不知道如何将其与我预定义的整数变量匹配。我还尝试过设置类似于 Python 字典的东西,但目前完全不知道如何做。有任何帮助吗?我试图在不使用4000个嵌套的if语句和为每个音阶设置157个变量的情况下完成这个问题。

英文:

I am trying to write a code to print music scales. I cannot use a list or array type of structure (school reasons). here's what i have so far:

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;
		//**************************************************************************************

then further in the code it will, once you select where you want to go, it should print a scale. right now i am on major scales:

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));
}}

my current problem is in the line above last where i am printing the scale. I am wondering if it is possible to check the string scale and see if the words inside correspond to a variable that i predefined. i then want to be able to use the number value that i assigned to the variable. I have tried using the unicode value of the first character the user types, but i don't know how to use that to match to one of my int variables. I have also looked into setting up something similar to a python dictionary, but I'm completely at a loss for how to do that right now. any help? trying to do this without using 4000 nested if statements and 157 variables for each scale.

答案1

得分: 1

Java并不是一种动态语言,因此将用户字符串“翻译”为变量名虽然可能,但却非常繁琐(也是一个非常高级的主题)。

解决你问题的直接方法是使用 Map 结构(在Java中,映射是类似于Python中字典的概念)。以下是一个快速示例:

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

Integer value = scaleByName.get(someStringComingFromUserInput);

然后你可以使用类似 scaleByName.get(someStringComingFromUserInput) 的方法,将字符串键映射到对应的整数值。关于如何使用映射,可以在这里找到更多的 javadoc

英文:

Java isnt a dynamic language, so taking a user string and "translating" that into a variable name is (albeit possible) extremely burdensome (and a very advanced topic).

The straight forward answer to your problem is to use the Map structure (maps are Java's equivalent of python's dicts). A quick example:

Map&lt;String, Integer&gt; scaleByName = new HashMap&lt;&gt;();
scaleByName.put(&quot;a&quot;, 1); 
scaleByName.put(&quot;bb&quot;, 2); ...

Then you can use calls like scaleByName.get(someStringComingFromUserInput) to map the string key to its Integer value. There is plenty of javadoc that tells you more how to use maps.

huangapple
  • 本文由 发表于 2020年10月15日 11:53:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64364612.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定