编写一个简单Java程序,计算银行年存款的本息。
package test1;
import java.util.*;
import java.io.*;
public class CalcBankMoney {
public static void main(String[] args) {
int money=0;
double rate=0.0;
int year=0;
double receive=0.0;
Scanner input= new Scanner(System.in);
try {
System.out.println("Please input your money");
money=input.nextInt();
}
catch(Exception e){
System.out.println(e);
}
try {
System.out.println("Please input year ");
year=input.nextInt();
}
catch(Exception e){
System.out.println(e);
}
try {
System.out.println("Please input bank's rate");
rate=input.nextDouble();
}
catch(Exception e){
System.out.println(e);
}
receive=money*Math.pow((1+rate), year);
System.out.println(receive);
}
}
编写程序完成矩阵的乘法运算。
package test1;
import java.util.*;
public class matrix {
public static void main(String[] args) {
int matrix1_r=0;
int matrix1_c=0;
int matrix2_r=0;
int matrix2_c=0;
Scanner input= new Scanner(System.in);
System.out.println("please input the fisrt matrix row ");
matrix1_r=input.nextInt();
System.out.println("please input the fisrt matrix column");
matrix1_c=input.nextInt();
System.out.println("please input the second matrix row ");
matrix2_r=input.nextInt();
System.out.println("please input the second matrix column");
matrix2_c=input.nextInt();
int matrix1[][] = new int[matrix1_r][matrix1_c];
int matrix2[][] = new int[matrix2_r][matrix2_c];
input.nextLine();
for(int i=0;i<matrix1_r;i++)
{
System.out.println("please input the first matrix's "+"the "+i+" column");
String str = input.nextLine();
String[] numstr = str.split(" ");
for(int j = 0;j < numstr.length; j++)
{
matrix1[i][j] = Integer.parseInt(numstr[j]);
}
}
for(int i=0;i<matrix2_r;i++)
{
System.out.println("please input the second matrix's "+"the "+i+" column");
String str = input.nextLine();
String[] numstr = str.split(" ");
for(int j = 0;j < numstr.length; j++)
{
matrix2[i][j] = Integer.parseInt(numstr[j]);
}
}
if (matrix1_c != matrix2_r)
{
System.out.println("Sorry,these matrixs can not multiply");
}
int matrix3[][] = new int[matrix1_r][matrix2_c];
for (int i = 0; i < matrix1_r; i++)
for (int j = 0; j < matrix2_c; j++)
for (int k = 0; k < matrix2_r; k++)
matrix3[i][j] += matrix1[i][k] * matrix2[k][j];
System.out.println("The result is:");
for(int i=0;i<matrix3[0].length;i++)
{
for(int j = 0;j < matrix3.length; j++)
{
System.out.print(matrix3[i][j]+" ");
}
System.out.println();
}
}
}
编写程序用*号输出等腰梯形。
package test1;
import java.util.*;
public class isosceles_trapezoid {
public static void main(String[] args) {
int top=0;
int high=0;
Scanner input=new Scanner(System.in);
System.out.println("please input the top * number");
top=input.nextInt();
System.out.println("please input the high");
high=input.nextInt();
for(int i=0;i<high;i++)
{
for(int a=high-i;a>=0;a--)
{
System.out.print(" ");
}
for(int b=0;b<top+2*i;b++)
{
System.out.print("*");
}
System.out.println();
}
}
}