[英]Scanner.nextInt() struggles
I made a simple text-based calculator, and it all runs fine except for my method which handles getting the user input. 我做了一个简单的基于文本的计算器,除了可以处理用户输入的方法外,其他所有功能都运行良好。 Even though I create a new instance of my class in the main method, and use that instance to call the input method, I still get the compiler error that I'm referencing nextInt() from a static context.
即使我在main方法中创建了类的新实例,并使用该实例调用输入方法,我仍然从静态上下文中得到了我引用nextInt()的编译器错误。 Enlighten me, please.
请赐教。 My main method is as follows:
我的主要方法如下:
public static void main (String[] args) {
CalculatorTextVersion calculator = new CalculatorTextVersion();
Scanner scanner = new Scanner(System.in);
calculator.getCalculation();
}
And the getCalculation method is as follows: 而getCalculation方法如下:
public void getCalculation() {
while (calculating = true) {
System.out.print("First number: ");
first = Scanner.nextInt();
System.out.print("Second number: ");
second = Scanner.nextInt();
System.out.println("Choose an operation: | 1. Add | 2. Subtract | 3. Multiply | 4. Divide |:");
opcode = Scanner.nextInt();
calculate(first, second, opcode);
getSymbol(opcode);
System.out.println(first + opcode + second + " equals " + result + "." );
System.out.print("Continue? Y/N:");
yesNo = Scanner.next();
if (yesNo == "Y" | yesNo == "Yes" | yesNo == "yes") {
calculating = true;
}
if (yesNo == "N" | yesNo == "No" | yesNo == "no") {
calculating = false;
}
}
}
Note: all variables, methods, and imports are fine, I just didn't include them as they weren't part of the problem. 注意:所有变量,方法和导入都可以,我只是不包括它们,因为它们不是问题的一部分。
EDIT: Using the oft-suggested technique of creating an instance variable for the scanner got past the compiler, but generated null pointer errors upon running. 编辑:使用通常为扫描器创建实例变量的技术已经超出了编译器的范围,但是在运行时会生成空指针错误。
EDIT 2: Problem solved, I tried the idea of creating a new scanner in the only method it was needed, worked like a charm. 编辑2:问题解决了,我尝试了用唯一需要的方法创建新扫描仪的想法,就像一个魅力一样。 Thanks!
谢谢!
This is the simplest/best solution so I removed the others. 这是最简单/最好的解决方案,因此我删除了其他解决方案。
public class YourClass
{
public static void main(String[] args)
{
//Scanner scanner is now declared in your CalculatorTextVersion
//CalculatorTextVersion() class in its constructor
//Scanner scanner = new Scanner(System.in);
CalculatorTextVersion calculator = new CalculatorTextVersion();
calculator.getCalculation();
}
}
import java.util.Scanner;
public static class CalculatorTextVersion
{
Scanner scanner;
public CalculatorTextVersion() {
scanner = new Scanner(System.in);
}
public void getCalculation()
{
scanner.nextInt();
}
}
I guess you need to move 我想你需要搬家
Scanner scanner = new Scanner(System.in);
into the CalculatorTextVersion class. 进入CalculatorTextVersion类。
You're going to need to do this: 您将需要执行以下操作:
public Scanner scanner = new Scanner(System.in);
in the main method. 在主要方法中。
This publicizes the scanner for use in the getCalculation() method. 这会公开扫描程序以用于getCalculation()方法。
Good Luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.