为什么我要输入两次数字?为什么第一个数字只给0?

时间:2013-06-12 20:41:03

标签: java

好吧我正在尝试创建一个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 个答案:

答案 0 :(得分:1)

  • 您似乎没有向a分配任何内容,因此始终为零。

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

    in.next();//Go to next
    

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

  • 如果缓冲输入,使用两个扫描仪也会搞砸。使用 完成相同的扫描仪。

答案 1 :(得分: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();
        }

答案 2 :(得分: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.");
    }
}

答案 3 :(得分:0)

我为您更正了您的计算器程序。代码存在小问题: 它遗漏了另外两个陈述 你没有设置“a”变量。

这是更正后的代码:

public static void main(String args [])抛出IOException {         //这里是TODO代码应用程序逻辑         布尔测试= true;         Scanner in = new Scanner(System.in);         //         int a = 0;         int 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);
    }
}