这是代码:
import javax.swing.*;
public class SwitchCase {
/** * @param args */
public static void main(String[] args) {
int dec = 0, num, rem, org, pow, length = 0, i = 1, input;
LOOPA: {
input = Integer
.parseInt(JOptionPane
.showInputDialog("Press 1 for binary to decimal conversion \n "
+ "Press 2 for decimal to binary conversion\n"
+ "Press 3 for octal to decimal conversion\n"
+ "Press 4 for decimal to octal conversion\n"));
switch (input) {
case 1:
num = Integer.parseInt(JOptionPane
.showInputDialog("Enter binary number"));
org = num;
while (num != 0) {
num = num / 10;
length++;
}
pow = length - 1;
System.out.print("decimal Equivalent is: ");
while (pow >= 0) {
rem = org % 10;
dec = dec + (rem * i);
i = i * 2;
pow--;
org = org / 10;
}
System.out.print(dec);
break LOOPA;
// decimal to binary conversion
case 2:
int addTwo = 2,
bin;
num = Integer.parseInt(JOptionPane
.showInputDialog("Enter decimal number"));
while (num != 0) {
rem = num % 2;
num = num / 2;
addTwo = (addTwo * 10) + rem;
}
System.out.print("Binary Equivalent is: ");
while (addTwo != 2) {
bin = addTwo;
bin = bin % 10;
System.out.print(bin);
addTwo = addTwo / 10;
}
break LOOPA;
case 3: // octal to decimal conversion
num = Integer.parseInt(JOptionPane
.showInputDialog("Enter octal number"));
org = num;
while (num != 0) {
num = num / 10;
length++;
}
pow = length - 1;
System.out.print("decimal Equivalent is: ");
while (pow >= 0) {
rem = org % 10;
dec = dec + (rem * i);
i = i * 8;
pow--;
org = org / 10;
}
System.out.print(dec);
break LOOPA; // decimal to octal conversion
case 4:
int addEight = 8,
octal;
num = Integer.parseInt(JOptionPane
.showInputDialog("Enter decimal number"));
while (num != 0) {
rem = num % 8;
num = num / 8;
addEight = (addEight * 10) + rem;
}
System.out.print("Octal Equivalent is: ");
while (addEight != 8) {
octal = addEight;
octal = addEight % 10;
System.out.print(octal);
addEight = addEight / 10;
}
break LOOPA;
default:
System.out.print("do u want to continue");
}
}
}
}