sid's

OPen Discussion
 
HomePortalCalendarFAQSearchRegisterMemberlistLog in

Share | 
 

 PROGRAMMING EXAMPLE..

View previous topic View next topic Go down 
AuthorMessage
Admin
Admin


Posts : 50
Join date : 2007-08-10
Age : 38
Location : Kuala Lumpur

PostSubject: PROGRAMMING EXAMPLE..   Fri Sep 14, 2007 12:12 pm

Arrow ok..here are some examples that will help you out..!!

import javax.swing.*;

public class EnterNumbers {

public static void main(String[] args){

String input, outMessage;
int maxNum,count;
double num=0,sum=0;


input = JOptionPane.showInputDialog("How many numbers do you want to enter?");
maxNum= Integer.parseInt(input);

outMessage = "This program will ask you to enter " +maxNum +" numbers";


JOptionPane.showMessageDialog(null,outMessage,"Numbers",
JOptionPane.INFORMATION_MESSAGE);

count = 1;

while (count<=maxNum){

input = JOptionPane.showInputDialog("Enter number " + count + ":");
num= Double.parseDouble(input);

JOptionPane.showMessageDialog(null,"The number entered is " + num,"Numbers",
JOptionPane.INFORMATION_MESSAGE);
sum=sum+num;

count++;
}

JOptionPane.showMessageDialog(null,"The total of "+maxNum+" numbers entered is : " + sum,"Numbers",
JOptionPane.INFORMATION_MESSAGE);
System.out.println(sum);



System.exit(0);
}//end of main
}//end of class
Back to top Go down
View user profile http://sidnazuha.forumotion.com
Admin
Admin


Posts : 50
Join date : 2007-08-10
Age : 38
Location : Kuala Lumpur

PostSubject: NESTED FOR   Fri Sep 14, 2007 12:13 pm

Exclamation Nested for, try to figure out how it works..

public class NestedFor1 {

public static void main(String[] args){

for(int x=0; x<4; x++){
for(int y= x ;y<4; y++){
System.out.print("*");
}
System.out.println();
}

}
}
Back to top Go down
View user profile http://sidnazuha.forumotion.com
Admin
Admin


Posts : 50
Join date : 2007-08-10
Age : 38
Location : Kuala Lumpur

PostSubject: Formatted out..   Fri Sep 14, 2007 12:16 pm

bounce useful examples to format ur output..

Arrow eg1:
import java.text.DecimalFormat;

/**
This program demonstrates the DecimalFormat class.
*/

public class Format3
{
public static void main(String[] args)
{
double number1 = 123.899;
double number2 = 1233.899;
double number3 = 12345.899;
double number4 = 123456.899;
double number5 = 1234567.899;

// Create a DecimalFormat object.
DecimalFormat formatter = new DecimalFormat("#,##0.00");

// Display the formatted variable contents.
System.out.println(formatter.format(number1));
System.out.println(formatter.format(number2));
System.out.println(formatter.format(number3));
System.out.println(formatter.format(number4));
System.out.println(formatter.format(number5));
}
}


Arrow eg2:import java.text.DecimalFormat;

/**
This program demonstrates the DecimalFormat class.
*/

public class Format2
{
public static void main(String[] args)
{
double number1 = 0.166666666666667;
double number2 = 1.666666666666667;
double number3 = 16.666666666666667;
double number4 = 166.666666666666667;

// Create a DecimalFormat object.
DecimalFormat formatter = new DecimalFormat("000.00");

// Display the formatted variable contents.
System.out.println(formatter.format(number1));
System.out.println(formatter.format(number2));
System.out.println(formatter.format(number3));
System.out.println(formatter.format(number4));
}
}
Back to top Go down
View user profile http://sidnazuha.forumotion.com
Sponsored content




PostSubject: Re: PROGRAMMING EXAMPLE..   

Back to top Go down
 
PROGRAMMING EXAMPLE..
View previous topic View next topic Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
sid's :: TEACHING MATERIAL :: UPM-SAK3100-
Jump to: