我有问题在这里我希望我的程序循环,直到用户说它停止。我希望用户知道他或她是否希望它继续下去(然后键入"是")或者如果它想要它停止例如(然后键入"停止")。
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);
}
}
答案 0 :(得分:1)
if(!"no".equals(userInput)){
calculator();
}else{
System.exit(0);
}
字符串比较使用.equals()
答案 1 :(得分:0)
变化
List
到
if(userInput != "no"){
字符串必须通过.equals()方法进行比较,因为它们是对象。 ==方法将比较它们的内存位置而不是它们的值。