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);
}
}
}
我正在尝试制作一个计算器,但我遇到了一个小问题...... 问题发生在
之后tryAgain = input.next();
我不认为我的其余代码正在运行 扫描仪工作正常,但在输入内容后程序退出。
答案 0 :(得分:1)
您无法使用==
来比较strings
中的java
。使用:
tryAgain = input.next();
if(tryAgain.equals("yes")){
re = true;
restart(re);
etc...
如果你不关心大写,也可以使用.equalsIgnoreCase
答案 1 :(得分:0)
使用equals()
方法代替==
if(tryAgain.equals("yes")){
restart(true);
}else if (tryAgain.equals("no")){
restart(false);
}
答案 2 :(得分: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);
}
}
}
我所做的唯一改变就是tryAgain == "yes"
行!使用equals()或equalsIgnoreCase()来比较字符串!
看看exapmle:
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