当我尝试运行此命令时,出现错误提示
Exception in thread "main" java.lang.NullPointerException
at lsamelson_lab5.LSamelson_Lab5.main(LSamelson_Lab5.java:21)
Java Result: 1
第21行是要求第一个用户输入的行。
我正在运行Ubuntu,是因为我没有正确导入控制台,还是我的java文件出了问题?
package lsamelson_lab5;
import java.io.Console;
/**
*
* @author binka
*/
public class LSamelson_Lab5 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here\
Console console = System.console();
String input = console.readLine("Please enter your first number:");
int newinput = Integer.parseInt(input);
String input2 = console.readLine("Please enter your second number: ");
int newinput2 = Integer.parseInt(input2);
int sum = newinput + newinput2;
int diff = newinput - newinput2;
int mult = newinput * newinput2;
float div = newinput / newinput2;
System.out.print(sum);
System.out.print(diff);
System.out.print(mult);
System.out.print(div);
}
}
最佳答案
使用
System.out.print("Please enter your first number:")
代替
String input = console.readLine("Please enter your first number:");
Console::readLine()不是您需要的
用读行
Scanner in = new Scanner(System.in);
编辑
Scanner in = new Scanner(System.in);
System.out.println("Please enter your first number:");
int newinput = in.nextInt();
System.out.println("Please enter your second number: ");
int newinput2 = in.nextInt();