Java 根据开关盒中的字母计算点数
我正在尝试创建一个类似拼字的程序来计算单词中字母的点数。这些单词包含在.txt文件中。我可以让它从文件中读取,但不确定如何让它计算每个单词的值。我已经附上了到目前为止我所做的,我想知道开关盒是否是最好的方式,如果是的话,我如何为带有开关盒的字母赋值。感谢您的帮助Java 根据开关盒中的字母计算点数,java,switch-statement,Java,Switch Statement,我正在尝试创建一个类似拼字的程序来计算单词中字母的点数。这些单词包含在.txt文件中。我可以让它从文件中读取,但不确定如何让它计算每个单词的值。我已经附上了到目前为止我所做的,我想知道开关盒是否是最好的方式,如果是的话,我如何为带有开关盒的字母赋值。感谢您的帮助 /* * To change this license header, choose License Headers in Project Properties. * To change this template fil
/*
* 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的int。我希望它读取文件中包含的单词并给出总分。看起来像一张地图
适合:
public class PointsProblem {
final Map<Character, Integer> pointsMap = Map.of(
'a', 1,
'e', 1,
//.......
'z', 10
);
我不确定你给我们看的代码是否可以编译。有一些事情你需要改变 1) 您正在将字符串设置为int。您需要将其更改为
int point=0
2) 您没有在switch语句中设置任何内容
将案例“a”:=“1”:
更改为案例“a”:点=1代码>
3) 您永远不会在switch语句中设置唯一值,因为您没有使用“break”
签出此页面以获取良好的教程:
基本上没有任何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[] 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);
使用映射存储值:
Map<Character, Integer> charValues = new HashMap();
charValues.put('a', 2);
charValues.put('b', 1);
但是根据您的用例,您可以先计算字符数,然后再进行乘法
Map<Character, Integer> 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();
您也可以使用开关块的直通功能,而不是为每个“case”语句单独指定
int calcScore(String str)
{
int score = 0;
for(int i=0; i<str.length(); 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;
}
int-calcScore(字符串str)
{
智力得分=0;
对于(inti=0;i谢谢大家,我最终得到了下面的代码,它给出了我想要的输出
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." );
}
System.out.println(“点问题”);
File File=新文件(“dictionary.txt”);
//读文件//
扫描仪输入=新扫描仪(文件);
//检查是否可以找到文件//
如果(!file.isFile()){
System.err.println(“无法打开文件:+文件”);
系统出口(0);
}否则{
System.out.println(“成功打开文件:“+file+”);
}
//读取文件中的所有行//
while(input.hasNext()){
String word=input.nextLine();
//System.out.println(word);
int l=单词长度();
int点=0;
对于(int x=0;x
太棒了,谢谢。这看起来比我正在做的要整洁得多。谢谢,我想如果我的开关坏了,它不会计算整个单词的总数。我将开始修改我的代码。你是对的,它不会计算整个单词。你要做的是对单词中的每个字符运行这个开关语句,然后每年加一次“点”值。我将修改我的答案以帮助解决这个问题。谢谢,我最后使用了这个:开关(c){case'a':case'e':case'i':case'l':case'n':case'o':case'r':case's':case't':case'u':点+=1;中断;等。
Map<Character, Integer> 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();
int total = 0;
switch (c){
case 'a' : total += 1; break;
case 'b' : total += 2; break;
}
int calcScore(String str)
{
int score = 0;
for(int i=0; i<str.length(); 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;
}
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." );
}