Java 扫描仪对象在While循环中给我错误
我在让这个程序正常运行方面遇到了一个小问题Java 扫描仪对象在While循环中给我错误,java,while-loop,java.util.scanner,Java,While Loop,Java.util.scanner,我在让这个程序正常运行方面遇到了一个小问题 /** * * @author Randy */ import java.util.Scanner;//Import scanner public class RandyGilmanhw2a { int year_of_birth; int age; public RandyGilmanhw2a (){//begin constructor year_of_birth = 1900; a
/**
*
* @author Randy
*/
import java.util.Scanner;//Import scanner
public class RandyGilmanhw2a {
int year_of_birth;
int age;
public RandyGilmanhw2a (){//begin constructor
year_of_birth = 1900;
age = 0;
}//end constructor
public int getYear(){//get year method
return year_of_birth;
}//end method
public int getAge(int year_of_birth){//get year method
age = 2014 - year_of_birth;
return age;
}//end get year method
public void setYear (int year){//set year method
this.year_of_birth = year;
}//end method
public static void main(String[] args) {//begin main
RandyGilmanhw2a user1 = new RandyGilmanhw2a();
Scanner year = new Scanner(System.in);//create a scanner object
System.out.println("Please enter the year you were born: ");
int year_of_birth = year.nextInt();
while( year_of_birth < 1900 || year_of_birth > 2014 ) {//begin while loop
System.out.println("Please reenter the year you were born." );
System.out.println("You must have an integer between 1900 and 2014:" );
System.out.println("\n");
System.out.println("Please enter the year you were born: ");
int year_of_birth = year.nextInt();//ERROR OCCURS HERE SAYS VARIABLE
//year_of_birth ALREADY DEFINED IN METHOD MAIN
}//end while
user1.getAge(year_of_birth);
System.out.println("You are " + age + " years old." );//ERROR HERE SAYS NON-STATIC
// VARIABLE age CANNOT BE REFERENCED FROM A STAIC CONTEXT
}//end main
}//end class
/**
*
*@作者兰迪
*/
导入java.util.Scanner//导入扫描仪
公共级兰迪吉尔曼W2A{
出生年份;
智力年龄;
公共RandyGilmanhw2a(){//begin构造函数
出生年份=1900年;
年龄=0;
}//端构造函数
public int getYear(){//get year方法
返回出生年份;
}//结束方法
公共整数获取(整数年出生){//get year方法
年龄=2014年-出生年份;
回归年龄;
}//年终收益法
公共无效设置年(整数年){//设置年方法
本年出生年月=年;
}//结束方法
公共静态void main(字符串[]args){//begin main
RandyGillManhW2A user1=新的RandyGillManhW2A();
扫描仪年份=新扫描仪(System.in);//创建扫描仪对象
System.out.println(“请输入您出生的年份:”;
int year_of_birth=year.nextInt();
while(出生年份<1900 | |出生年份>2014){//开始while循环
System.out.println(“请重新输入您出生的年份”);
System.out.println(“必须有一个介于1900和2014之间的整数:”;
System.out.println(“\n”);
System.out.println(“请输入您出生的年份:”;
int year_of_birth=year.nextInt();//此处发生错误
//主方法中已定义出生年份
}//结束时
用户1.年龄(出生年份);
System.out.println(“您是“+age+”岁。”);//这里的错误是非静态的
//无法从STAIC上下文引用可变年龄
}//端干管
}//末级
我已经对显示错误的区域进行了评论。我试图制作一个程序,通过输入一个人的年龄来显示他的年龄。但是,如果他们输入1900年之前或2014年之后的年份,我希望它要求用户重新输入他们的出生年份。我似乎找不到问题所在。任何帮助都将不胜感激。只需删除
int
声明即可。这样,就可以重新定义变量。
所以,切换这个:
int year_of_birth = year.nextInt();
为此:
year_of_birth = year.nextInt();
只需删除
int
声明。这样,就可以重新定义变量。
所以,切换这个:
int year_of_birth = year.nextInt();
为此:
year_of_birth = year.nextInt();
从
出生年份的第二次初始化中删除int
,您的问题就会消失。从出生年份的第二次初始化中删除int
,您的问题就会消失。在出生年份=年份时删除int
代码>并将年龄输出更改为:
System.out.println("You are " + user1.getAge(year_of_birth) + " years old." );
在int出生年份=year.nextInt()处删除int
代码>并将年龄输出更改为:
System.out.println("You are " + user1.getAge(year_of_birth) + " years old." );
不要在while循环中将出生年份
定义为int
,就像您在out-of-loop中定义的那样
while( year_of_birth < 1900 || year_of_birth > 2014 ) {//begin while loop
...
year_of_birth = year.nextInt();//just assign next value
...
}
while(出生年份<1900 |出生年份>2014){//开始while循环
...
出生年份=year.nextInt();//只需指定下一个值
...
}
不要在while循环中将出生年份定义为int
,就像您在out-of-loop中定义的那样
while( year_of_birth < 1900 || year_of_birth > 2014 ) {//begin while loop
...
year_of_birth = year.nextInt();//just assign next value
...
}
while(出生年份<1900 |出生年份>2014){//开始while循环
...
出生年份=year.nextInt();//只需指定下一个值
...
}