correct this one for me there are some mistakes and tell me what you did -------
ID: 3595744 • Letter: C
Question
correct this one for me there are some mistakes and tell me what you did
------------------------------------------------------------------------
mport java.util.Scanner;
import java.lang.*;
public class Odd {
public static void main(String ar[]) {
int option;
int x;
Scanner input = new Scanner(System.in);
boolean loop = true; //boolean value for continue loop
while (loop) {
int oddEven = input.nextInt();
System.out.println("-- MENU --"); //Prints the menu for deposite,withdraw, balance and exit
System.out.println(
" 1) findOddEven " +
" 2) areaCircle " +
" 3) sumOfOddNumbers " +
" 4) Exit "
);
System.out.print("Enter option:");
option = input.nextInt();
switch (option) {
case '1'://case 1 for deposite
System.out.println("Enter an integer number");
Scanner in = new Scanner(System.in);
x = input.nextInt();
if ( x % 2 == 0 )
System.out.println("You entered an even number.");
else
System.out.println("You entered an odd number.");
break;
case '2':
int r;
double pi = 3.14, area;
System.out.print("Enter radius of circle:");
r = input.nextInt();
area = pi * r * r;
System.out.println("Area of circle:"+area);
break;
case '3':
int n = input.nextInt();
int sumOfOddNumbers = 0;
System.out.println("enter a positive number: ");
int total = 0;
for (int i = 0; i < n; i++) {
if (n == 1) {
System.out.println(1);
} else {
System.out.println((i * 2) + 1);
System.out.println("total : " + total);
switch (option) {
case '4':
System.exit(0); //system exit
break;
default:
}
}
}
}
}
}
}
-----------------------------------------------------------------------------
Explanation / Answer
Hi
I have fixed the issues and highlighted the code change sbelow
Odd.java
import java.util.Scanner;
public class Odd {
public static void main(String ar[]) {
int option;
int x;
Scanner input = new Scanner(System.in);
boolean loop = true; // boolean value for continue loop
while (loop) {
System.out.println("-- MENU --"); // Prints the menu for
// deposite,withdraw, balance
// and exit
System.out.println(" 1) findOddEven " + " 2) areaCircle "
+ " 3) sumOfOddNumbers " + " 4) Exit ");
System.out.print("Enter option:");
option = input.nextInt();
switch (option) {
case 1:// case 1 for deposite
System.out.println("Enter an integer number");
Scanner in = new Scanner(System.in);
x = input.nextInt();
if (x % 2 == 0)
System.out.println("You entered an even number.");
else
System.out.println("You entered an odd number.");
break;
case 2:
int r;
double pi = 3.14,
area;
System.out.print("Enter radius of circle:");
r = input.nextInt();
area = pi * r * r;
System.out.println("Area of circle:" + area);
break;
case 3:
int sumOfOddNumbers = 0;
System.out.println("enter a positive number: ");
int n = input.nextInt();
for (int i = 0; i < n; i++) {
if(i % 2 != 0)
sumOfOddNumbers+=i;
}
System.out.println("Sum of odd numbers: "+sumOfOddNumbers);
break;
case 4:
System.exit(0); // system exit
break;
default:
}
}
}
}
Output:
-- MENU --
1) findOddEven
2) areaCircle
3) sumOfOddNumbers
4) Exit
Enter option:1
Enter an integer number
3
You entered an odd number.
-- MENU --
1) findOddEven
2) areaCircle
3) sumOfOddNumbers
4) Exit
Enter option:2
Enter radius of circle:3
Area of circle:28.259999999999998
-- MENU --
1) findOddEven
2) areaCircle
3) sumOfOddNumbers
4) Exit
Enter option:3
enter a positive number:
10
Sum of odd numbers: 25
-- MENU --
1) findOddEven
2) areaCircle
3) sumOfOddNumbers
4) Exit
Enter option:4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.