Вы находитесь на странице: 1из 2

import java.util.

Scanner;
public class Assignment {
public static void main(String[] args) {
// TODO Auto-generated method stub
String choice = "Y";
while(choice.equals("Y") || choice.equals("y"))
{
Scanner mychoice;
int num = 0;
int sumEven = 0;
int sumOdd = 0;
String sentEven = "Sum of Even Numbers: ";
String sentOdd = "Sum of Odd Numbers: ";
Scanner one = new Scanner (System.in);
System.out.println("Enter a number:");
String n = one.nextLine();
boolean isStringInt = false;
try{
Integer.parseInt(n);
isStringInt = true;
}
catch(NumberFormatException ex){}
if(isStringInt == false){
System.out.println("INVALID!");
System.out.println("Do you still want to
continue? Type 'Y' to continue while 'N' to exit.");
mychoice = new Scanner(System.in);
choice = mychoice.nextLine();
}
else if (isStringInt == true)
{
num = Integer.parseInt(n);
for(int x=1; x <= num; x++){
if(x%2 == 0)
{
sumEven = sumEven + x;
if(x == num || x == num-1){
sentEven = sentEven + x + " = "
+ sumEven;
}
else{
sentEven = sentEven + x + " + ";
}
}
else if(x%2 == 1){
sumOdd += x;
if((x == num) || (x == (num-1))){
sentOdd = sentOdd + x + " = " +
sumOdd;
}

else{
sentOdd = sentOdd + x + " + ";
}
}
}
System.out.println("Sum of Even Numbers: " +
sentEven);
System.out.println("Sum of Odd Numbers: " +
sentOdd);
System.out.println("Do you still want to
continue? Type 'Y' to continue while 'N' to exit.");
mychoice = new Scanner(System.in);
choice = mychoice.nextLine();
}
}
System.out.println("Thank you for using the program!");
}
}

Вам также может понравиться