如何重复部分代码?

时间:2016-09-26 12:11:52

标签: java

我正在尝试编写一个简单的计算器程序。但问题是当用户完成计算时,程序终止。所以我尝试以这样的方式制作它,在完成计算后,程序会询问您是否希望程序终止。但现在我不知道如何无限地重复计算器程序。

我的代码(不完整):

import java.util.Scanner ;
public class NewSrc
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter first number.... ");
        double x = sc.nextDouble();
        System.out.println("Enter next number....");
        double y = sc.nextDouble();
        double z = x*y;
        System.out.println("processing answer ....");
        System.out.println("The answer is :- " + z );

        System.out.println("Do you want to continue ?Answer with either Yes or No ");
        String Agree = "Yes";
        String Disagree = "No";
        String Continue = sc.nextLine();
        if (Continue == Agree)
        {   
          //How to restart it?
        }
    }
 }

3 个答案:

答案 0 :(得分:1)

使用while循环

import java.util.Scanner ;
public class NewSrc {

 public static void main(String[] args) {

    boolean continueCalculation = true;
    Scanner sc = new Scanner(System.in);
    while (continueCalculation == true) {        
        System.out.println("Enter first number.... ");
        double x = sc.nextDouble();
        System.out.println("Enter next number....");
        double y = sc.nextDouble();
        double z = x*y;
        System.out.println("processing answer ....");
        System.out.println("The answer is :- " + z );
        System.out.println("Do you want to continue ?Answer with either Yes or No   ");
        String continueInput = sc.nextLine();
        if (continueInput.equals("No")) //do not use == to compare strings
        {
             continueCalculation = false;
        }
   }

 }

}

答案 1 :(得分:0)

程序中的一个小修改,根据用户输入的输入继续循环,否则停止

public static void main(String[] args) {

    while(true){
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter first number.... ");
             double x = sc.nextDouble();
            System.out.println("Enter next number....");
             double y = sc.nextDouble();
             double z = x*y;
            System.out.println("processing answer ....");
             System.out.println("The answer is :- " + z );

            System.out.println("Do you want to continue ?Answer with either Yes or No   ");
                String Disagree = "No";
                String Continue = sc.nextLine();
                if (Continue.equals(Disagree ))
                {
                   break;
                }
    }

        }

答案 2 :(得分:-1)

import java.util.Scanner ;
public class NewSrc
{
    public static void main(String[] args)
    {
        boolean doRepeat = false;
        Scanner sc = new Scanner(System.in);

        do{

        System.out.println("Enter first number.... ");
        double x = sc.nextDouble();
        System.out.println("Enter next number....");
        double y = sc.nextDouble();
        System.out.println("processing answer ....");
        System.out.println("The answer is :- " + (x*y) );

        System.out.println("Do you want to continue ?Answer with either Yes or No ");
        String userWish= sc.nextLine();
        if (userWish.equalsIgnoreCase("Yes"))
        {   
          doRepeat = true;
        }
        else doRepeat = false;

       }(doRepeat)
    }
 }
  

请不要使用额外的变量声明来打印输出

     

在适用的情况下使用EqualsIgnoreCase方法,它将增强最终用户按其愿望键入的可用性