0% found this document useful (0 votes)
30 views

Programming in Java - 9

The document provides 5 examples of Java programs that use loops and input/output operations. The examples include programs that calculate the average of input numbers, count positive/negative/zero numbers, generate random numbers, analyze invoice amounts, and demonstrate the break and continue statements. Loops like for and while are used to repeatedly accept user input and perform calculations on the data. Output is displayed using messages or print statements.

Uploaded by

abwyasra240
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Programming in Java - 9

The document provides 5 examples of Java programs that use loops and input/output operations. The examples include programs that calculate the average of input numbers, count positive/negative/zero numbers, generate random numbers, analyze invoice amounts, and demonstrate the break and continue statements. Loops like for and while are used to repeatedly accept user input and perform calculations on the data. Output is displayed using messages or print statements.

Uploaded by

abwyasra240
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

‫‪Programming in JAVA‬‬

‫المحاضرة السابعة‬

‫د‪.‬فاطمة قيلش‬
Outline

• Program enters n numbers then calculates the average.

• Program enters n numbers then displays the count of positive , zero , negative numbers.

• Program generates n random integer numbers.

• Program enters n invoices then displays the count and sum of many ranges.

• break statement

• continue statement
Example 9.1:Program prompts the user to
enter n number then display the average
public static void main(String[] args)
{
int i,n;
double x,sum,avg;
n=Integer.parseInt(JOptionPane.showInputDialog("n="));
sum=0;
for (i=1;i<=n;i++)
{
x=Double.parseDouble(JOptionPane.showInputDialog("x"+i));
sum=sum+x;
}
avg=sum/n;
JOptionPane.showMessageDialog(null,"avarage="+avg);
}
Example 9.2:Program prompts the user to enter many
numbers it stops when a negative number is entered then
display the average
public static void main(String[] args)
{
int i,n; double x,sum,avg;
x=Double.parseDouble(JOptionPane.showInputDialog("x1"));
sum=0;n=0;
while (x>=0)
{
sum=sum+x;
n++;
x=Double.parseDouble(JOptionPane.showInputDialog("x"+(n+1)));
}
if (n>0)
avg=sum/n;
else
avg=0;
JOptionPane.showMessageDialog(null,"avarage="+avg);
}
Example 9.3: Program prompts the user to enter n
number then display the count of positive , zero ,
negative numbers
public static void main(String[] args)
{
int i, n,x ,c1,c2,c3;
c1=c2=c3=0;
n=Integer.parseInt(JOptionPane.showInputDialog("n="));
for (i=1;i<=n;i++)
{
x=Integer.parseInt(JOptionPane.showInputDialog("x"+i+"="));
if (x>0) c1++;
else if (x==0) c2++;
else c3++;
}
JOptionPane.showMessageDialog(null,"positive="+c1+"\nzero="+c2+"\nnegative="+c3 );
}
Example 9.4: Program generates n random integer number at
the range [ 1, 6 ] then displays how many times of each
number
public static void main(String[] args)
{
int i,x,n,c1,c2,c3,c4,c5,c6;
c1=c2=c3=c4=c5=c6=0;
n=Integer.parseInt(JOptionPane.showinputDialog(“n=“));
for (i=1;i<=n++)
{
x=(int)(1+Math.random()*6);
switch(x)
{
case 1:c1++;break;
case 2:c2++;break;
case 3:c3++;break;
case 4:c4++;break;
case 5:c5++;break;
case 6:c6++;break;
}
}
System.out.println("c1="+c1+" c2="+c2+" c3="+c3+" c4="+c4+" c5="+c5+" c6="+c6);
Example 9.5: Program prompts the user to enter n invoices then
display the count and sum of each range: <5000 [5000 – 10000]
>10000
public static void main(String[] args)
{
int i, n , x , s1,s2,s3,c1,c2,c3;
s1=s2=s3=c1=c2=c3=0;
n=Integer.parseInt(JOptionPane.showInputDialog("n="));
for(i=1;i<=n;i++)
{
x=Integer.parseInt(JOptionPane.showInputDialog("money"+i+"="));
if (x<5000) { s1=s1+x; c1++;}
else if (x<=10000) { s2=s2+x; c2++;}
else { s3=s3+x; c3++;}
}
System.out.println("<5000 "+s1+" "+c1);
System.out.println("[5000-10000] "+s2+" "+c2);
System.out.println(">10000 "+s3+" "+c3);
}
break statement

public static void main(String[] args)


{
int i;
for (i=1;i<=10;i++)
{
if (i==5)
break;
System.out.println(i);
}
}
continue statement

public static void main(String[] args)


{
int i;
for (i=1;i<=10;i++)
{
if (i==5)
continue;
System.out.println(i);
}
}

You might also like