gpt4 book ai didi

java - 编译错误: variable may not have been initialized

转载 作者:行者123 更新时间:2023-12-01 05:15:00 26 4
gpt4 key购买 nike

所有-我已经看过很多与此类似的问题,但似乎无法解决我的问题。我的问题是,在 if 语句外部声明但在 if 语句内部初始化的变量不会被与其定义的同一范围内的变量识别。这是我的代码:

  String lastP1Boa; //Declared here

if (ownersBoa == message) {
Spinner houseBoa = (Spinner) findViewById(R.id.houseBoa);
String housesBoa = houseBoa.getSelectedItem().toString();
Integer lastIntHouseBoa = Integer.parseInt(housesBoa.replaceAll("[\\D]", ""));

Spinner hotelBoa = (Spinner) findViewById(R.id.hotelBoa);
String hotelsBoa = hotelBoa.getSelectedItem().toString();
Integer lastIntHotelBoa = Integer.parseInt(hotelsBoa.replaceAll("[\\D]", ""));


int intLastP1Boa = lastIntHotelBoa * 1500 + lastIntHouseBoa * 100;
lastP1Boa = String.valueOf(intLastP1Boa); } //Initialized here



String p1Total = lastP1Boa; //Error saying that lastP1Boa needs it be initialized

感谢大家抽出宝贵的时间,希望我已经说清楚了!

最佳答案

编译器报告的问题是 if (ownersBoa == message) 的计算结果有可能为 falselastP1Boa不会被初始化。在声明它的地方将其设置为等于 null(或更合适的默认值)。

String lastP1Boa = null; //Declared here

关于java - 编译错误: variable may not have been initialized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11388083/

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