用java 编写60进制计算器
时间: 2023-10-07 21:12:37 浏览: 166
以下是一个简单的 Java 60 进制计算器示例程序:
```java
import java.util.Scanner;
public class Base60Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入第一个数:");
String num1 = scanner.nextLine();
System.out.print("请输入第二个数:");
String num2 = scanner.nextLine();
// 将输入的数转换为 10 进制数
int dec1 = base60ToDec(num1);
int dec2 = base60ToDec(num2);
// 进行加减乘除运算
int sum = dec1 + dec2;
int difference = dec1 - dec2;
int product = dec1 * dec2;
int quotient = dec1 / dec2;
// 将结果转换为 60 进制数并输出
System.out.println("和:" + decToBase60(sum));
System.out.println("差:" + decToBase60(difference));
System.out.println("积:" + decToBase60(product));
System.out.println("商:" + decToBase60(quotient));
}
/**
* 将 60 进制数转换为 10 进制数
*/
private static int base60ToDec(String base60) {
int dec = 0;
for (int i = 0; i < base60.length(); i++) {
char c = base60.charAt(i);
int digit = 0;
if (c >= '0' && c <= '9') {
digit = c - '0';
} else if (c >= 'A' && c <= 'Z') {
digit = c - 'A' + 10;
} else if (c >= 'a' && c <= 'z') {
digit = c - 'a' + 36;
}
dec = dec * 60 + digit;
}
return dec;
}
/**
* 将 10 进制数转换为 60 进制数
*/
private static String decToBase60(int dec) {
StringBuilder sb = new StringBuilder();
while (dec > 0) {
int remainder = dec % 60;
char c = 0;
if (remainder >= 0 && remainder <= 9) {
c = (char) ('0' + remainder);
} else if (remainder >= 10 && remainder <= 35) {
c = (char) ('A' + remainder - 10);
} else if (remainder >= 36 && remainder <= 59) {
c = (char) ('a' + remainder - 36);
}
sb.insert(0, c);
dec /= 60;
}
return sb.toString();
}
}
```
运行示例:
```
请输入第一个数:3g
请输入第二个数:1C
和:4X
差:2S
积:21sg
商:3
```
阅读全文