编程程序,请看详细描述
1. 假如我国国民生产总值的年增长率为r,计算n年后我国国名生产总值与现在相比增长多少百分比p。计算公式为
提示:调用乘方函数的方法 Math.pow(x,y),前提是导入Math类。
2. 将字符串“China”加密。密码的生成规律是:用原来的字母后的第3个字母代替原来的字母。分别输出加密前和加密后的字符串。
提示:从键盘上读取一个字符的方法有两种
(1)char i = (char)System.in.read(); //每次读取一个字符
(2) Scanner sc = new Scanner(System.in); //创建输入对象sc
String str = sc.nextLine(); //读取字符串
char ch = str.charAt(k); //获取字符串中第k(从0开始)个字符
具体该怎编写代码,谢了。
二个都是入门级的,参考:
1。
public static void main(String[]args) {
int n;
double p, r;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
r = sc.nextFloat();
p = Math.pow((r + 1), n);
System.out.printf("%.2f
", p); //%。2f保留两位小数
}
2。
public static void main(String[]args) {
Scanner sc = new Scanner(System.in); //创建输入对象sc
String str = sc.nextLine(); //读取字符串
char ch =0;
int i;
System.out.printf("加密前:%s
",str);
System.out.printf("加密后:");
for(i=0;i<str.length();i++)
{
ch = str.charAt(i); //获取字符串中第k(从0开始)个字符
if (ch>='x') //大于x就不能加3了
ch=(char)(ch-'z'+'a');
ch=(char)(ch+3);
System.out.printf("%c",ch);
}
System.out.println();
}
上一篇:请教一下我这配置能打吃鸡吗?