Java 我正在做一个计算器

Java 我正在做一个计算器,java,Java,我想做一个计算器,但我遇到了一个小问题。。。 问题发生在之后 import java.util.Scanner; public class Calculator { static double num1 = 0, num2 = 0; static Scanner input = new Scanner(System.in); static int choice = 0; static double answer = 0; public static void main(String a

我想做一个计算器,但我遇到了一个小问题。。。 问题发生在之后

import java.util.Scanner;


public class Calculator {
static double num1 = 0, num2 = 0;
static Scanner input = new Scanner(System.in);
static int choice = 0;
static double answer = 0;



public static void main(String args []){
    enter(num1, num2);
}

public static void enter(double num1, double num2) {
    System.out.println("********CALCULATOR*********");
    System.out.println("Type in your first number.");
    num1 = input.nextDouble();
    System.out.println("Type in your second number.");
    num2 = input.nextDouble();
    System.out.println("Enter the number associated with what you would like to do.");
    System.out.println("1. Add them");
    System.out.println("2. Subtract them");
    System.out.println("3. Multiply them");
    System.out.println("4. Divide them");
    choice = input.nextInt();
    operations(num1, num2, choice);

}

public static void operations(double num1, double num2, int choice) {
    if(choice == 1){
        answer = num1 + num2;
    }else if(choice == 2){
        answer = num1 - num2;
    }else if(choice == 3){
        answer = num1 * num2;
    }else if(choice == 4){
        answer = num1 / num2;
    }
    printAnswer(answer);
}

public static void printAnswer(double answer) {
    boolean re;
    String tryAgain;
    System.out.println("The answer is " + answer);
    System.out.println("Would you like to use it again? Yes or No.");
    tryAgain = input.next();
    if(tryAgain == "yes"){
        re = true;
        restart(re);
    }else if (tryAgain == "no"){
        re = false;
        restart(re);
    }

}

public static void restart(boolean re) {
    if(re == true){
        enter(num1, num2);
    }
    if (re == false){
        System.exit(0);
    }

}
}
我不认为我的其余代码正在运行
扫描仪工作正常,但在我键入内容后程序退出。

您不能使用
==
java
中比较
字符串。使用:

tryAgain = input.next();
如果不关心大小写,也可以使用.equalsIgnoreCase

使用
equals()
方法,而不是==

tryAgain = input.next();
if(tryAgain.equals("yes")){
    re = true;
    restart(re);

etc...
我所做的唯一更改是在第
tryAgain==“yes”
行中!使用equals()或equalsIgnoreCase()来比较字符串

看看这个例子:

package test;

import java.util.Scanner;

public class Calculator {
    static double num1 = 0, num2 = 0;
    static Scanner input = new Scanner(System.in);
    static int choice = 0;
    static double answer = 0;

    public static void main(String args[]) {
        enter(num1, num2);
    }

    public static void enter(double num1, double num2) {
        System.out.println("********CALCULATOR*********");
        System.out.println("Type in your first number.\n");
        num1 = input.nextDouble();
        System.out.println("Type in your second number.\n");
        num2 = input.nextDouble();
        System.out
                .println("Enter the number associated with what you would like to do.");
        System.out.println("1. Add them");
        System.out.println("2. Subtract them");
        System.out.println("3. Multiply them");
        System.out.println("4. Divide them\n");
        choice = input.nextInt();
        operations(num1, num2, choice);

    }

    public static void operations(double num1, double num2, int choice) {
        if (choice == 1) {
            answer = num1 + num2;
        } else if (choice == 2) {
            answer = num1 - num2;
        } else if (choice == 3) {
            answer = num1 * num2;
        } else if (choice == 4) {
            answer = num1 / num2;
        }
        printAnswer(answer);
    }

    public static void printAnswer(double answer) {
        boolean re;
        String tryAgain;
        System.out.println("The answer is " + answer);
        System.out.println("Would you like to use it again? Yes or No.");
        tryAgain = input.next();
        if (tryAgain.equalsIgnoreCase("yes")) { // don't compare Strings using ==
            re = true;
            restart(re);
        } else if (tryAgain.equalsIgnoreCase("no")) {
            re = false;
            restart(re);
        }

    }

    public static void restart(boolean re) {
        if (re == true) {
            enter(num1, num2);
        }
        if (re == false) {
            System.exit(0);
        }

    }
}

请使用一个问题标题来描述您的问题,而不是您的一天。您需要学习如何比较字符串。我不知道问题是什么。然后获取问题并返回。然后调试您的程序并找出问题是什么。
package test;

import java.util.Scanner;

public class Calculator {
    static double num1 = 0, num2 = 0;
    static Scanner input = new Scanner(System.in);
    static int choice = 0;
    static double answer = 0;

    public static void main(String args[]) {
        enter(num1, num2);
    }

    public static void enter(double num1, double num2) {
        System.out.println("********CALCULATOR*********");
        System.out.println("Type in your first number.\n");
        num1 = input.nextDouble();
        System.out.println("Type in your second number.\n");
        num2 = input.nextDouble();
        System.out
                .println("Enter the number associated with what you would like to do.");
        System.out.println("1. Add them");
        System.out.println("2. Subtract them");
        System.out.println("3. Multiply them");
        System.out.println("4. Divide them\n");
        choice = input.nextInt();
        operations(num1, num2, choice);

    }

    public static void operations(double num1, double num2, int choice) {
        if (choice == 1) {
            answer = num1 + num2;
        } else if (choice == 2) {
            answer = num1 - num2;
        } else if (choice == 3) {
            answer = num1 * num2;
        } else if (choice == 4) {
            answer = num1 / num2;
        }
        printAnswer(answer);
    }

    public static void printAnswer(double answer) {
        boolean re;
        String tryAgain;
        System.out.println("The answer is " + answer);
        System.out.println("Would you like to use it again? Yes or No.");
        tryAgain = input.next();
        if (tryAgain.equalsIgnoreCase("yes")) { // don't compare Strings using ==
            re = true;
            restart(re);
        } else if (tryAgain.equalsIgnoreCase("no")) {
            re = false;
            restart(re);
        }

    }

    public static void restart(boolean re) {
        if (re == true) {
            enter(num1, num2);
        }
        if (re == false) {
            System.exit(0);
        }

    }
}
String s1 = "text1";
String s2 = "text1";
System.out.println(s1 == s2); // true 
System.out.println(s1.equals(s2)); // true

String s3 = new String("text1");
String s4 = new String("text1");
System.out.println(s3 == s4); // false because you are comparing references but not the objects
System.out.println(s3.equals(s4)); // true , because you are comapring objects