Java 为什么我的计算器会自动回答“为什么?”;0“;在我登录之前?

Java 为什么我的计算器会自动回答“为什么?”;0“;在我登录之前?,java,eclipse,Java,Eclipse,在15号线附近的某个地方,它给了我一些问题 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //scanner object created System.out.println("Enter your first number"); int nr1 = sc.nextI

在15号线附近的某个地方,它给了我一些问题

 import java.util.Scanner;

 public class Main 
 {
 public static void main(String[] args) 
 {
 Scanner  sc = new Scanner(System.in); //scanner object created

 System.out.println("Enter your first number");
  int nr1 = sc.nextInt();
 System.out.println("Enter your second number");
 int nr2 = sc.nextInt();
 System.out.println("Enter your sign (+ , - , /, *)");
 String anvin = sc.nextLine();
 int ans = 0; 
//这条线周围的某个地方就是它遇到问题的地方。我还没来得及插上牌子,它就给了我答案

 if(anvin.equalsIgnoreCase("+")) {
 ans = nr1 + nr2;
 }
 else if(anvin.equalsIgnoreCase("-")) {
 ans = nr1 - nr2;
 }
 else if(anvin.equalsIgnoreCase("*")) {
 ans = nr1 * nr2;
 }
 else if(anvin.equalsIgnoreCase("/")) {
 ans = nr1 / nr2;
 }

System.out.println(ans);

System.out.println("To continue type yes");
String yes= sc.nextLine();

if(yes.equalsIgnoreCase("yes")) {
return;

}
}
}
在我输入符号之前,无论我输入什么,它都会回答“0”

Enter your first number
9
Enter your second number
9
Enter your sign (+ , - , /, *)
0
To continue type yes

请告诉我我做错了什么,并可能加以纠正,以便我能进一步理解

我建议您,您不仅可以学习使用对象,还可以学习编写托管代码的更好方法,
Calculator.java
->a类

import java.util.Scanner;
public class Calculator {

public static void main(String[] args) {
    //Instantiate
    Scanner input = new Scanner(System.in);
    Calculations calc = new Calculations();

    // Variable declarations
    double answer = 0, entry1 , entry2 ;
    char operator;

    // Start 
    System.out.println("***** Welcome to the Command line calculator program *****");
    System.out.print("Please enter the first number :");
    entry1 = input.nextDouble();
    System.out.print("Please enter the second number:");
    entry2 = input.nextDouble();
    System.out.println("Please enter the operation : ");
    System.out.println("***** Operations :- + -> Add ; - ->Substract ; / -> Divide ; * -> Multiply ; ^ : Power *****");
    operator = input.next().charAt(0);

    // Switch case

    switch (operator){

        case '+' : answer = calc.add(entry1, entry2);
            break;
        case '-' : answer = calc.substract(entry1, entry2);
            break;
        case '/' : answer = calc.divide(entry1, entry2);
            break;
        case '*' : answer = calc.multiply(entry1, entry2);
            break; 
        case '^' : answer = calc.power(entry1, entry2); 
            break;
    }

    System.out.println(entry1 + " " + operator + " " + entry2 + " = " + answer);    
}
}`
Calculations.java
-->另一个包含计算的类

import java.math.*;
public class Calculations {
    // Addition Method
    double add (double first, double second){
        double answer = first + second;
        return answer;
    }
    // Substraction Method
    double substract (double first, double second){
        double answer = first - second;
        return answer;
    }
    // Multiplication Method
    double multiply (double first, double second){
        double answer = first * second;
        return answer;
    }
    // Division Method
    double divide (double first, double second){
        double answer = first / second;
        return answer;
    }
    // Power Method
    double power(double a, double b){
        double answer =Math.pow(a, b);
        return answer;
    }

} 
改变

还要记住,您可以除以零;-)

编辑:

也改变

String yes= sc.nextLine();


尝试将
sc.nextInt()
行更改为
Integer.parseInt(sc.nextLine())
。这将使您的代码正常工作

编辑:更新代码,使其包含while循环,以便您可以对每个注释执行多次运行。这还需要将最后一个if语句更改为
break而不是
返回

Scanner sc = new Scanner(System. in ); //scanner object created

while (true) {
    System.out.println("Enter your first number");
    int nr1 = Integer.parseInt(sc.nextLine());
    System.out.println("Enter your second number");
    int nr2 = Integer.parseInt(sc.nextLine());
    System.out.println("Enter your sign (+ , - , /, *)");
    String anvin = sc.nextLine();
    int ans = 0;
    //somewhere around this line is where it is having the problems. it gives me the answer before i can put in my sign;
    if (anvin.equalsIgnoreCase("+")) {
        ans = nr1 + nr2;
    } else if (anvin.equalsIgnoreCase("-")) {
        ans = nr1 - nr2;
    } else if (anvin.equalsIgnoreCase("*")) {
        ans = nr1 * nr2;
    } else if (anvin.equalsIgnoreCase("/")) {
        ans = nr1 / nr2;
    }

    System.out.println(ans);
    System.out.println("To continue type yes");
    String yes = sc.nextLine();

    if (!yes.equalsIgnoreCase("yes")) {
        break;
    }
}

而不是
sc.nextLine()使用
sc.next()

好的,我会测试一下,看看它是否解决了我的问题。当然,如果您希望继续当前的方式,请尝试更改
String anvin=sc.nextLine()
字符串anvin=sc.next()请缩进您的代码,以便我们更容易理解。除此之外,好问题!大写和标点符号可以使用工作,但可读性很强。这让它工作起来了。现在我必须弄清楚为什么当我输入“是”返回时它不返回你甚至不需要最后一部分。。。无论您键入“否”、“bob”、“joe”、“random”或“yes”,方法的结尾都会被击中。这和你的报税表是一样的。我有没有办法让它再做一个等式?是的,如果这里的答案有帮助的话,如果你能接受最好的答案,我们将不胜感激。
String yes= sc.nextLine();
String yes= sc.next();
Scanner sc = new Scanner(System. in ); //scanner object created

while (true) {
    System.out.println("Enter your first number");
    int nr1 = Integer.parseInt(sc.nextLine());
    System.out.println("Enter your second number");
    int nr2 = Integer.parseInt(sc.nextLine());
    System.out.println("Enter your sign (+ , - , /, *)");
    String anvin = sc.nextLine();
    int ans = 0;
    //somewhere around this line is where it is having the problems. it gives me the answer before i can put in my sign;
    if (anvin.equalsIgnoreCase("+")) {
        ans = nr1 + nr2;
    } else if (anvin.equalsIgnoreCase("-")) {
        ans = nr1 - nr2;
    } else if (anvin.equalsIgnoreCase("*")) {
        ans = nr1 * nr2;
    } else if (anvin.equalsIgnoreCase("/")) {
        ans = nr1 / nr2;
    }

    System.out.println(ans);
    System.out.println("To continue type yes");
    String yes = sc.nextLine();

    if (!yes.equalsIgnoreCase("yes")) {
        break;
    }
}