拼字游戏ACSL java编码

时间:2013-12-16 23:31:14

标签: java

所以最新发生的是这段代码,这段代码太长了。有没有办法以更简单的方式制作此代码?我是一个菜鸟,一个朋友问我,他没有任何帐号要求,所以我问我的问题,我的编码非常糟糕所以请帮忙!(我欠他的)

Scanner input = new Scanner(System.in);

    System.out.println("Enter the word:");
    String firstLetter = input.next();
    String secondLetter = input.next();
    String thirdLetter = input.next();;
    String lastLetter = input.next();;
    int valOfFirstLetter = 0;
    int valOfSecondLetter = 0;
    int valOfThirdLetter = 0;
    int valOfLastLetter = 0;



    if (firstLetter.equals("a"))
        valOfFirstLetter = valOfFirstLetter + 1;
    else if (firstLetter.equals("e"))
        valOfFirstLetter = valOfFirstLetter + 1;
    else if (firstLetter.equals("d"))
        valOfFirstLetter = valOfFirstLetter + 2;        
    else if (firstLetter.equals("r"))
        valOfFirstLetter = valOfFirstLetter + 2;
    else if (firstLetter.equals("b"))
        valOfFirstLetter = valOfFirstLetter + 3;
    else if (firstLetter.equals("m"))
        valOfFirstLetter = valOfFirstLetter + 3;
    else if (firstLetter.equals("v"))
        valOfFirstLetter = valOfFirstLetter + 4;
    else if (firstLetter.equals("y"))
        valOfFirstLetter = valOfFirstLetter + 4;
    else if (firstLetter.equals("j"))
        valOfFirstLetter = valOfFirstLetter + 8;
    else if (firstLetter.equals("x"))
        valOfFirstLetter = valOfFirstLetter + 8;
    else
        System.out.println("NOOOOOOO");

    System.out.println(valOfFirstLetter);


    if (secondLetter.equals("a"))
        valOfSecondLetter = valOfSecondLetter + 1;
    else if (secondLetter.equals("e"))
        valOfSecondLetter = valOfSecondLetter + 1;
    else if (secondLetter.equals("d"))
        valOfSecondLetter = valOfSecondLetter + 2;      
    else if (secondLetter.equals("r"))
        valOfSecondLetter = valOfSecondLetter + 2;
    else if (secondLetter.equals("b"))
        valOfSecondLetter = valOfSecondLetter + 3;
    else if (secondLetter.equals("m"))
        valOfSecondLetter = valOfSecondLetter + 3;
    else if (secondLetter.equals("v"))
        valOfSecondLetter = valOfSecondLetter + 4;
    else if (secondLetter.equals("y"))
        valOfSecondLetter = valOfSecondLetter + 4;
    else if (secondLetter.equals("j"))
        valOfSecondLetter = valOfSecondLetter + 8;
    else if (secondLetter.equals("x"))
        valOfSecondLetter = valOfSecondLetter + 8;
    else
        System.out.println("NOOOOOOO");

    System.out.println(valOfSecondLetter);


    if (thirdLetter.equals("a"))
        valOfThirdLetter = valOfThirdLetter + 1;
    else if (thirdLetter.equals("e"))
        valOfThirdLetter = valOfThirdLetter + 1;
    else if (thirdLetter.equals("d"))
        valOfThirdLetter = valOfThirdLetter + 2;        
    else if (thirdLetter.equals("r"))
        valOfThirdLetter = valOfThirdLetter + 2;
    else if (thirdLetter.equals("b"))
        valOfThirdLetter = valOfThirdLetter + 3;
    else if (thirdLetter.equals("m"))
        valOfThirdLetter = valOfThirdLetter + 3;
    else if (thirdLetter.equals("v"))
        valOfThirdLetter = valOfThirdLetter + 4;
    else if (thirdLetter.equals("y"))
        valOfThirdLetter = valOfThirdLetter + 4;
    else if (thirdLetter.equals("j"))
        valOfThirdLetter = valOfThirdLetter + 8;
    else if (thirdLetter.equals("x"))
        valOfThirdLetter = valOfThirdLetter + 8;
    else
        System.out.println("NOOOOOOO");

    System.out.println(valOfThirdLetter);

    if (lastLetter.equals("a"))
        valOfLastLetter = valOfLastLetter + 1;
    else if (lastLetter.equals("e"))
        valOfLastLetter = valOfLastLetter + 1;
    else if (lastLetter.equals("d"))
        valOfLastLetter = valOfLastLetter + 2;      
    else if (lastLetter.equals("r"))
        valOfLastLetter = valOfLastLetter + 2;
    else if (lastLetter.equals("b"))
        valOfLastLetter = valOfLastLetter + 3;
    else if (lastLetter.equals("m"))
        valOfLastLetter = valOfLastLetter + 3;
    else if (lastLetter.equals("v"))
        valOfLastLetter = valOfLastLetter + 4;
    else if (lastLetter.equals("y"))
        valOfLastLetter = valOfLastLetter + 4;
    else if (lastLetter.equals("j"))
        valOfLastLetter = valOfLastLetter + 8;
    else if (lastLetter.equals("x"))
        valOfLastLetter = valOfLastLetter + 8;
    else
        System.out.println("NOOOOOOO");

    System.out.println(valOfSecondLetter);
    System.out.println(firstLetter + secondLetter + thirdLetter + lastLetter);

所以这个程序的作用是,如果你输入一个单词而且只有四个字母单词(来自ACSL)只有a,e = 1点d,r = 2点b,m = 3点v,y = 4点j ,x = 8分。如果我输入例如j a v a with space,它将打印以下内容

8
1
4
1
java

如果您输入的内容不是那些字母,它将打印“NOOOOOO”(我不知道他为什么这样做) 而且我不知道如何将其变成更简单的形式。我正在考虑制作一个字符串并读取每个字母并将其放在if else语句中。这可能吗?

1 个答案:

答案 0 :(得分:0)

  

我认为制作一个字符串并阅读每个字母并将其放在if else语句中。这可能吗?

绝对

您可以使用nextLine()阅读整个单词字符串,使用chars拆分为toCharArray(),使用for loop迭代字符,然后使用switch statement以比使用if / elseif更少重复的方式测试char的值。

Scanner input = new Scanner(System.in);
System.out.println("Enter the word:");
String word = input.nextLine();
int wordValue = 0;

for (char c : word.toCharArray()) {
    int val;
    switch (c) {
    case 'a':
    case 'e':
        val = 1;
        break;
    case 'd':
    case 'r':
        val = 2;
        break;
    case 'b':
    case 'm':
        val = 3;
        break;
    case 'v':
    case 'y':
        val = 4;
        break;
    case 'j':
    case 'x':
        val = 8;
        break;
    default:
        System.out.println("NOOOOOOO");
        return;
    }

    wordValue += val;
}

System.out.println(wordValue);