2016-10-05 5 views
-1

私はソーダマシンからアイテムのコストを入力し、支払った金額の変更を返す必要がある私のAP Compsciクラスのプロジェクトを持っています。"while(x || y)" Javaのループを使用

しかし、私は入力に少し問題があります。私は入力が2倍であることを保証するためにwhileループを使用しています。支払額の値はコスト以上です。 これをテストすると、スキャンするために支払った金額を入力する必要があるようです(23〜30行目を参照)。

私は、 "scan.next()"をループ内の別の場所に移動し、 "scan.next()"を "scan.nextLine"に変更しようとしましたが、 。誰かが、入力が倍以上でないかどうかを確認できる方法を知っていますか?そうであれば、再度入力するようにユーザーに促しますか?私は以下のコードを貼り付けました:

import java.util.Scanner; 
import java.lang.Math; 

public class Sodamachine 
{ 
public static void main(String [] args) 
{ 
    Scanner scan = new Scanner(System.in); 

    System.out.println("SODA MACHINE: V1.2"); 
    System.out.println("Input the cost of the soda can and the amount you put in and your change will"); 
    System.out.println("be output."); 

    System.out.print("\nEnter cost of purchase below:\n$"); 
    while (! scan.hasNextDouble()) 
    { 
     System.out.println("ERROR: Input was not a real number."); 
     System.out.print("Enter cost of purchase below:\n$"); 
     scan.nextLine(); 
    } 
    double cost = scan.nextDouble(); 

    //this is where the problem starts 

    System.out.print("Enter amount paid below:\n$"); 
    while ((! scan.hasNextDouble()) || (scan.nextDouble() < cost)) 
    { 
     System.out.println("ERROR: Improper input."); 
     System.out.print("Enter amount paid below:\n$"); 
     scan.next(); 
    } 
    double paid = scan.nextDouble(); 
} 
} 
+2

一部の時間を2回スキャンしています。入力ごとに 'next() 'を呼び出すように書き直す必要があります。 –

+0

入力を文字列として取得しようとすると、ユーザーにもう一度プロンプトを表示せずに、必要なすべてのチェックを行うことができます(たとえば、ダブルの場合)。 – Sentry

答えて

0

私の非常に基本的で似たような解決策を試してください。

要件:totalCostおよびtotalPaidの値は、DOUBLE変数でなければなりません。 これらの変数がDOUBLEでない場合は、プログラムが停止し、再度実行する必要があります(もちろん、他のロジックのループを追加してtotalCostとtotalPaidを何度も呼び出すことができます)。

私の解答の基本的な仮定:totalPaid> totalCost。

もちろん、私のコードを変更して改善することができます。この解決法は、あなたの問題をどのように解決するかという考え方にすぎません。あなたはしばらくの間、複数回のスキャンを避けたい

public static void main(String[] args) 
{ 
    Scanner scan = new Scanner(System.in); 

    System.out.println("SODA MACHINE: V1.2"); 
    System.out.println("Input the cost of the soda can and the amount you put in and your change will"); 
    System.out.println("be output."); 

    double totalCost = 0.0; 

    System.out.print("\nEnter cost of purchase below:\n$"); 
    try{ 
     totalCost = scan.nextDouble(); 
    } 
    catch (Exception e) 
    { 
     System.out.println("ERROR: Input was not a real number - please provide DOUBLE value."); 
     System.exit(0); 
    }    

    System.out.print("Enter amount paid below:\n$"); 

    double totalPaid = 0.0; 

    try{ 
     totalPaid = scan.nextDouble(); 
    } 
    catch (Exception e) 
    { 
     System.out.println("ERROR: Input was not a real number - please provide DOUBLE value."); 
     System.exit(0); 
    } 

    System.out.println("Change:\n$" + (totalPaid - totalCost)); 
} 
0

は、あなたのコストは100であると有料loop.Supposeだからあなたのwhileループが

scan.nextDouble()<コストを行くために起こっている90です)< - 最初のスキャン(コストよりも少ない)。

scan.next(); < - 2回目のスキャン。

scan.nextDouble()<コスト)< - whileループ状態に戻り、余分なスキャンがあります!

double paid = scan.nextDouble(); < - そして、whileループを終了すると、さらに別のスキャンがあります!

理想的には、1回スキャンし、条件が満たされない場合は、新しい入力を再度スキャンします。

//method to check if String can be converted to Double 
    public static boolean isDouble(String input){ 
     try{ 
      Double.parseDouble(input); 
     }catch(NumberFormatException e){ 
      return false; 
     } 
     return true; 
    } 

    String paid = scan.next(); //scan once 

    //while not double or paid less than cost, scan again 
    while (!isDouble(paid) || (Double.parseDouble(paid) < cost)){ 
     System.out.println("Wrong paid number"); 
     paid = scan.next(); 
    } 

    double paidDouble = Double.parseDouble(paid);