[Java] 实验5参考代码

本文提供了一系列Java编程实验的代码模板及指导,包括显示成绩等级、寻找最小值、计算三角形面积与周长、判断数值符号等任务。每项任务都配有详细的步骤说明与代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实验4月3日晚截止,实验截止后将在此给出完整的参考代码。


1. 如何使用下面的代码模板:

    1.1 在eclipse中创建对应名称的

    1.2 将代码复制到类文件里

    1.3 在//todo注释中输入你用于解题的代码。

    1.4 例子:参考第一题“显示两级名字”,大家就可以这么做

        1.4.1 在eclipse中创建类,名字叫做PassOrFail

        1.4.2 将下面的代码复制到.java文件中,并删除//todo注释,开始在while循环里写代码 (判断成绩是否大于60, 输出等)

        1.4.3 将写好的PassOrFail.java上交到实验网站上

2. 不了解什么是缩进的可以参考什么是代码缩进(code indent), 或与周围同学讨论。

3. 自动排版快捷键:ctrl + shift + F (很方便的,一定要试试。谁用谁知道:P)


显示两级名字

这题目的名字真烂... 代码:

import java.util.Scanner;

public class PassOrFail {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int repeat = in.nextInt();
		while (repeat-- != 0) {
			// todo
		}
	}
}

找最小值

提示

1. 两个数的最小值

int minimal = Math.min(a, b);

import java.util.Scanner;

public class FindMinimal {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int repeat = in.nextInt();
		while (repeat-- != 0) {
			// todo
			System.out.println("min is " + min);
		}
	}
}

求三角形的面积和周长

提示

1. if后面跟多条语句的时候,需要用花括号,将“多条语句”括起来,使它们变成一个“复合语句”.

2. 条件A, B的“逻辑与”关系

if (conditionA && conditionB) {
    // todo
} else {
    // todo
}
3. 条件A, B的“逻辑或”关系

if (conditionA || conditionB) {
    // todo
} else {
    // todo
}

4. 求平方根

sqrt方法大家之前用过,可以去看[Java] 实验3参考代码.

代码模板

import java.util.Scanner;

public class Triangle {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int repeat = in.nextInt();
		while (repeat-- != 0) {
			float a = in.nextFloat();
			float b = in.nextFloat();
			float c = in.nextFloat();
			// todo
		}
	}
}


判断数的符号

import java.util.Scanner;

public class JudgeSign {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int repeat = in.nextInt();
		while (repeat-- != 0) {
			int num = in.nextInt();
			// todo
		}
	}
}

计算个人所得税

提示

1. 什么是初始化

大家可以去群里下载《Java核心技术 卷1 基础知识 (原书第9版)》,参考"3.4.1 变量初始化"部分。

2. 为什么在这道题中,有些同学会出现编译错误:“可能使用未初始化的变量”

根据语法,变量在读取前必须被赋值(Variables must be initialized before used.).

考虑下述代码:

double rate;
if (conditionA) {
    rete = 0.05;
} else if (condition B) {
    rate = 0.1;
} else if (condition C) {
    rate = 0.2;
}
double res = rate; // compilation error!!!
代码最后一行做的事,是 读取rate的值,将这个值赋值给res. 

根据语法,这个rate的值在被读取前需要被初始化(非正式地可以理解成“赋值”)。问题是,从编译器的角度上看,如果分支A, B, C都没有被执行,那么rate可能就没有被赋值,所以在double res = rate中,试图读取rate的值就是非法的。

有些同学会说,为解决这个问题,可以在定义rate的时候先随便给它赋一个值:double rate = 0.

这样可以阻止编译器报错,但未必是并非最优的解决方案。如果A, B, C三个分支能包含整个条件空间,那么我认为代码应该写成这样更为合理:

double rate;
if (conditionA) {
    rete = 0.05;
} else if (condition B) {
    rate = 0.1;
} else { // there is no if here!!!
    rate = 0.2;
}
double res = rate;

程序模板

import java.util.Scanner;

public class Tax {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int repeat = in.nextInt();
		while (repeat-- != 0) {
			float salary = in.nextFloat();
			// todo
			System.out.println("tax=" + (int)(tax * 100 + 0.5) / 100d);
		}
	}
}

显示水果的价格

import java.util.Scanner;

public class Fruits {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int repeat = in.nextInt();
		while (repeat-- != 0) {
			System.out.println("[1] apples");
			System.out.println("[2] pears");
			System.out.println("[3] oranges");
			System.out.println("[4] grapes");
			int choice = in.nextInt();
			// todo
		}
	}
}


字母转换

import java.io.*;

public class CharacterConversion {
	public static void main(String[] args) throws IOException {
		for (int ch = System.in.read(); ch != '?'; ch = System.in.read()) {
			// todo
			System.out.print((char)ch);
		}
	}
}


