我用非常有限的知识制作了一个使用Java的非常简单的控制台计算器。程序执行时抛出的NullPointerException Java
程序必须在两个数字上执行基本的数学函数(+, - ,*,/)。该程序在开始时要求两个String
值,即inpmode
和outmode
。 inpmode
是用户输入两个数字的模式(两种模式分别为十进制输入“dec”和二进制输入“bin”)。 outmode
是用户希望显示其结果的模式。此信息(inpmode
和outmode
)被写入文本文件并存储在某处,然后程序继续询问值并相应地显示结果。显示结果后,用户有三个选项:退出,继续使用相同的模式,改变模式。这一直持续到用户选择退出。
Here's a simple flowchart I made for the purpose.
这里是我的可怜代码:
import java.io.*;
import java.util.*;
class FileTry {
Scanner sc=new Scanner(System.in);
String inpmode="";
String outmode="";
FileWriter fw;
BufferedWriter bw;
PrintWriter pw;
String n1,n2;
FileReader fr;
BufferedReader br;
String read;
String arr[]=new String[2];
int nu2,res,i=0;
String op; int nu1=nu2=res=0;
void main(){
try {
fr=new FileReader("C:\\Users\\dell pc\\Desktop\\tryer.txt");
askdata();
}
catch (IOException e) {
try
{
System.out.println("File needs to be created first");
fw=new FileWriter("C:\\Users\\dell pc\\Desktop\\tryer.txt");
bw=new BufferedWriter(fw);
pw=new PrintWriter(bw);
askuser();
}
catch(IOException a){
System.out.println("Error");
}
}
}
void askuser()
{
System.out.println("Input mode?");
inpmode=sc.nextLine();
System.out.println("Output mode?");
outmode=sc.nextLine();
modewriter();
}
void modewriter()
{
try
{
pw.println(inpmode);
pw.println(outmode);
pw.close();
bw.close();
fw.close();
}
catch(IOException b)
{
System.out.println("error");
}
askdata();
}
void askdata()
{
System.out.println("Enter num 1");
n1=sc.nextLine();
System.out.println("Enter num 2");
n2=sc.nextLine();
System.out.println("Enter the operation");
op=sc.nextLine();
reader();
}
void reader()
{
int i=0;
try
{
Scanner fileScanner=new Scanner(new File("C:\\Users\\dell pc\\Desktop\\tryer.txt"));
while (fileScanner.hasNextLine()){
arr[i]=fileScanner.nextLine();
i++;
}
}
catch (IOException x)
{
System.out.println("errer");
}
caller();
}
void caller(){
if (arr[0].equals("bin")&&arr[1].equals("bin"))
{
todec();
operate();
tobin();
print();
}
else if(arr[0].equals("bin")&&arr[1].equals("dec"))
{
todec();
operate();
print();
}
else if(arr[0].equals("dec")&&arr[1].equals("dec"))
{
nu1=Integer.parseInt(n1);
nu2=Integer.parseInt(n2);
operate();
print();
}
else if(arr[0].equals("dec")&&arr[1].equals("bin"))
{
nu1=Integer.parseInt(n1);
nu2=Integer.parseInt(n2);
operate();
tobin();
print();
}
else System.out.println("kk");
}
void todec()
{
int decimal = 0;
int power = 0;
int binary=Integer.parseInt(n1);
while(true){
if(binary == 0){
break;
} else {
int tmp = binary%10;
decimal += tmp*Math.pow(2, power);
binary = binary/10;
power++;
}
}
nu1=decimal;
decimal = 0;
power = 0;
binary=Integer.parseInt(n2);
while(true){
if(binary == 0){
break;
} else {
int tmp = binary%10;
decimal += tmp*Math.pow(2, power);
binary = binary/10;
power++;
}
}
nu2=decimal;
System.out.println(nu1+" "+nu2);
}
void operate()
{
switch(op.charAt(0))
{
case '+' :
{ res=nu1+nu2;
break;}
case '-':
{ res=nu1-nu2;
break;}
case '/':
{ res=nu1/nu2;
break;}
case '*':
{ res=nu1*nu2;
break;}
default:
System.out.println("Errorr");
}
}
void tobin()
{
String temp="";
i=res;
while(i>0)
{
temp=(i%2)+temp;
i=i/2;
}
temp=i+temp;
res=Integer.parseInt(temp);
}
void print()
{
System.out.println(res);
System.out.println("n for another operation");
System.out.println("m to change the modes");
System.out.println("e to exit");
char c=sc.nextLine().charAt(0);
switch (c)
{
case 'n' :
{
askdata();
break;
}
case 'm' :
{
askuser();
break;
}
case 'e' :
{
System.out.println("Bye");
break;
}
default: System.out.println("errreeer");
}
}
}
时,我选择了在第一次运行后更改模式外得到投掷,所以有人可以帮我解决这个问题?
编辑:我看到有关我的问题是重复的this one.我不明白那是怎么回事。我被困在一个程序中,而这个问题仅仅解释了我得到的错误。我需要一个解决方案或可能的建议来使我的代码工作。
可能重复[什么是NullPointerException,以及如何解决它?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-doi-i-fix -it) –
打印的确切错误消息是什么?我们需要堆栈跟踪。 – marstran
[这是它是什么](https://s6.postimg.org/644j4z8cx/Untitled.png)@marstran – JavaPilgrim