如何访问大括号内的整数?

时间:2014-01-14 00:12:55

标签: java scope

数据文件:

J A V A
1 H 11 H 21 H
1 V 2 V 3 V
2 H 12 H 22 H
3 V 4 V 5 V
7 H 6 V 17 H

代码(较大程序的一部分):

 File dataFile = new File("data.txt");

    Scanner in;
    in = new Scanner (dataFile);

    String letter1 = in.next(); //Reads first letter of the word and stores it as a string
    String letter2 = in.next(); //Reads second letter of the word and stores it as a string
    String letter3 = in.next(); //Reads third letter of the word and stores it as a string
    String letter4 = in.next(); //Reads fourth letter of the word and stores it as a string

    // Not all of these ints are used in the code shown here        
    int firstLetterValue1;
    int secondLetterValue1;
    int thirdLetterValue1;
    int fourthLetterValue1;

    int firstLetterValue2;
    int secondLetterValue2;
    int thirdLetterValue2;
    int fourthLetterValue2;

    int firstLetterValue3;
    int secondLetterValue3;
    int thirdLetterValue3;
    int fourthLetterValue3;

    int multiplier1 = 1;
    int multiplier2 = 1;
    int multiplier3 = 1;

    int totalWordValue1 = 0;
    int totalWordValue2 = 0;
    int totalWordValue3 = 0;            

    // These test to see whether the first, second, third, or fourth letter of the word
// Are equal to a specified letter and if they are,
// then a value is stored into a different integer
    if (letter1.equalsIgnoreCase("A") || letter1.equalsIgnoreCase("E"))
    {
    firstLetterValue1 = 1;
    firstLetterValue2 = 1;
    firstLetterValue3 = 1;
    }               

    else if (letter1.equalsIgnoreCase("D") || letter1.equalsIgnoreCase("R"))
    {
    firstLetterValue1 = 2;
    firstLetterValue2 = 2;
    firstLetterValue3 = 2;
    }

    else if (letter1.equalsIgnoreCase("B") || letter1.equalsIgnoreCase("M"))
    {
    firstLetterValue1 = 3;
    firstLetterValue2 = 3;
    firstLetterValue3 = 3;
    }

    else if (letter1.equalsIgnoreCase("V") || letter1.equalsIgnoreCase("Y"))
    {
    firstLetterValue1 = 4;
    firstLetterValue2 = 4;
    firstLetterValue3 = 4;
    }

    // For example, in the word "J A V A", the first letter, which is "J", will store 
    // the value "8" into the integers inside the brackets (firstLetterValue1, firstLetterValue2, etc.)
    else if (letter1.equalsIgnoreCase("J") || letter1.equalsIgnoreCase("X"))
    {
    firstLetterValue1 = 8;
    firstLetterValue2 = 8;
    firstLetterValue3 = 8;
    }

    for (int num = 0; num <= 4; num++)
    {   

    int location1 = in.nextInt();
    String direction1 = in.next(); 

// This is where I am getting my problem: "The local variable firstLetterValue1 has not been initialized"   
    if ((location1 == 1) && (direction1.equalsIgnoreCase("H")))
    {
      firstLetterValue1 *= 1;
      secondLetterValue1 *= 1;
      thirdLetterValue1 *= 2;
      fourthLetterValue1 *= 1;
      multiplier1 = 1;                  
      totalWordValue1 = (firstLetterValue1 + secondLetterValue1 + thirdLetterValue1 + fourthLetterValue1) * multiplier1;
    }   
}

我试着用评论来概述我的问题。所以,我在if ((location1 == 1) && (direction1.equalsIgnoreCase("H")))的部分收到错误。我知道该程序要我按int firstLetterValue1 = 0;声明整数,但我得到的问题是它之前的整个部分只有if语句将被跳过。我希望firstLetterValue1firstLetterValue2等整数保持其值。

如果仍然不清楚,请告诉我

2 个答案:

答案 0 :(得分:4)

您只需将默认值分配给可编辑的局部变量

int i = -1;

if( /* some condition */) {
  i = 10;
}

System.out.println(i);

答案 1 :(得分:2)

如果if / else if中没有条件为真,则不会初始化变量。两个选项:

  • 在声明它们时使用默认值初始化它们
  • 添加else语句以确保始终初始化变量