Upload files to "OOP/Java/Lab/Week10"
This commit is contained in:
parent
22f306573d
commit
0b8cfc509f
5 changed files with 162 additions and 0 deletions
46
OOP/Java/Lab/Week10/Sum.java
Normal file
46
OOP/Java/Lab/Week10/Sum.java
Normal file
|
@ -0,0 +1,46 @@
|
|||
import java.util.*;
|
||||
class floatnum extends Exception
|
||||
{
|
||||
float input;
|
||||
floatnum(float num)
|
||||
{
|
||||
input=num;
|
||||
}
|
||||
public String toString()
|
||||
{
|
||||
return "Error:"+input+" is a Floating point number";
|
||||
}
|
||||
}
|
||||
class Input_Exception
|
||||
{
|
||||
public static void main(String[] args) {
|
||||
int n,sum=0;
|
||||
float input=0;
|
||||
Scanner in=new Scanner(System.in);
|
||||
System.out.println("Enter the numbers one at a time,enter -1 to calculate the sum");
|
||||
while(true)
|
||||
{
|
||||
try
|
||||
{
|
||||
input=in.nextFloat();
|
||||
if(input == -1)
|
||||
{
|
||||
System.out.println("Sum="+sum);
|
||||
break;
|
||||
}
|
||||
else if(input%1!=0)
|
||||
{
|
||||
throw new floatnum(input);
|
||||
}
|
||||
else
|
||||
{
|
||||
sum=sum+(int)(input);
|
||||
}
|
||||
}
|
||||
catch(floatnum e)
|
||||
{
|
||||
System.out.println(e.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue