2017-03-24 5 views
0
//Payment Process  
     System.out.println("Order payment");    
     System.out.println("-------------");    
     System.out.println("");  
     System.out.printf("$%.2f remains to be paid. Enter coin or note: ", totalPrice);  
     double payment;  
     payment = scan.nextInt();  
     while (payment <= totalPrice) {  
      System.out.printf("$%.2f remains to be paid. Enter coin or note:", totalPrice - payment);  
      payment += scan.nextInt();  

私は自分のプログラムからコーヒーを購入するようにUniのプロジェクトを作成しています。これは、支払い処理セクションのコードのスニペットです。
1. $で始まる入力(たとえば10は無効ですが、$ 10.00は有効です)の場合にのみプログラムを動作させるにはどうすればいいですか
2。ユーザーが$ 100.00、$ 50.00、$ 20.00、$ 10.00、$ 5.00、$ 2.00、$ 1.00、$ 0.50、$ 0.20、$ 0.10、$ 0.05のみを入力できるようにするにはどうすればよいですか。

+0

あなたは文字列を返すscan.next() ''で次の入力行を解析し、この文字列は、あなたの条件を満たしているかどうかをチェックでき - あなたがString.startsWith 'でそれを行うことができます(文字列) 'またはRegExパターン –

+0

このアプリケーションにフロントエンドがある場合は、html、スクリプトを使用して – emotionlessbananas

+1

を検証してください。 –

答えて

0

私は文字列なので、それはテキストが$で始まるかどうかを確認することができます最初、小さなプログラムを書きました。あなたのプログラムは数字を使用しているので、この後に$を削除することによって、文字列はdoubleに変換されます。

public static void main(String[] args) { 
     // TODO code application logic here 
     Scanner input = new Scanner(System.in); 

     final String payment = input.next(); 
     if(payment.startsWith("$") && payment.matches(".*\\.\\d\\d")) 
     { 
      double paymentConverted = Double.parseDouble(payment.substring(1)); 
      // write ur others logic here 
     } 
     else 
     { 
      System.out.println("Invalid text !"); 
     } 
    } 

startWith()列がドット後の2桁で終了した場合、最初の値をチェックするmatches(".*\\.\\d\\d")チェックを使用しました。


の間、あなたは間違っていない場合は、あなたのコードにいくつかの誤り

double payment;  
payment = scan.nextInt(); 

nextDouble()する必要があります持っています。あなたの入力はドル記号($)で始まる場合、あなたは関数にコードを移動し、そのよう

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

    boolean isValid = false; 
    String input; 
    do { 
    System.out.printf("input: "); 
    input = sc.next(); 

    //check if the input is valid 
    if((input != null) && (input.length() > 0)) { 
     if('$' == input.charAt(0)) { 

     isValid = true; 
     break; 
     } 
    } 

    System.err.printf("Your input was invalid! input:%s%n", input); 
    } while(!isValid); 

    sc.close(); 

    System.out.printf("Your input was correct! input:%s%n", input); 
} 

ではなく、粗しかし、簡単な答え、それをチェックすることができ このチェック:質問1について

0

答え質問1 & 2用:

すべての有効な入力が含まれており、入力が非常に設定することに含まれているかどうかを確認の設定を行います。

public static void main(String[] args) { 
    Set<String> validInputs = new HashSet<>(); 

    validInputs.addAll(Arrays.asList("$100.00", "$50.00", "$20.00", "$10.00", "$5.00", "$2.00", "$1.00", "$0.50", "$0.20", "$0.10", "$0.05")); 

    Scanner sc = new Scanner(System.in); 

    boolean isValid = false; 
    String input; 
    do { 
    System.out.printf("input: "); 
    input = sc.next(); 

    // check if the input is valid 
    if(validInputs.contains(input)) { 
     isValid = true; 
     break; 
    } 

    System.err.printf("Your input was invalid! input:%s%n", input); 
    } while(!isValid); 

    sc.close(); 

    System.out.printf("Your input was correct! input:%s%n", input); 
} 
0
  1. 利用nextLine()の代わりに、nextInt()$の添加は、入力文字列なるため、入力を読み込みます。

  2. 許容可能な入力である事前設定値の短いリストがある場合、入力を検証する最も簡単な方法は、ユーザーからの行を読み取って、これを既に知っている許容可能な値のリストと単純に比較することです。

    // Create a list of allowed values 
        List<String> allowedValues = new ArrayList<>(Arrays.asList("$100.00", "$50.00", "$20.00")); 
    
        // read user input 
        String input = scan.nextLine(); 
    
        // use the built-in contains() function on the List object 
        if (!allowedValues.contains(input)) { 
         System.out.println("Your input is not valid."); 
        } 
        System.out.println("Your input is valid."); 
    
関連する問題