【问题标题】:How to read characters in a string and return depending on input?如何读取字符串中的字符并根据输入返回?
【发布时间】:2016-01-25 19:16:17
【问题描述】:

这就是我所拥有的:我想制作一个程序,要求用户输入:你的名字。

这是我需要帮助的:然后,我希望程序读取名称中的每个字符、字母。最后,我希望它返回与用户名中每个字符对应的形容词列表。

这是我目前所拥有的:

public static void main (String [] args) {

    System.out.println("This program will give meanings to every "
                        + "letter in your name.\n");

    Scanner input = new Scanner (System.in);
    System.out.println("Please enter your name:");
    String name = input.next();

    String A = "adventurous";
    String B = "bold";
    String C = "caring";
    String D = "devoted";
    String E = "encouraging";
    String F = "funny";
    String G = "gentle";
    String H = "honest";
    String I = "intelligent";
    String J = "joyful";
    String K = "kind";
    String L = "loving";
    String M = "mature";
    String N = "neat";
    String O = "organized";
    String P = "persistent";
    String Q = "quick";
    String R = "religious";
    String S = "sensitive";
    String T = "thankful";
    String U = "useful";
    String V = "virtuous";
    String W = "witty";
    String X = "this letter isn't in your name";
    String Y = "young";
    String Z = "zany";  
}

【问题讨论】:

  • 您需要更多标签。这是什么语言?我认为是 JAVA 但其他人都知道吗?
  • 你看到了什么输出/错误?
  • @WillSheppard 我只得到两个打印语句:“这个程序将为你名字中的每个字母赋予含义。” “请输入你的名字。”当我输入我的姓名并按 Enter 时,由于代码不完整,因此没有任何反应。

标签: java string java.util.scanner


【解决方案1】:

将你的形容词放在一个 Map 中(以 char 作为键,形容词作为值)而不是使用这些字符串,然后将名称转换为 CharArray,对其进行迭代,并为每个字母从 Map 中获取项目。您可以将形容词放入列表中,然后再打印出来。

//create the map and the list of adjectives
Map<Character, String> adjectives = new HashMap<>();
List<String> personAdjectives = new ArrayList<>();
//fill the map (although it would be better retrieving data from a database)
adjectives.put('a',"adventurous");
adjectives.put('b',"bold");
// ...
//convert the name to a char array
char[] chars = name.toCharArray();
//iterate over it
for(char c : chars){
  //access the map and fill the list
  personAdjectives.add(adjectives.get(c));
}
//print the list

【讨论】:

  • 我收到一个错误:“Map...” - 它说我必须插入“Dimensions”。我搜索并将其更改为:“HashMap...” - 你的建议可以吗?
  • 是的,但您必须颠倒顺序。我已经用正确的顺序更新了我的答案。
  • 太棒了,谢谢!显然我也必须创建一个 Char 类。
  • 不,对不起,我匆忙写了代码,出现了一些错误。实际上我的意思是 char 而不是 Char……你也可以使用 Character,Java 会自动处理转换。无论如何,我很高兴能帮助你。如果您的程序运行正常,请花点时间接受这个答案。您可以通过点击答案得分下方的绿色勾号来做到这一点。
  • java.lang.Character是原语char的对象版本,没有Char。您不能将原始 char 作为 Map 中的键。
【解决方案2】:

#希望对你有帮助#

时间不够,所以总结了这段代码,大家自己动手吧

import java.io.*;

public class StringRead
{
    
    public static void main(String[] arg){
        
        String hi = "hi all man and sexy.";
        
        for(int x = 0; x < hi.length(); x++){
            
            System.out.println(hi.charAt(x));
            
        }
        
        
    
    
    }
} 

【讨论】:

  • 欢迎来到 Stack Overflow。请花时间写一个正确的答案,供参考见How do I write a good answer?。这个答案没有解释(另请参阅include an explanation for your code),而给定的代码确实 not 确实为 OP 的问题提供了答案,因为它需要 OP 猜测他需要做什么或修复什么他给定的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-26
  • 2011-07-09
  • 2018-07-21
  • 1970-01-01
相关资源
最近更新 更多