2016-09-26 67 views
-1

我想编程一个简单的计算器程序。但问题是,当用户完成他的计算,程序终止。所以我试图以这样一种方式来完成计算,程序会问你是否希望程序终止。但是现在我不知道如何无限地重复计算器程序。如何重复部分代码?

我的代码(它是不完整):

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? 
     } 
    } 
} 
+1

什么'loop':https://docs.oracle。 com/javase/tutorial/java/nutsandbolts/while.html? – Shahid

+0

做..虽然会对你有所帮助 – Venkat

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

} 

} 
+0

为什么我们不应该使用“==”来比较字符串? – TheEliteAlien

+0

@TheEliteAlien看看[this](http://stackoverflow.com/a/513839/5333936)回答。 – Blobonat

+0

感谢您的帮助! – TheEliteAlien

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

     } 
-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方法(如适用),它会提高可用性最终用户键入他们的愿望

+2

你比较错误的字符串。 – Kayaman