计算分段函数的值

import java.util.Scanner;

public class PiecewiseFunction {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int repeat = in.nextInt();
		while (repeat-- != 0) {
			int x = in.nextInt();
			double y;
			// todo
			System.out.println("f(" + x + ")=" + y);
		}
	}
}

求一元二次方程的根

提示

1. 为什么 (int) (x * 100 + 0.5) / 100d的方法有些时候会出错?

因为这个方法只能应付x >= 0的情况,考虑 x = -2.5, 那么

x * 100 = -250

-250 + 0.5 = -249.5

(int) -249.5 = -249

-249 / 100d = -2.49

注意到,在此我们想要的结果是-2.5而不是-2.49

2. 四舍五入保留两位小数

public class Test {
  public static void main(String[] args) {
    double num = 3.1415926535;
    for (int i = 0; i <= 10; ++ i) {
      System.out.println(remainDigits(num, i));
    }
  }

  static double remainDigits(double num, int digitsToRemain) {
    // e.g. remainDigits(3.14159, 2) was called, then it will return 
    // Math.round(3.14159 * 100d) / 100d, which equals 3.14
    return Math.round(num * Math.pow(10, digitsToRemain)) 
        / Math.pow(10, digitsToRemain);
  }
}

import java.util.Scanner;

public class Root {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int repeat = in.nextInt();
		while (repeat-- != 0) {
			int a = in.nextInt();
			int b = in.nextInt();
			int c = in.nextInt();
			// todo
		}
	}
}

显示五级记分制成绩所对应的百分制成绩区间

import java.util.Scanner;

public class Grade {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int repeat = in.nextInt();
		while (repeat-- != 0) {
			// todo
		}
	}
}



Java程序设计》课程实验指导书程序代码(答案)(实验Java的异常处理),个人原创,仅供参考与交流。 希望多多交流,共同进步! 实验 Java的异常处理 一、实验目的: 理解 java 语言中独特的异常处理机制; 掌握异常处理方法; 正确地使用捕获异常和声明抛弃异常的两种异常处理的方法; 了解自定义异常类的使用; 理解抛出异常和声明抛出异常的区别与联系; 二、实验内容: 1. 从命令行得到5个整数,放入一整型数组,然后打印输出,要求:如果输入数据不为整数,要捕获Integer.parseInt()产生的异常,显示“请输入整数”,捕获输入参数不足5个的异常(数组越界),显示“请输入至少5个整数”。 2.写一个方法void sanjiao(int a,int b,int c),判断三个参数是否能构成一个三角形,如果不能则抛出异常IllegalArgumentException,显示异常信息a,b,c+”不能构成三角形”,如果可以构成则显示三角形三个边长,在主方法中得到命令行输入的三个整数,调用此方法,并捕获异常。 3.自定义类Sanj,其中有成员 x,y,z,作为三边长,构造方法Sanj(a,b,c)分别给x,y,z赋值,方法求面积getArea和显示三角形信息(三个边长)showInfo,这2个方法中当三条边不能构成一个三角形时要抛出自定义异常NotSanjiaoException,否则显示正确信息。在另外一个类中的主方法中构造一个Sanj对象(三边为命令行输入的三个整数),显示三角形信息和面积,要求捕获异常。 三、实验要求: 1. 通过实验掌握捕获异常和声明抛弃异常的两种异常处理的方法; 2. 程序必须能够捕获Integer.parseInt()产生的异常并作相应的处理; 3. 程序必须能够将处理的相应的信息输出出来; 4. 写出实验报告。要求记录编译和执行Java程序当中的系统错误信息提材示,并给出解决办法。(附运行界面、源代码)。 四、实验步骤: 1. (第1题) 使用try…..catch…. finally语句对Integer.parseInt()产生的异常进行捕获处理; 2. (第2题) 定义方法void sanjiao(int a,int b,int c)中,不符合条件则抛出异常(if a+b<=c(或a+c<=b,b+c<=a) then throw new IllegalArgumentException(),),再定义main方法,调用此方法,用try…..catch…. finally语句并捕获异常; 3. (第3题) 定义三角形类及相应的方法, 在定义的方法中对不符合条件则抛出异常(如上题) ,再定义main方法,对正确的数据输出正确信息,否则捕获异常; 、自做实验 1.参考下面的程序,试修改程序,捕获相关异常,使得程序能正常运行。[提示:用错误数据测试,即可得到异常类名,运行时主方法参数输入 abc 测试] public class StringIndexOutOf{ public static void main(String args[]){ System.out.println("字符串索引越界异常"); String str=args[0]; System.out.println(“第四个字符为 ”+str.charAt(3)); int aa=Integer.parseInt(args[0]); System.out.println(“平方为 ”+aa*aa); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值