Java 发出为每个输入字符分配值并显示输入值的命令
我正在尝试创建一个程序,它接受用户输入,扫描每个子字符串并检查该字符是否与字母表中的某个位置匹配,如果匹配,我希望获得分数字符串中的等效位置,以获得该字符的分数,并显示每个字符的分数和总分数 我在下面的代码中所做的应该已经完成了我试图做的每一部分,但是第二个for循环存在一个问题,我无法找到解决方案,我想可能是循环中的子字符串,但我不确定Java 发出为每个输入字符分配值并显示输入值的命令,java,string,loops,if-statement,for-loop,Java,String,Loops,If Statement,For Loop,我正在尝试创建一个程序,它接受用户输入,扫描每个子字符串并检查该字符是否与字母表中的某个位置匹配,如果匹配,我希望获得分数字符串中的等效位置,以获得该字符的分数,并显示每个字符的分数和总分数 我在下面的代码中所做的应该已经完成了我试图做的每一部分,但是第二个for循环存在一个问题,我无法找到解决方案,我想可能是循环中的子字符串,但我不确定 import java.util.*; import java.io.*; public class testt { public static vo
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);
}
}
}
import java.util.*;
导入java.io.*;
公共类测试
{
公共静态void main(字符串[]args)引发IOException
{
字符串alpha=“abcdefghijklmnopqrstuvxyz”;
String score=“1332142148131139111144849”;
字符串结果=”;
整数值=0,总计=0;
扫描仪输入=新扫描仪(系统输入);
系统输出打印(“输入单词:”);
字符串输入=in.nextLine();
if(input==null | | input.length()==0)
{
System.out.println(“输入”);
系统出口(0);
}
字符串inputLower=input.replaceAll(“[^a-zA-Z]”,“”)。toLowerCase();
字符串inputArray[]=inputLower.split(\\s+);
对于(int i=0;i
任何建议或提示,我将非常感谢,因为我认为我已经完成了大部分程序
这是我正在研究的一个问题,因为在
输入阵列中可能有多个单词,所以需要两个嵌套循环,而不是单个循环。循环应该一个接一个地遍历inputArray
,然后在该数组中遍历字母表中的字母并进行评分:
String alpha = "abcdefghijklmnopqrstuvwxyz";
// 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
int[] score = {1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
int total = 0;
for (String word : inputArray) {
int wordTotal = 0;
for(int i = 0; i < word.length(); i++) {
char letter = word.charAt(i);
int pos = alhpa.indexOf(letter);
int value = score[pos];
System.out.println("Letter "+letter+" will earn you: " + value);
// Increment wordTotal, not total
wordTotal += value;
}
System.out.println("Word "+word+" will earn you: " + wordTotal);
total += wordTotal;
}
System.out.println("Total for all words: " + total);
String alpha=“abcdefghijklmnopqrstuvxyz”;
//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
int[]分数={1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,1,4,4,8,4};
int-total=0;
for(字符串字:inputArray){
int-wordTotal=0;
for(int i=0;i
以下是一些实施注意事项:
- 使用整数数组而不是字符数组可以避免将10“编码”为
'9'
,并帮助您删除if(value==9)
hack
- 对于
char
,使用indexOf
,并使用该索引查找分数数组,而不是使用子字符串和查找字符串中的单词
我想我的两分钱,我有点晚了。
您可以将实际分数代码放入一个函数中,并在循环中调用它
public class Test {
public static void main(String[] args) throws IOException {
int total = 0;
Scanner in = new Scanner(System.in);
System.out.print("Enter word: ");
String input = in.nextLine();
// Close the input stream to avoid memory leaks
in.close();
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+");
// Loop through each entered word, I only did this step because you appeared to split up the input even though the Array wasn't actually being used in your original program
for(String word : inputArray) {
int currentWordTotal = 0;
// Use a method to get the value for each letter in the current word
for(int i = 0; i < word.length(); i++) {
currentWordTotal += letterScore(word.charAt(i));
}
// Print out the word total
System.out.println("Word: " + word + ", will earn you: " + currentWordTotal + " points.");
// Keep track of all word total in the event that multiple words were entered
total += currentWordTotal;
}
// Print out the total for all words
System.out.println("Your Total for the entered Words: " + total);
}
private static int letterScore(char letter) {
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'};
int[] scores = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 9, 1, 1, 1, 1, 4, 4, 8 ,4 ,10};
// Here we look through the array of letters, when we find a match we use the index of the character array to get the corresponding value from the score array
for(int i = 0; i < letters.length; i++) {
if(letters[i] == letter) {
return scores[i];
}
}
return 0;
}
}
公共类测试{
公共静态void main(字符串[]args)引发IOException{
int-total=0;
扫描仪输入=新扫描仪(系统输入);
系统输出打印(“输入单词:”);
字符串输入=in.nextLine();
//关闭输入流以避免内存泄漏
in.close();
if(input==null | | input.length()==0){
System.out.println(“输入”);
系统出口(0);
}
字符串inputLower=input.replaceAll(“[^a-zA-Z]”,“”)。toLowerCase();
字符串inputArray[]=inputLower.split(\\s+);
//循环遍历每个输入的单词,我只执行了这一步,因为即使在原始程序中没有实际使用数组,您似乎也在分割输入
for(字符串字:inputArray){
int currentWordTotal=0;
//使用方法获取当前单词中每个字母的值
for(int i=0;i
您的实际代码显示了我将陈述的主要问题:
您正在循环遍历alpha.length
,并且您冒着获得
如果i
不大于input.length
您正在使用子字符串(i,i+1)
获取该位置的字符
我知道你可以重播
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.next();
for(int i = 0; i < input.length(); i++) {
if( i<alpha.length()) {
if(input.charAt(i) == alpha.charAt(i))
{
value = Integer.parseInt(score.charAt(i)+"");
if(value == 9)
value++;
result += input.charAt(i) + " will give you " + value + " point(s)\n";
total += value;
}
}
}
System.out.println(result + "This word will earn you: " + total);
a will give you 1 point(s)
c will give you 3 point(s)
e will give you 1 point(s)
This word will earn you: 5