168,751 views
37 votes
37 votes
write a java program that convert a positive number into a negative number and negative number into a positive number using switch statements

User Suresh Chaudhary
by
3.0k points

1 Answer

20 votes
20 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 Chris Allinson
by
2.9k points