## Trending News

# 有關Java的問題 ~ 不想要顯示出有關0的

import java.util.Scanner;

public class xx

{

public static void main(String[] args)

{

Scanner input = new Scanner(System.in);

System.out.println("Enter an amount");

double amount = input.nextDouble();

int remainingamount = (int)(amount * 100);

int dollars = remainingamount / 100;

int remainingDollars = remainingamount % 100;

int quarters = remainingDollars / 25;

int remainingQullars = remainingDollars % 25;

int dims = remainingQullars / 10;

int remainingDims = remainingQullars % 10;

int nickles = remainingDims / 5;

int remainingNickles = remainingDims % 5;

int pennies = remainingNickles;

String output = "Your amount " + amount + " consists of : \n" +

dollars + " dollars\n" + quarters + " quarters\n" + dims + " dims\n" + nickles + " nickles\n" + pennies + " pennies";

System.out.println(output);

}

}

這是程式的語法

例如輸入11.56

會顯示出

11 dollars

2 quarters

0 dims

1 nickles

1 pennis

我不想要讓

" 0 dims "

顯示出來

要怎麼改 ? ? ?

### 1 Answer

- FuhuaLv 41 decade agoFavorite Answer
這個可以在

String output = "Your amount " + amount + " consists of : \n" +

dollars + " dollars\n" + quarters + " quarters\n" + dims + " dims\n" + nickles + " nickles\n" + pennies + " pennies";

這段作手腳，只要判斷那些值不等於才相加

如

String output = "Your amount " + amount + " consists of : \n";

if (dollars > 0) {

output = output + dollars + " dollars\n";

}

if (quarters > 0) {

output = output + quarters + " quarters\n";

}

if (dims > 0) {

output = output + dims + " dims\n";

}

if (nickles > 0) {

output = output + nickles + " nickles\n";

}

if (pennis > 0) {

output = output + pennis + " pennis\n";

}

最後再把output值印出

System.out.println(output);

Source(s): me