对自动售票机的例子进一步升级,如果第一次投币5元,第二次投币6元,这种应该找零(5+6)-10=1元。并且让程序循环,只要多次投币的金额超过10元,就计算。
public class Main {
<span style="white-space:pre"> </span>public static void main(String[] args) {
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>Scanner in = new Scanner(System.in);
<span style="white-space:pre"> </span><span style="color:#ff0000;">int balance = 0;</span>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span><span style="color:#ff0000;"><strong>while(true)</strong></span>{
<span style="white-space:pre"> </span>System.out.print("请投币:");
<span style="white-space:pre"> </span>int amount = in.nextInt();
<span style="white-space:pre"> </span><span style="color:#ff0000;"><strong>balance = balance +amount;</strong></span>
<span style="white-space:pre"> </span><strong><span style="color:#ff0000;">if(balance >= 10)</span></strong>{
<span style="white-space:pre"> </span>System.out.println("**Java城际铁路专线**");
<span style="white-space:pre"> </span>System.out.println(" 票价10元 ");
<span style="white-space:pre"> </span>System.out.println("******************");
<span style="white-space:pre"> </span>System.out.println("找零" + (balance - 10) +"元");
<span style="white-space:pre"> </span><span style="color:#ff0000;">balance = 0;</span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int x = in.nextInt();
int count = 1;
while(x>=10){
x/=10;
count++;
}
System.out.println(count);
}
}