实验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
}
}
}

最新文章

  1. codeforces 597C C. Subsequences(dp+树状数组)
  2. AC日记——将字符串中的小写字母换成大写字母 openjudge 1.7 13
  3. imx6 kernel clock
  4. hibernate有关联关系删除子表时可能会报错,可以用个clear避免错误
  5. Android_用户界面概述和数据单位
  6. Lipschitz连续【zz】
  7. [11-1] adaboost DTree
  8. Asp.Net 禁用cookie后使用session
  9. linux时间方面的设置
  10. css(四)-- 盒子模型和定位
  11. hiboCoder 1041 国庆出游 dfs+思维
  12. obj-c编程15[Cocoa实例01]:一个会发声的随机数生成器
  13. kubernetes的安装方法
  14. 3D Graph Neural Networks for RGBD Semantic Segmentation
  15. JAVA设计模式——代理(静态代理)
  16. ping 丢包或不通时链路测试说明【转】
  17. WebForm内置对象:Session、Cookie,登录和状态保持
  18. 进程间通信 IPC(Inter-Process Communication)
  19. duilib 修复CTreeViewUI复选功能判断不准确的bug
  20. centos6.5挂载windows共享的文件夹

热门文章

  1. 前端phtooshop基础
  2. How to Configure YUM to Install Packages From Installation ISO (RHEL)
  3. 迅为iTOP-4418开发板串口虚拟控制台配置为普通串口
  4. 深入理解python对象及属性
  5. Java软件开发不同薪资级别-技术要求
  6. CREATE SEQUENCE - 创建一个新的序列发生器
  7. springboot实现web应用过程中的摸爬打滚(持续更新ing)
  8. 2019ICPC西安邀请赛(计蒜客复现赛)总结
  9. CSU 2018年12月月赛 H(2220): Godsend
  10. 使用sphinx