gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:04:43 25 4
gpt4 key购买 nike

数据文件:

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 等整数保持其值。

如果还是不清楚,请告诉我

最佳答案

你只需要为局部变量分配默认值,使其可编译

int i = -1;

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

System.out.println(i);

关于java - 如何访问大括号内的整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21103900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com