我正在尝试创建一个类似拼字游戏的程序来计算单词中包含的字母点。 这些词包含在.txt文件中。 我可以从文件中读取它,但是不确定如何获取它来计算每个单词的值。 我已经附上我到目前为止所做的事情,并且想知道是否最好使用切换大小写,如果是,那么如何为带有切换大小写的字母分配一个值。 任何帮助表示赞赏。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pointsproblem;
/**
*
*/
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class PointsProblem {
/**
* @param args the command line arguments
* @throws java.io.FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
// TODO code application logic here
//create new object//
PointsProblem task1 = new PointsProblem();
File file = new File("dictionary.txt");
// read the file//
Scanner input = new Scanner(file);
//check if file can be found//
if (!file.isFile()) {
System.err.println("Cannot open file:" + input);
System.exit(0);
}
else {
System.out.println("Successfully opened file:" + input +".");
}
//read all the lines in the file//
{
while (input.hasNext()) {
String word = input.nextLine();
System.out.println(word);
System.out.println("'" + input +"' is worth" + point +" points");
int point ="";
switch(point) {
case 'a': ="1":
case 'e': ="1";
case 'i': ="1";
case 'l': ="1";
case 'n': ="1";
case 'o': ="1";
case 'r': ="1";
case 's': ="1";
case 't': ="1";
case 'u': ="1";
case 'g': ="2";
case 'g': ="2";
case 'b': ="3";
case 'm': ="3";
case 'c': ="3";
case 'p': ="3";
case 'f': ="4";
case 'h': ="4";
case 'v': ="4";
case 'w': ="4";
case 'y': ="4";
case 'k': ="5";
case 'j': ="8";
case 'x': ="8";
case 'q': ="10";
case 'z': ="10";
return score = point +"";
}//end switch
}//end point calculation loop
public int getScore() {
return score;
}
//public boolean containsLetter(Character letter) {
//return points.contains(letter);//
}
我也尝试将X的整数赋值给该值。 我希望它阅读文件中包含的单词并给出总分。
看起来Map将适合:
public class PointsProblem {
final Map pointsMap = Map.of(
'a', 1,
'e', 1,
//.......
'z', 10
);
然后,在函数中,只需使用地图为每个字符找到对应的点:
int wordPoints = 0;
for(char c : word.toCharArray())
wordPoints += pointsMap.get(c);
太好了,谢谢你。 这似乎比我正在做的要整洁。
谢谢大家,我最终得到了下面的代码,这些代码给出了我想要的输出;
System.out.println("Points problem");
File file = new File("dictionary.txt");
// read the file//
Scanner input = new Scanner(file);
//check if file can be found//
if (!file.isFile()) {
System.err.println("Cannot open file:" + file);
System.exit(0);
} else {
System.out.println("Successfully opened file:" + file +".");
}
//read all the lines in the file//
while (input.hasNext()) {
String word = input.nextLine();
//System.out.println(word);
int l = word.length();
int point = 0;
for (int x = 0; x < l; x++) {
char c = word.charAt(x);
switch (c) {
case 'a':
case 'e':
case 'i':
case 'l':
case 'n':
case 'o':
case 'r':
case 's':
case 't':
case 'u':
point += 1;
break;
case 'd':
case 'g':
point += 2;
break;
case 'b':
case 'c':
case 'm':
case 'p':
point += 3;
break;
case 'f':
case 'h':
case 'v':
case 'w':
case 'y':
point += 4;
break;
case 'k':
point += 5;
break;
case 'j':
case 'x':
point += 8;
break;
case 'q':
case 'z':
point += 10;
break;
}//end switch*/
}//end point calculation loop
System.out.println(word +"is worth" + point +" points." );
}
除了可以为每个" case"语句分别分配之外,您还可以使用switch块的直通功能。
int calcScore(String str)
{
int score = 0;
for(int i=0; i
{
switch(str.charAt(i)) {
case 'a':
case 'e':
case 'i':
case 'l':
case 'n':
case 'o':
case 'r':
case 's':
case 't':
case 'u': score += 1; break;
case 'g': score += 2; break;
case 'b':
case 'm':
case 'c':
case 'p': score += 3; break;
case 'f':
case 'h':
case 'v':
case 'w':
case 'y': score += 4; break;
case 'k': score += 5; break;
case 'j':
case 'x': score += 8; break;
case 'q':
case 'z': score += 10; break;
}
}
return score;
}
可以为每个单词调用此功能。
谢谢,我结束了:(c){case a:case e:case i:case l:case n:case o:case r:case s:case t:case u:point + = 1; 打破; 等等
使用地图存储值:
Map charValues = new HashMap();
charValues.put('a', 2);
charValues.put('b', 1);
您可以使用chars()并作为总和进行收集
int total = word.chars()
.mapToObj(c -> charValues.get((char)c))
.mapToInt(i -> (int)i)
.sum();
但是根据您的用例,您可以先计算字符数,然后相乘
Map counter = new HashMap<>();
while (input.hasNext()) {
String word = input.next();
word.chars().forEach(c -> counter.compute(c, (k, v) -> v == null ? 1 : v + 1));
}
counter.entrySet()
.stream()
.mapToInt(e -> charValues.get(e.getKey())*e.getValue())
.sum();
如果使用switch,则代码将如下所示:
int total = 0;
switch (c){
case 'a' : total += 1; break;
case 'b' : total += 2; break;
}
我不确定您显示给我们的代码是否可以编译。您需要更改一些内容;
1)您将String设置为int。您需要将其更改为
int point = 0
2)您没有在switch语句中设置任何内容
将case 'a': ="1":更改为case 'a': point = 1;
3)您永远不会在switch语句中设置唯一值,因为您没有使用'break'
在此页面上查看良好的教程:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
基本上没有任何break语句,您的代码将通过这些语句的,并且仅将点分配给您的最后一种情况
您想拥有一些类似的东西
switch(char) {
case 'a': point = 1;
break;
case 'e': point = 1;
break;
// etc.
default: point = 1; // maybe throw an error or add some logging for the default case
break;
}
return point;
我假设您实际上在自己的方法中有此switch语句,而不是如上面在上面显示的那样在main中,否则return语句将无济于事。
您还可以缩短此值,以便每种情况仅返回一个值(同样,如果在其自己的方法中),即
switch(char) {
case 'a': return 1;
case 'b': return 1;
// etc.
}
编辑:
通过switch语句获取整个单词的点值的最佳方法是:
char[] chars = word.toCharArray();
int point=0;
for (char c: chars) {
switch(c) {
case 'a':
point += 1;
break;
// etc.
}
}
System.out.println("Total point value of word '" + word +"' was" + point);
谢谢,我以为如果我在开关中中断了,它将不会计算出整个单词的总数。 我将开始修改我的代码。
没错,它不会计算出整个单词。 您要执行的操作是在单词中的每个字符上运行此switch语句,然后手动求和点值。 我会修改我的答案以帮助解决这个问题