JAVA语言程序设计(基础篇) 第十版——第八章 多维数组 (参考答案)

 只写了几题(待以后更新.......)

*8.1(求矩阵中各列数字的和)

import java.util.Scanner;

public class E1 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.println("Enter a 3-by-4 matrix row by row: ");
		double[][] m=new double[3][4];
		
		for(int row=0; row

 *8.2(求矩阵主对角线元素的和)

import java.util.Scanner;

public class E2 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.println("Enter a 4-by-4 matrix row by row: ");
		double[][] m=new double[4][4];
		
		for(int row=0; row

 *8.3(按考分对学生排序)

略,目前不会

**8.4(计算每个雇员每周工作的时间)

略,看见排序的我要缓一缓

8.5(代数方面:两个矩阵相加)

JAVA语言程序设计(基础篇) 第十版——第八章 多维数组 (参考答案)_第1张图片

import java.util.Scanner;

public class E5 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter matrix1: ");
		double[][] m1=new double[3][3];
		for(int row=0; row

 

*10.8(最大的行和列)

JAVA语言程序设计(基础篇) 第十版——第八章 多维数组 (参考答案)_第2张图片

package p8;

import java.util.ArrayList;
import java.util.Scanner;

public class Test10 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter the array size n: ");
		int n=input.nextInt();
		
		input.close();
		
		System.out.println("The random array is : ");
		
		
		//创建一个二维数组
		int[][] array=new int[n][n];
		
		//下标[row]:行
		//下标[column]:列
		
		//按行,循环赋值,并且输出
		for(int row=0; row

**8.12(财务应用程序:计算税率)

package p8;

import java.util.Scanner;

//使用数组重写程序清单3-5
public class E12 {

	public static void main(String[] args) {
		     //六种税率: 10%,  15%,   25%,    28%,   33%,  35%。
		double[] rates= {0.10, 0.15, 0.25, 0.28, 0.33, 0.35};
		
		int[][] backets= {
				{8350, 33950, 82250, 171550, 372950},//Single
				{16700, 67900, 137050, 208850, 372950},//Married Joint
				{8350, 33950, 68525, 104425, 186475},//Married Separate
				{11950, 45500, 117450, 190200, 372950}//Head of a House
		};
//-----------------------------------------------------------------------------		
		//single
	/*	double tax0=backets[0][0] * rates[0]+
				(backets[0][1] - backets[0][0]) * rates[1] +
				(backets[0][2] - backets[0][1]) * rates[2] +
				(backets[0][3] - backets[0][2]) * rates[3] +
				(backets[0][4] - backets[0][3]) * rates[4] +
				(400000 - backets[0][4]) * rates[5] ;   */
		
		//Married Joint
	/*	double tax1=backets[1][0] * rates[0]+
				(backets[1][1] - backets[1][0]) * rates[1] +
				(backets[1][2] - backets[1][1]) * rates[2] +
				(backets[1][3] - backets[1][2]) * rates[3] +
				(backets[1][4] - backets[1][3]) * rates[4] +
				(income - backets[1][4]) * rates[5] ;   */
		
		Married Separate
	/*	double tax2=backets[2][0] * rates[0]+
				(backets[2][1] - backets[2][0]) * rates[1] +
				(backets[2][2] - backets[2][1]) * rates[2] +
				(backets[2][3] - backets[2][2]) * rates[3] +
				(backets[2][4] - backets[2][3]) * rates[4] +
				(income - backets[2][4]) * rates[5] ;   */
		
		
		//Head of a House
	/*	double tax3=backets[3][0] * rates[0]+
				(backets[3][1] - backets[3][0]) * rates[1] +
				(backets[3][2] - backets[3][1]) * rates[2] +
				(backets[3][3] - backets[3][2]) * rates[3] +
				(backets[3][4] - backets[3][3]) * rates[4] +
				(income - backets[3][4]) * rates[5] ;  */
		
//-------------------------------------------------------------------------------------		
		Scanner input=new Scanner(System.in);
		System.out.print("(0-single filer, 1-Married Joint or qualifying widow(er), "
				+ "2-Married Separately, 3-Head of a household)\n"
				+ "Enter the filong status:");
		
		int status=input.nextInt();
		
		System.out.print("Enter the taxable intcome: ");
		double taxableIncome=input.nextDouble();
		
		double tax=0;
		
		//status为0、1、2、3针对每一种身份
				if(status==0) {
					if(taxableIncome<=8350)
						tax=backets[0][0] * rates[0];
					else if(taxableIncome<=33950)
						tax=backets[0][0] * rates[0]+
								(backets[0][1] - backets[0][0]) * rates[1];
					else if(taxableIncome<=82250)
						tax=backets[0][0] * rates[0]+
								(backets[0][1] - backets[0][0]) * rates[1] +
								(backets[0][2] - backets[0][1]) * rates[2];
					else if(taxableIncome<=171550)
						tax=backets[0][0] * rates[0]+
								(backets[0][1] - backets[0][0]) * rates[1] +
								(backets[0][2] - backets[0][1]) * rates[2] +
								(backets[0][3] - backets[0][2]) * rates[3]; 
					else if(taxableIncome<=372950)
						tax=backets[1][0] * rates[0]+
								(backets[1][1] - backets[1][0]) * rates[1] +
								(backets[1][2] - backets[1][1]) * rates[2] +
								(backets[1][3] - backets[1][2]) * rates[3] +
								(backets[1][4] - backets[1][3]) * rates[4];
					else
						tax=backets[0][0] * rates[0]+
								(backets[0][1] - backets[0][0]) * rates[1] +
								(backets[0][2] - backets[0][1]) * rates[2] +
								(backets[0][3] - backets[0][2]) * rates[3] +
								(backets[0][4] - backets[0][3]) * rates[4] +
								(taxableIncome - backets[0][4]) * rates[5];   
				}
				else if(status==1) {
					if(taxableIncome<=16700)
						tax=backets[1][0] * rates[0];
					else if(taxableIncome<=67900)
						tax=backets[1][0] * rates[0]+
								(backets[1][1] - backets[1][0]) * rates[1];
					else if(taxableIncome<=137050)
						tax=backets[1][0] * rates[0]+
								(backets[1][1] - backets[1][0]) * rates[1] +
								(backets[1][2] - backets[1][1]) * rates[2];
					else if(taxableIncome<=208850)
						tax=backets[1][0] * rates[0]+
								(backets[1][1] - backets[1][0]) * rates[1] +
								(backets[1][2] - backets[1][1]) * rates[2] +
								(backets[1][3] - backets[1][2]) * rates[3];
					else if(taxableIncome<=372950)
						tax=backets[1][0] * rates[0]+
								(backets[1][1] - backets[1][0]) * rates[1] +
								(backets[1][2] - backets[1][1]) * rates[2] +
								(backets[1][3] - backets[1][2]) * rates[3] +
								(backets[1][4] - backets[1][3]) * rates[4];
					else
						tax=backets[1][0] * rates[0]+
								(backets[1][1] - backets[1][0]) * rates[1] +
								(backets[1][2] - backets[1][1]) * rates[2] +
								(backets[1][3] - backets[1][2]) * rates[3] +
								(backets[1][4] - backets[1][3]) * rates[4] +
								(taxableIncome - backets[1][4]) * rates[5];  
			
				}
				else if(status==2) {
					if(taxableIncome<=8350)
						tax=backets[2][0] * rates[0];
					else if(taxableIncome<=33950)
						tax=backets[2][0] * rates[0]+
								(backets[2][1] - backets[2][0]) * rates[1];
					else if(taxableIncome<=68525)
						tax=backets[2][0] * rates[0]+
								(backets[2][1] - backets[2][0]) * rates[1] +
								(backets[2][2] - backets[2][1]) * rates[2];
					else if(taxableIncome<=104425)
						tax=backets[2][0] * rates[0]+
								(backets[2][1] - backets[2][0]) * rates[1] +
								(backets[2][2] - backets[2][1]) * rates[2] +
								(backets[2][3] - backets[2][2]) * rates[3];
					else if(taxableIncome<=186475)
						tax=backets[2][0] * rates[0]+
								(backets[2][1] - backets[2][0]) * rates[1] +
								(backets[2][2] - backets[2][1]) * rates[2] +
								(backets[2][3] - backets[2][2]) * rates[3] +
								(backets[2][4] - backets[2][3]) * rates[4] ;
					else 
						tax=backets[2][0] * rates[0]+
								(backets[2][1] - backets[2][0]) * rates[1] +
								(backets[2][2] - backets[2][1]) * rates[2] +
								(backets[2][3] - backets[2][2]) * rates[3] +
								(backets[2][4] - backets[2][3]) * rates[4] +
								(taxableIncome - backets[2][4]) * rates[5] ;
						
				}
				else if(status==3) {
					if(taxableIncome<=11950)
						tax=backets[3][0] * rates[0];
					else if(taxableIncome<=45500)
						tax=11950*0.1+(taxableIncome-11950)*0.15;
					else if(taxableIncome<=117450)
						tax=11950*0.1+(45500-11950)*0.15+(taxableIncome-45500)*0.25;
					else if(taxableIncome<=190200)
						tax=11950*0.1+(45500-11950)*0.15+(117450-45500)*0.25+(taxableIncome-117450)*0.28;
					else if(taxableIncome<=372950)
						tax=11950*0.1+(45500-11950)*0.15+(117450-45500)*0.25+(190200-117450)*0.28+(taxableIncome-190200)*0.33;
					else
						tax=backets[3][0] * rates[0]+
								(backets[3][1] - backets[3][0]) * rates[1] +
								(backets[3][2] - backets[3][1]) * rates[2] +
								(backets[3][3] - backets[3][2]) * rates[3] +
								(backets[3][4] - backets[3][3]) * rates[4] +
								(taxableIncome - backets[3][4]) * rates[5];
				}
				else {
					System.out.println("Error: invalid status");
					System.exit(1);
				}
				
				System.out.println("Tax is "+(int)(tax * 100) / 100.0);	
		
			
	}

}

 

 

你可能感兴趣的:(JAVA编程,java,程序设计)