Java 变量年份可能尚未初始化
我正试图为我的Java类编译和运行这个程序。每次我试图编译它时,都会说变量year可能尚未初始化。我不能初始化它,因为它是根据用户输入的生日来确定的。有人知道我哪里出错了吗?谢谢(另外:我将所有数据类型都更改为string,因为当我将它们作为整数时,它会说“不兼容的类型:string不能转换为int”) 代码如下:Java 变量年份可能尚未初始化,java,Java,我正试图为我的Java类编译和运行这个程序。每次我试图编译它时,都会说变量year可能尚未初始化。我不能初始化它,因为它是根据用户输入的生日来确定的。有人知道我哪里出错了吗?谢谢(另外:我将所有数据类型都更改为string,因为当我将它们作为整数时,它会说“不兼容的类型:string不能转换为int”) 代码如下: import java.util.Scanner; public class Hundred { public static void main(String[] args)
import java.util.Scanner;
public class Hundred
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String month;
String day;
String year;
String HundredthBirthday = ((year) + 100);
System.out.print("Enter the month in which you were born ");
month = stdIn.nextLine();
System.out.print("Enter the day of the month on which you were born: ");
day = stdIn.nextLine();
System.out.print("Enter the year that you were born ");
year = stdIn.nextLine();
System.out.println("Your Hundredth birtday will be on" + month + day + HundredthBirthday + "!");
} // end main
} // end class Hundred
谢谢大家的反馈!我不知道为什么我以前没有意识到这一点。就我而言,试图在用户输入年份之前计算“第一百天”显然是愚蠢的。而且,我意识到我使用的是“stdIn.nextLine”,而我本应该对某些变量使用“stdIn.nextLine”。这就是我的“无法将字符串转换为int”错误的来源
以下是我最终如何做到这一点:
import java.util.Scanner;
public class Hundred
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String month;
int day;
int Year;
System.out.print("Enter the month in which you were born: ");
month = stdIn.nextLine();
System.out.print("Enter the day of the month on which you were born: ");
day = stdIn.nextInt();
System.out.print("Enter the year that you were born: ");
Year = stdIn.nextInt();
int HundredthBirthday = (Year + 100);
System.out.println("Your Hundredth birtday will be on " + month +" " + day + " " + HundredthBirthday + "!");
} // end main
} // end class Hundred
在初始化变量时定义它们。此外,您不能分配
第一百个生日
,直到年后您获得年
// String month;
// String day;
// String year;
System.out.print("Enter the month in which you were born ");
String month = stdIn.nextLine();
System.out.print("Enter the day of the month on which you were born: ");
String day = stdIn.nextLine();
System.out.print("Enter the year that you were born ");
String year = stdIn.nextLine();
// Presumably, you want a number!
int hundredthBirthday = (Integer.parseInt(year) + 100);
或,首先将您的年
、月
和日
作为整数
(如)
局部变量(方法中的变量)在未初始化的情况下无法使用。默认值应为null
您也可以通过将变量设置为实例变量来实现这一点。在
((year)+100)
中,您应该如何将100
添加到尚不存在的对象中?一些lambda表达式可能会有所帮助-类似()->((Integer.parseInt(year))+100)
可以让您稍后对其进行计算。。。请注意,您需要将年份的字符串表示形式转换为数值<代码>“2015”+“100”
是“2015100”,这不太可能是您要寻找的值。虽然这显然是示例代码,@Fryguy最好先检查year
的值,确保它代表一个有效的整数,然后再将其传递给parseInt
——否则可能会引发NumberFormatException
。@BobJarvis说得好;使用nextInt()
添加了替代方案。公平地说,nextInt
同样存在问题。它仍然会对格式错误的输入抛出异常。
System.out.print("Enter the month in which you were born ");
int month = stdIn.nextInt();
System.out.print("Enter the day of the month on which you were born: ");
int day = stdIn.nextInt();
System.out.print("Enter the year that you were born ");
int year = stdIn.nextInt();
int hundredthBirthday = year + 100;