2008年12月11日 星期四

simple java(十一)---application(3)

In Hong Kong, we have several kinds of coins, e.g. 10 dollars, 5 dollars, 2 dollars, and1 dollar coins. Write a program to let users input an amount, and then calculate the minimum numbers of coins for corresponding amounts of dollars.

solution:其實很簡單,我地輸入一個數字..比如輸入38..電腦就會計算...輸三個10元..一個五元..一個二元,一個一元.程式如下:

import javax.swing.*;
public class range {
public static void main( String args[] ){
String give;
int dol10,dol5,dol1,dol2,numbers;

give=JOptionPane.showInputDialog("input an amount");

numbers=Integer.parseInt(give);

dol10=numbers/10;
dol5=(numbers%10)/5;
dol2=(numbers%10%5)/2;
dol1=(numbers%10%5%2)/1;

JOptionPane.showMessageDialog(null,"10 dollars coins ="+dol10+"\n5 dollars coins
="+dol5+"\n2 dollars coins ="+dol2+"\n1 dollars coins ="+dol1);
System.exit(0);}}
********************************
Write an application that inputs one number consisting of five digits from the user,separates the number into its individual digits and prints the digits separated from oneanother by three spaces each. For example, if the user types in the number 12345, theprogram should print1 2 3 4 5
solution:
import javax.swing.*;

import javax.swing.JOptionPane;
public class range {
public static void main( String args[] ){
int number;String inputString;

inputString = JOptionPane.showInputDialog("Enter five digit integer:" );
number = Integer.parseInt( inputString );

int digit1, digit2, digit3, digit4, digit5;
digit1 = number / 10000;
digit2 = number % 10000 / 1000;
digit3 = number % 10000 % 1000 / 100;
digit4 = number % 10000 % 1000 % 100 / 10;
digit5 = number % 10000 % 1000 % 100 % 10;

String resultString = digit1 + " " + digit2 + " " +digit3 + " " + digit4 + " " + digit5 ;

JOptionPane.showMessageDialog( null, resultString,"Digits in " + number, JOptionPane.INFORMATION_MESSAGE );System.exit( 0 );}}

沒有留言: