即時通訊問題在這裏我想我的程序循環,直到用戶說停止。我希望用戶知道他或她是否希望它繼續例如(然後鍵入「是」),或者如果它希望它停止例如(然後鍵入「停止」)。如何讓我的計算器保持循環?
import java.util.Scanner;
public class Calculator2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String userInput ;
while(true){
calculator();
System.out.println("would you like to make another calculation");
userInput = scan.nextLine();
if(userInput != "no"){
calculator();
}else{
System.exit(0);
}
}
}
public static void calculator()
{
// TODO code application logic here
Scanner user = new Scanner(System.in);
System.out.println("Enter ");
double value1 = user.nextDouble();
String op = user.next();
double value2 = user.nextDouble();
if (op.equals("+")){
Addition(value1,value2);
}if (op.equals("-")){
Subtraction(value1,value2);
}if(op.equals("/")){
Division(value1,value2);
}if(op.equals("*")){
Multiplication(value1,value2);
}if(op.equals("^")){
Exponent(value1,value2);
}if(op.equals("log")){
NaturalLog(value1);
}else{
}
}
public static void Addition (double value1, double value2){
double Sum= value1 + value2;
System.out.println(Sum);
}
public static void Subtraction (double value1 , double value2){
double diff = value1 - value2;
System.out.println(diff);
}
public static void Division(double value1 , double value2){
double div = value1/value2;
System.out.println(div);
}
public static void Multiplication(double value1, double value2){
double mult = value1 * value2;
System.out.println(mult);
}
public static void Exponent(double value1 , double value2){
double exp = Math.pow(value1, value2);
System.out.println(exp);
}
public static void NaturalLog(double value1){
double logs = Math.log(value1);
System.out.println(logs);
}
}
字符串不被評估。有趣的是,適當地使用'。等於()'在其他地方。 – KevinO