0

好吧,我正在尝试创建一个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)

4

4 回答 4

1
  • 你似乎没有分配任何东西,a所以它总是为零。

  • Scanner.next()Scanner.nextInt()都转到下一个令牌。你不需要这一行:

    in.next();//Go to next
    

    由于您没有使用它的返回值,它只会吞下一行输入。

  • 如果它们缓冲输入,使用两个扫描仪也会把事情搞砸。使用相同的扫描仪,直到完成。

于 2013-06-12T20:45:39.143 回答
0

零,因为您将两个 int 放入 b

  System.out.println("Please enter the first number you wish to calculate.");
        if(in.hasNextInt()) {
        a = in.nextInt();
        }
        while (!in.hasNextInt()) {
            System.out.println("Invalid number. Please enter digits only.");

}
        //stops infinite loop by requesting Scanner try again


        System.out.println("Please enter the second number you wish to calculate.");
        if(in.hasNextInt()) {
        b = in.nextInt();
        }
于 2013-06-12T20:46:02.307 回答
0

我为你更正了你的计算器程序。代码有一些小问题:1.它缺少两个 else 语句 2.你是“a”变量没有设置。

这是更正后的代码:

public static void main(String args[])throws IOException { // TODO code application logic here Boolean test = true; 扫描仪输入 = 新扫描仪(System.in);// int a = 0; 诠释 b = 0; 字符串总和;

    System.out.println("Please enter the first number you wish to calculate.");
    if(in.hasNextInt()) {
        a = in.nextInt();
    }else{
    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


    Scanner in2 = new Scanner(System.in);
    System.out.println("Please enter the second number you wish to calculate.");
    if(in2.hasNextInt()) {
        b = in2.nextInt();
    }else{
    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);
    }
}
于 2013-06-12T20:53:59.590 回答
0

你让你双重请求数字的原因是 Scanner.hasNextInt 正在检查是否有另一个 int,但输入是空的,所以它会卡在那里直到有其他东西。所以,当你输入第一个数字时,它会告诉你有另一个 int,然后你正在输入数字。

第二个问题是第一个数字请求您将值放在第二个数字上。

另一种简单的实现方法是:

System.out.println("Please enter the first number you wish to calculate.");
boolean ok = false;
while (!ok){
    try {
        a = in.nextInt();
        ok = true;
    } catch (Exception e){
        System.out.println("Invalid number.");
    }
}
于 2013-06-12T20:52:52.403 回答