好吧,我正在尝试创建一个Java计算器,但是每当我尝试输入一个数字时,它都会请求两次,不仅如此,而且我为第一个数字输入的数字不起作用,并且只给了我一个0+(输入过的数字进入 2 号)有人愿意帮助我吗?并解释我需要做什么来解决这个问题?
package Calc1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Calc1 {
public static void main(String[] args) throws IOException {
// TODO code application logic here
Boolean test = true;
Scanner in = new Scanner(System.in);
Scanner in2 = new Scanner(System.in);
int a = 0;
int b = 0;
String sum;
System.out.println("Please enter the first number you wish to calculate.");
if(in.hasNextInt()) {
b = in.nextInt();
}
while (!in.hasNextInt()) {
System.out.println("Invalid number. Please enter digits only.");
in.next();//Go to next
}
//stops infinite loop by requesting Scanner try again
System.out.println("Please enter the second number you wish to calculate.");
if(in2.hasNextInt()) {
b = in2.nextInt();
}
while (!in2.hasNextInt()) {
System.out.println("Invalid number. Please enter digits only.");
in2.next();//Go to next
}
//stops infinite loop by requesting Scanner try again
BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
System.out.println("please enter one of the following operators + - / *");
sum = br2.readLine();
if ("+".equals(sum))
{
int c = a + b;
System.out.println(a + "+" + b + "=" + c);
}
if ("-".equals(sum))
{
int c = a - b;
System.out.println(a + "-" + b + "=" + c);
}
if ("/".equals(sum))
{
int c = a / b;
System.out.println(a + "/" + b + "=" + c);
}
if("*".equals(sum))
{
int c = a * b;
System.out.println(a + "*" + b + "=" + c);
}
}
}
我的问题是怎么回事,我做错了什么?以及如何解决它,以便我只需要输入每个数字一次(并且两者都可以工作而不是给出 0)