Java 行号中的错误=inputLine.nextInt();
我有一个问题,因为它说int不能被解引用Java 行号中的错误=inputLine.nextInt();,java,Java,我有一个问题,因为它说int不能被解引用 公共课堂教育QUIZ1{ 公共静态void main(字符串参数[]){ 整数; int输入线; System.out.println(“选择主题:”); System.out.println(“1.手球”); System.out.println(“2.Football”); System.out.println(“3.Basketball”); 系统输出打印(“4.摔跤”); String sport=Keyboard.readString(); 开
公共课堂教育QUIZ1{
公共静态void main(字符串参数[]){
整数;
int输入线;
System.out.println(“选择主题:”);
System.out.println(“1.手球”);
System.out.println(“2.Football”);
System.out.println(“3.Basketball”);
系统输出打印(“4.摔跤”);
String sport=Keyboard.readString();
开关(运动){
案例“1”:
handball myhandball=新手球();
myhandball.printMenu();break;
案例“2”:
足球我的足球=新足球;
myfootball.printMenu();break;
案例“3”:
篮球mybasketball=新篮球();
mybasketball.printMenu();break;
案例“4”:
摔跤我的摔跤=新摔跤();
MyFatchling.printMenu();break;
number=inputLine.nextInt();
如果(数字<0)
System.out.println(“不计算!输入正数!”);
而(数量<0);
}
}
}
什么是取消引用
从技术上讲,任何不是基元类型的变量都持有
引用对象,而不是实际对象本身。使用“.”运算符访问对象的成员时,您是
“取消引用”引用
inputLine
是基元类型int,您不能用它调用方法,因为它不包含任何引用,所以您会得到一个错误int不能被取消引用
你应该做类似的事情。使用scanner
类获取int
输入
Scanner scanner = new Scanner(System.in);
java.util.Scanner类的nextInt(基数)方法扫描下一个
输入的标记为Int
您必须使用扫描仪输入以下数字:
import java.util.Scanner;
...
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
编辑您的示例:
...
switch(sport){
case "1":
handball myhandball = new handball();
myhandball.printMenu();break;
case "2":
football myfootball = new football();
myfootball.printMenu();break;
case "3":
basketball mybasketball = new basketball();
mybasketball.printMenu();break;
case "4":
wrestling mywrestling = new wrestling();
mywrestling.printMenu();break;
}
//put this out of switch statement
Scanner scan = new Scanner(System.in);
number = scan.nextInt();
while (number < 0){
System.out.println("Does not compute! Enter a positive number!");
number = scan.nextInt();
}
}
。。。
开关(运动){
案例“1”:
handball myhandball=新手球();
myhandball.printMenu();break;
案例“2”:
足球我的足球=新足球;
myfootball.printMenu();break;
案例“3”:
篮球mybasketball=新篮球();
mybasketball.printMenu();break;
案例“4”:
摔跤我的摔跤=新摔跤();
MyFatchling.printMenu();break;
}
//将此置于开关状态
扫描仪扫描=新扫描仪(System.in);
number=scan.nextInt();
while(数字<0){
System.out.println(“不计算!输入正数!”);
number=scan.nextInt();
}
}
在inputLine
上调用nextInt()
,它是一种基本的int类型,因此没有任何方法。我假设您希望从用户输入中获取nextInt()
。下面是一些优化代码的建议:
int number;
Scanner inputReader = new Scanner(System.in); // Use a scanner instead of a primitive int
System.out.println("Select Topic:");
System.out.println("1. Handball");
System.out.println("2. Football");
System.out.println("3. Basketball");
System.out.println("4. Wrestling");
String sport = inputReader.next();
switch(sport){
case "1":
Handball myhandball = new Handball(); // Classes should be capitalized
myhandball.printMenu();
break;
case "2":
Football myfootball = new Football();
myfootball.printMenu();
break;
case "3":
Basketball mybasketball = new Basketball();
mybasketball.printMenu();
break;
case "4":
Wrestling mywrestling = new Wrestling();
mywrestling.printMenu();
break;
}
// Move this part outside the switch structure
number = inputReader.nextInt(); // Get the input from the user using a Scanner
while (number < 0) { // Prevent infinite loop by retrieving new input from user inside the loop
System.out.println("Does not compute! Enter a positive number!");
number = inputReader.nextInt();
}
整数;
Scanner inputReader=新扫描仪(System.in);//使用扫描器而不是基元int
System.out.println(“选择主题:”);
System.out.println(“1.手球”);
System.out.println(“2.Football”);
System.out.println(“3.Basketball”);
系统输出打印(“4.摔跤”);
字符串sport=inputReader.next();
开关(运动){
案例“1”:
Handball myhandball=new Handball();//类应大写
myhandball.printMenu();
打破
案例“2”:
足球我的足球=新足球;
myfootball.printMenu();
打破
案例“3”:
篮球mybasketball=新篮球();
mybasketball.printMenu();
打破
案例“4”:
摔跤我的摔跤=新摔跤();
我的摔跤。打印菜单();
打破
}
//将此零件移出开关结构
number=inputReader.nextInt();//使用扫描仪从用户处获取输入
while(number<0){//通过从循环内的用户检索新输入来防止无限循环
System.out.println(“不计算!输入正数!”);
number=inputReader.nextInt();
}
inputLine
从未初始化。您在inputLine
上调用nextInt()
,它是一种基本的int类型,因此没有任何方法。我假设您想从用户输入中获取nextInt()
。number=inputLine.nextInt()
位于交换机内部,可能不是您想要的<代码>while(数字<0)如果数字为负,则代码>是一个无限循环。while(数字<0)代码>infintine循环另外,即使它不是(似乎它应该是一个扫描仪),它也没有初始化。所以你能告诉我具体怎么做,这样我就可以安排它了吗?是的,你是对的,但我只是指出了错误,int不能被取消引用
@VLAZMaybe,你可以做字符串输入
和number=Keyboard.readInt()代码>非常感谢!再来一个问题!你怎么能在一个程序中声明当用户输入一封信时有一个错误?它告诉我无法访问的语句你把它放在哪里了?@Gianluca Aquilina我已经为你的例子编辑了它!只需粘贴代码。你有messenger吗?谢谢,但它说inputReader可能没有初始化是的,发布到fast,几分钟前用初始化编辑了我的响应。但我相信这会向你展示全局,你可以自己填空…:-)你的信使叫什么因为我需要帮助因为我刚开始
...
switch(sport){
case "1":
handball myhandball = new handball();
myhandball.printMenu();break;
case "2":
football myfootball = new football();
myfootball.printMenu();break;
case "3":
basketball mybasketball = new basketball();
mybasketball.printMenu();break;
case "4":
wrestling mywrestling = new wrestling();
mywrestling.printMenu();break;
}
//put this out of switch statement
Scanner scan = new Scanner(System.in);
number = scan.nextInt();
while (number < 0){
System.out.println("Does not compute! Enter a positive number!");
number = scan.nextInt();
}
}
int number;
Scanner inputReader = new Scanner(System.in); // Use a scanner instead of a primitive int
System.out.println("Select Topic:");
System.out.println("1. Handball");
System.out.println("2. Football");
System.out.println("3. Basketball");
System.out.println("4. Wrestling");
String sport = inputReader.next();
switch(sport){
case "1":
Handball myhandball = new Handball(); // Classes should be capitalized
myhandball.printMenu();
break;
case "2":
Football myfootball = new Football();
myfootball.printMenu();
break;
case "3":
Basketball mybasketball = new Basketball();
mybasketball.printMenu();
break;
case "4":
Wrestling mywrestling = new Wrestling();
mywrestling.printMenu();
break;
}
// Move this part outside the switch structure
number = inputReader.nextInt(); // Get the input from the user using a Scanner
while (number < 0) { // Prevent infinite loop by retrieving new input from user inside the loop
System.out.println("Does not compute! Enter a positive number!");
number = inputReader.nextInt();
}