【问题标题】:Single character instance from string字符串中的单个字符实例
【发布时间】:2014-12-08 19:01:45
【问题描述】:

我想知道如何创建一个方法,我可以从字符串中获取单个实例并给它一个 numericValue 例如,如果有一个 String a = "Hello what the hell" 有 4 l 个字符,我想给一个子字符串来自 String a 这是 Hello 并给它数值。现在在我的程序中,它从字符串中获取所有字符实例,因此substring hello 也会从子字符串地狱中获取数值,因为它也具有相同的字符。

我的代码:

public class Puzzle {


private static char[] letters =  {'a','b','c','d','e','f','g','h','i', 'j','k','l','m','n','o','p','q','r','s',
    't','u','v','w','x','y','z'};
private static String input;
private static String delimiters = "\\s+|\\+|//+|=";
public static void main(String[]args)
{
    input = "help + me = please";
    System.out.println(putValues(input));
}

//method to put numeric values for substring from input
@SuppressWarnings("static-access")
public static long putValues(String input)
{
    Integer count;
    long answer = 0;
    String first="";
    String second = "";
    StringBuffer sb = new StringBuffer(input);
    int wordCounter = Countwords();
    String[] words = countLetters();
    System.out.println(input);  

        if(input.isEmpty())
        {
            System.out.println("Sisestage mingi s6na");
        }
        if(wordCounter == -1 ||countLetters().length < 1){

            return -1;
        }
        for(Character s : input.toCharArray())
        {

         for(Character c : letters)
         {               
             if(s.equals(c))
             {
                count = c.getNumericValue(c) - 9;

                System.out.print(s.toUpperCase(s) +"="+ count + ", ");

             }              

         }
         if(words[0].contains(s.toString()))
         {

            count = s.getNumericValue(s);
            //System.out.println(count);
            first += count.toString();      


         }
         if(words[3].contains(s.toString())){

             count = s.getNumericValue(s);
             second += count.toString();
         }

        }
        try {

        answer = Long.parseLong(first)+ Long.parseLong(second);
        } catch(NumberFormatException ex)
        {
            System.out.println(ex);
        }

        System.out.println("\n" + first + " + " + second + " = " + answer);


    return answer;

}
public static int Countwords()
{
    String[] countWords = input.split(" ");
    int counter = countWords.length - 2;
    if(counter == 0) {
    System.out.println("Sisend puudu!");
    return -1;
    }
    if(counter > 1 && counter < 3) {
        System.out.println("3 sõna peab olema");
        return -1;
    }
    if(counter > 3) {
    System.out.println("3 sõna max!");
    return -1;
    }
    return counter;
}

//method which splits input String and returns it as an Array so i can put numeric values after in the
//putValue method
public static String[] countLetters()
{   

    int counter = 0;
    String[] words = input.split(delimiters);       
    for(int i = 0; i < words.length;i++) {

            counter = words[i].length();

         if(words[i].length() > 18) {
            System.out.println("One word can only be less than 18 chars");              
        }           
    }                                           
        return words;           
}

程序必须解决单词谜题,您必须猜测哪个数字对应于哪个字母才能使给定的相等性有效。每个字母必须对应不同的十进制数字,数字中不允许有前导零。

例如,拼图 SEND+MORE=MONEY 只有一个解:S=9、E=5、N=6、D=7、M=1、O=0、R=8、Y=2、给出 9567+1085=10652。

【问题讨论】:

  • 你能更详细地解释一下程序会做什么吗?这听起来很有趣,我想帮忙,但坦率地说,我并没有完全理解这个程序应该做什么。更多输入和预期输出示例可能会有所帮助。
  • 问题到底是什么?是在知道哪个字符是哪个数字后如何将 String 转换为 int,还是如何找出哪个字符是哪个数字以提供正确的谜题解决方案?

标签: java string character


【解决方案1】:

导入 java.util.ArrayList;

public class main {

  private static String ChangeString;
  private static String[] ArrayA;
  private static String a;
  private static int wordnumber;
  private static String temp;
  public static void main(String[] args) {
    // TODO Auto-generated method stub

    a = "hello what the hell";
    wordnumber = 0;
    identifyint(a,wordnumber);

}
public static void identifyint (String a, int WhichWord){

    ChangeString = a.split(" ")[WhichWord];

    ArrayA = a.split(" ");
    replaceword();
    ArrayA[wordnumber] = ChangeString;
    //System.out.print(ArrayA[wordnumber]);
    a = "";
    for(int i = 0; i<ArrayA.length;i++){
        if(i==wordnumber){
            a = a.concat(temp+ " ");
        }
        else{
            a = a.concat(ArrayA[i]+" ");
        }
    }
    System.out.print(a);
}
public static void replaceword(){
    temp = "";
    Character arr[] = new Character[ChangeString.length()];

    for(int i = 0; i<ChangeString.length();i++){
        arr[i] = ChangeString.charAt(i);
        Integer k = arr[i].getNumericValue(arr[i])-9;
        temp = temp.concat(""+k);
    }
    a = temp;
}


}

将 wordnumber 更改为每次要替换的单词。如果这不是您所要求的,请更详细地解释您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 2017-12-01
    • 1970-01-01
    • 2016-03-24
    • 2017-11-15
    • 2010-10-25
    相关资源
    最近更新 更多