This is a change problem, I need to enter the price and payment, then it will compute the change a customer should receive when he has paid a certain sum(in coins and bills).
import java.util.Scanner;
public class Change {
public static void main(String[] args) {
int change;
int a; //a stands for 1000 kr bills.
int b; //b stands for 500 kr bills.
int c; //c stands for 200 kr bills.
int d; //c stands for 100 kr bills.
int e; //e stands for 50 kr bills.
int f; //f stands for 20 kr bills.
int g; //g stands for 10 kr coins.
int h; //h stands for 5 kr coins.
int i; //i stands for 2 kr coins.
int j; //j stands for 1 kr coins.
Scanner sc = new Scanner(System.in);
System.out.println("Price:");
double p = sc.nextDouble(); // p stands for the price.
System.out.println("Payment:");
int k = sc.nextInt(); //k stands for the payment.
int q = (int)Math.round(p);
change = k-q;
a = (k-q)/1000;
b = ((k-q)-(a*1000))/500;
c = ((k-q)-(a*1000+b*500))/200;
d = ((k-q)-(a*1000+b*500+c*200))/100;
e = ((k-q)-(a*1000+b*500+c*200+d*100))/50;
f = ((k-q)-(a*1000+b*500+c*200+d*100+e*50))/20;
g = ((k-q)-(a*1000+b*500+c*200+d*100+e*50+f*20))/10;
h = ((k-q)-(a*1000+b*500+c*200+d*100+e*50+f*20+g*10))/5;
i = ((k-q)-(a*1000+b*500+c*200+d*100+e*50+f*20+g*10+h*5))/2;
j = ((k-q)-(a*1000+b*500+c*200+d*100+e*50+f*20+g*10+h*5+i*2))/1;
System.out.println("Change:"+change);
System.out.println("1000 kr bills:"+a);
System.out.println("500 kr bills:"+b);
System.out.println("200 kr bills:"+c);
System.out.println("100 kr bills:"+d);
System.out.println("50 kr bills:"+e);
System.out.println("20 kr bills:"+f);
System.out.println("10 kr coins:"+g);
System.out.println("5 kr coins:"+h);
System.out.println("2 kr coins:"+i);
System.out.println("1 kr coins:"+j);
}
}
When I run it, it just show the Price, and after I entered the price and tap enter it shows Exception in thread "main" java.util.InputMismatchException.
sc.readDouble(), but we should confirm this by looking at the stacktrace)