我試圖創建一個接受用戶輸入的程序,掃描每個子字符串並檢查該字符是否匹配字母表中的某個位置,想要在分數字符串中獲得相同的位置以獲得該字符的分數並顯示每個字符和總分數的分數。問題爲輸入的每個字符分配一個值並顯示輸入的值
我在我的代碼下面應該已經完成了我想要做的每個部分,但是第二個for循環出現了問題,我找不到解決方案,我認爲它可能與子字符串在圈內,但我不知道。
import java.util.*;
import java.io.*;
public class testt
{
public static void main(String [] args) throws IOException
{
String alpha = "abcdefghijklmnopqrstuvwxyz";
String score = "13321424185131139111144849";
String result = "";
int value = 0, total = 0;
Scanner in = new Scanner(System.in);
System.out.print("Enter word: ");
String input = in.nextLine();
if(input == null || input.length() == 0)
{
System.out.println("Enter input.");
System.exit(0);
}
String inputLower = input.replaceAll("[^a-zA-Z ]", "").toLowerCase();
String inputArray[] = inputLower.split("\\s+");
for(int i = 0; i < alpha.length(); i++)
if(inputLower.substring(i, i + 1) == alpha.substring(i, i + 1))
{
value = Integer.parseInt(score.substring(i, i + 1));
if(value == 9)
value++;
result += inputLower.substring(i, i + 1) + " will give you " + value + " point(s)\n";
total += value;
}
else if(i == alpha.length() + 1)
{
System.out.println(result + "This word will earn you: " + total);
System.exit(0);
}
}
}
任何意見或提示我真的很感激,因爲我認爲我的程序大部分已完成。
下面是我從http://i.imgur.com/ftfcINX.jpg
做得好上指出他的錯誤。我只是重寫了一些不太有用的代碼 – Richard