2016-12-01 6 views
0

小切手に印刷する金額を入力するアプリケーションを作成しようとしていますが、大文字の****アスタリスクで保護されている小切手で番号を印刷する方法がわかりません。チェック保護フォーマット

ここで私が持っているコードは、これまでだ

import java.text.DecimalFormat; 
import java.util.Scanner; 
import java.text.NumberFormat; 
import java.util.Locale; 
public class CheckProtection 
{ 
    private static double maxAmount = 1000; 
    public static void main (String args []) 
    { 

     //System.out.printf(checkFormatter.format(check)); 
     //DecimalFormat checker = new DecimalFormat("******.**"); 
     //System.out.println(checker); 
     boolean validEntry = false; 
     while (validEntry == false) 
     { 
      Scanner userEntry = new Scanner(System.in); 
      System.out.println("Enter The check amount under a $1000.00 and greater that 0."); 
      if (userEntry.hasNextDouble() == true) 
      { 
       double check = userEntry.nextDouble(); 
       if (check > 0) 
       { 
        if(check < maxAmount) 
        { 
         validEntry = true; 

         NumberFormat checkFormatter = NumberFormat.getCurrencyInstance(); 
         checkFormatter.format(check); 
         System.out.printf("%5s",+ check); 
        } 
       } 
      } 
     } 








    } 
+0

コードダンプ以上のものが必要です。いくつかのインプット/期待アウトプット/実際のアウトプットとは何ですか? – rmlan

+0

入力:3.54,525.43,72.50アウトアウト:*** $ 3.54、** $ 525.43、*** $ 72.50、 –

答えて

0

編集:コメントに与えられたサンプルの入力と出力期待して、私の前の答えは正しくありませんでした。

を考えると、これらの入力の各々、この更新が期待される出力を生成する必要があります

String value = "$" + String.format("%.2f", check); 
System.out.println(value); 
String check_fmt = ("*********" + value).substring(value.length()); 
System.out.println(check_fmt); 

注:this answerから借りた創意工夫を

古い答え:それは、printf()で行うことができるわからない場合はString.format作品、

String result = String.format("%6.2f", check); // 6 spaces width, 2 spaces precision, 'f' converts the double 
System.out.println(result.replace(" ", "*")); 

注:6スペース幅= 3人のスペースを使用leドル+セントスペース2スペース+小数第1スペース