简体   繁体   English

如何使计算器保持循环?

[英]How can i make my calculator to keep looping?

im having problem here i want my program to loop until the user say it to stop. 我在这里有问题我希望我的程序循环播放直到用户说它停止为止。 i want the user to know if he or she want it to continue eg (then type "yes") or if it wants it stop eg(then type "stop"). 我希望用户知道他或她是否希望继续(例如(然后键入“是”)),或者是否希望它停止(例如(然后键入“停止”))。

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);
    }


}
    if(!"no".equals(userInput)){
        calculator();
    }else{
        System.exit(0);
    }

String compare use .equals() 字符串比较使用.equals()

change 更改

if(userInput != "no"){

to

if(!userInput.equals("no")){

Strings must be compared by the .equals() method, because they are objects. 字符串必须通过.equals()方法进行比较,因为它们是对象。 The == method will compare their memory location instead of their value. ==方法将比较它们的内存位置而不是它们的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM