实验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] 实验5參考代码的更多相关文章

  1. [Java] 实验6參考代码

    1. 大家的.java程序都须要在一个"缺省包"(default package)下编写\执行\提交,不要去命名新的package     - 系统不支持package contr ...

  2. [Java] 实验4參考代码

    题目.提示.代码.解释都已公布. 提供这些的目的不是要求大家要写得像我写得这样,而是希望大家在实验后看看别人写的代码:     1. 提升理解代码的能力.     2. 不要自满于完毕题目.要明确你的 ...

  3. Java第二次作业參考代码

    Java第二次作业參考代码 [程序11] 题目:有1.2.3.4四个数字,能组成多少个互不同样且无反复数字的三位数?都是多少? public class lianxi11 { public stati ...

  4. HDU 1042 N! 參考代码

    HDU 1042 N! 题意:给定整数N(0 ≤ N ≤ 10000), 求 N! (题目链接) #include <iostream> using namespace std; //每一 ...

  5. HDU 2136 Largest prime factor 參考代码

    #include <iostream> #include <vector> #include <cmath> using namespace std; const ...

  6. [Java] 实验8

    [Java] 实验7參考代码,代码已更新.感兴趣的同学能够去学习. 1. default package问题可參考实验6 2. for, if, while等.后面包括多条语句时,须要用花括号括起来 ...

  7. ANTLR4权威參考手冊(一)

    写在前面的话: 此文档是对伟大的Terence Parr的著作<the definitive antlr4 reference>的翻译本.致敬!欢迎转载,请注明原地址,请尊重劳动成果.翻译 ...

  8. 6. GC 调优(工具篇) - GC參考手冊

    进行GC性能调优时, 须要明白了解, 当前的GC行为对系统和用户有多大的影响. 有多种监控GC的工具和方法, 本章将逐一介绍经常使用的工具. 您应该已经阅读了前面的章节: 垃圾收集简单介绍 - GC參 ...

  9. Java实验五

    20145113 Java实验五 网络编程及安全 实验内容 对于客户端与服务器端:修改原代码,使其可以实现连续的传消息,并且传送文件. 对于加解密部分: 对于原先的加密只加密"hello w ...

随机推荐

  1. 深入理解java虚拟机---垃圾收集器和分配策略-1

    博文重点: 学习目标:哪些内存需要回收 什么时候回收    如何回收 在基于概念讨论的模型中,主要对Java堆和方法区进行讨论. why?:一个接口中的多个实现类需要的内存可能不一样,一个方法中的多个 ...

  2. PowerDesigner 操作手册

    1.错误信息:Generation aborted due to errors detected during the verification of the model 解决方案: 把检查模型的选项 ...

  3. windows sdk编程禁止改变窗体大小

    #include <windows.h> /*消息处理函数声明*/ HRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM ...

  4. CAD实现批量打印(网页版)

    主要用到函数说明: IMxDrawPrint::BatchPrintDialog 批量打印对话框,详细说明如下: 参数 说明 [in] IMxDrawResbuf* pParam 批量打印位置参数, ...

  5. MySQL主从配置详解

    一.mysql主从原理 1. 基本介绍 MySQL 内建的复制功能是构建大型,高性能应用程序的基础.将 MySQL 的 数亿分布到到多个系统上去,这种分步的机制,是通过将 MySQL 的某一台主机的数 ...

  6. Volume 1. Sorting/Searching(uva)

    340 - Master-Mind Hints /*读了老半天才把题读懂,读懂了题输出格式没注意,结果re了两次. 题意:先给一串数字S,然后每次给出对应相同数目的的一串数字Si,然后优先统计Si和S ...

  7. NOI模拟(3.6)Assignment

    Description 随机生成一个长度为m且每个元素都为1~n之间的整数的单调不下降序列~(即序列的(i>1)都不小于),(随机生成指每一种可能的序列都等概率被生成).请问这个序列的众数出现次 ...

  8. NOI模拟(3.3)螺旋序列(出题人一定是月厨)

    Description S也想寻求真正的智慧,然而由于“抑制力”的存在,她必须先解决一系列询问.有一个长度为n的序列a,一个长度为m序列b被称为螺旋序列当且仅当b1=bm且对于1<=i<= ...

  9. JavaEE JDBC ResultSet内外移动

    ResultSet内外移动 @author ixenos 内外移动指位置光标的移动 内移动就是一个ResultSet得到后的那个光标! 外移动就是多个ResultSet的迭代 内移动 一般的数据库都不 ...

  10. 解决在使用Amoeba遇到的问题

    最近有同行在使用Amoeba 的过程中多少遇到了一些问题. 总结一下遇到问题的解决方法: 1.读写分离的时候设置的在queryRouter中设置无效? 读写分离配置的优先级别:        1)满足 ...