import java.util.Scanner;
public class Text3 {
public static void main(String[] args) {
System.out.println("请输入一个0~1000的数字:");
Scanner s = new Scanner(System.in);
int i = s.nextInt();
if (i > 0 && i < 1000) {
//取百位
int x=i/100;
//取十位
int y=i/100;
//取个位
int z=i;
System.out.println("个位数相加和为:" x y z);
}else{
System.out.println("对不起,您输入的数字有误!");
}
}
}
4.复利值
代码语言:javascript复制
import java.util.Scanner;
public class Text4 {
public static void main(String[] args) {
System.out.println("请输入存入的金额以及要查询余额月份:");
Scanner s=new Scanner(System.in);
int dollar=s.nextInt();
int month=s.nextInt();
double cost=0.0;
for(int i=1;i<=month;i ){
cost=(dollar cost)*(1 0.00417);
System.out.println("第" i "个月后帐户的金额为:" cost);
}
}
}
5.实现二进制转化十进制
代码语言:javascript复制
import java.util.Scanner;
public class Text5 {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("输入一个二进制数 ");
int binaryNumber = s.nextInt();
int decimal = 0;
int p = 0;
while (true) {
if (binaryNumber == 0) {
break;
} else {
int temp = binaryNumber % 10;
decimal = temp * Math.pow(2, p);
binaryNumber = binaryNumber / 10;
p ;
}
}
System.out.println("十进制数为:" decimal);
}
}
6.实现十进制转化二进制
代码语言:javascript复制
import java.util.Scanner;
public class Text6 {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("输入一个十进制数 ");
int n = s.nextInt();
String str = "";
while (n != 0) {
str = n % 2 str;
n = n / 2;
}
System.out.println(str);
}
}