2016-09-26 6 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

whileループを使用して望むように入力し、エンドユーザーの利便性を高めます:https://docs.oracle。 com/javase/tutorial/java/nutsandbolts/while.html? – Shahid

+0

Do ..役に立つと思いますが – Venkat

+0

ユーザーがNo. –

答えて

1

は何loop` `について

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

関連する問題