版权声明:本文为Trinity原创文章,未经Trinity允许不得转载 https://blog.csdn.net/Caoyang_He/article/details/82662318
编写一个简单Java程序,计算银行年存款的本息。
/**
* Author: Thrinity
* Date: 2018-9-12
* Version: 1.0
* Description: test_1.1
(1)编写一个简单Java程序,计算银行年存款的本息。
要求:程序运行后要求用户输入存款本金、年利率和存款年限,最后程序计算并输出相应年限后存款的金额。相应的计算公式为:存款总额=本金 * (1 + 利率)存款年限。
提示:请检测用户输入的合法性。可以使用Math类来进行次方的运算:Math.pow(底数,指数)。
*/
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);
}
}
编写程序完成矩阵的乘法运算。
/**
* Author: Thrinity
* Date: 2018-9-12
* Version: 1.0
* Description: test_1.2
(2)编写程序完成矩阵的乘法运算。
要求:程序运行后要求用户分别输入两个矩阵的行数和列数,然后判断两个矩阵是否可以相乘,如果可以相乘,则计算矩阵相乘后的结果,并按矩阵的格式输出;若不能相乘,则提示用户矩阵不相容,并退出程序。
*/
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++)
//第三个矩阵的第i行第j列所对应的数值,等于第一个矩阵的第i行分别乘以第二个矩阵的第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();
}
//输出相乘的结果
}
}
编写程序用*号输出等腰梯形。
/**
* Author: Thrinity
* Date: 2018-9-12
* Version: 1.0
* Description: test_1.3
(3)编写程序用*号输出等腰梯形。
要求:程序运行后要求用户输入梯形的上边长度(*号的数量),然后输入梯形的层数。梯形的每一层比上一层左右边各多一个*号,程序输出梯形的形状。
例如:当用户输入3和4,则程序应该输出:
***
*****
*******
********* */
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();
}
}
}