120k views
1 vote
write a java program that convert a positive number into a negative number and negative number into a positive number using switch statements

1 Answer

7 votes

import java.util.Scanner;

class convertint {

public static void main(String[] args) {

Scanner f = new Scanner(System.in);

System.out.println("Enter a number to convert: ");

int s = f.nextInt();

int check = (s>0) ? 1 : (s==0) ? 0 : -1;

switch(check) {

case 1:

System.out.println(s*-1);

break;

case 0:

System.out.println("0");

break;

case -1:

System.out.println(s*-1);

break;

default:

;;

}

}

}

User Tomas Camin
by
4.7k points