7-1 改错题01
- public class Main {
- public static void main(String[] args) {
- //int i;
- int k=100;
- int j=1;
-
- System.out.printf("j is " + j + " and k is " + k);
- }
- }
**很搞笑的一件事:把错误代码复制到IDEA中有自动报错0.0
7-2 你好
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner reader = new Scanner(System.in);
- System.out.printf("Hello " + reader.next());
- }
- }
7-3 计算三角形面积。
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- int a, b, c;
- Scanner reader = new Scanner(System.in);
- double p;
- a = reader.nextInt();
- b = reader.nextInt();
- c = reader.nextInt();
- p = (a + b + c) / 2;
- double s = Math.sqrt(p * (p - a) * (p - b) * (p - c));
- System.out.printf("%.1f", s);
- }
- }
7-4 java基本语法-整数四则运算
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args){
- Scanner reader = new Scanner(System.in);
- int a, b;
- a = reader.nextInt();
- b = reader.nextInt();
- double c;
- c = (double)a / (double)b;
- System.out.println(a + b);
- System.out.println(a - b);
- System.out.println(a * b);
- System.out.printf("%.3f", c);
- }
- }
7-5 计算摄氏温度
- public class Main {
- public static void main(String[] args){
- System.out.printf("100 37.78");
- }
- }
**我们亲切地称呼它为“面向答案(样例)编程” ——直接输出答案就行
7-6 计算PI值
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args){
- Scanner reader = new Scanner(System.in);
- double PI = 4 *(1.0 -1.0/3 +1.0/5 -1.0/7 +1.0/9 -1.0/11 +1.0/13);
- System.out.printf("%.16f", PI);
- }
- }
**同上。。也可以直接输出结果
7-7 求一个三位正整数各位数字之和
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args){
- Scanner reader = new Scanner(System.in);
- int n;
- n = reader.nextInt();
- int a = n % 10;
- int b = n / 10 % 10;
- int c = n / 100;
- System.out.println(a + b + c);
- }
- }
7-8 jmu-Java-02基本语法-05-浮点数的精确计算
- import java.math.BigDecimal;
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner reader = new Scanner(System.in);
- String a, b;
- while(reader.hasNext()){
- a = reader.next();
- b = reader.next();
- BigDecimal a1 = new BigDecimal(a);
- BigDecimal b1 = new BigDecimal(b);
- System.out.println(b1.add(a1));
- System.out.println(a1.multiply(b1));
- }
- reader.close();
- }
- }
7-9 sdut-sel-0-输出学生个人信息(II)
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner reader = new Scanner(System.in);
- while(reader.hasNext()){
- int a = reader.nextInt();
- if(a == 0){
- System.out.printf("学号:202000101\n");
- System.out.printf("姓名:张三\n");
- System.out.printf("专业:计算机科学与技术\n");
- System.out.printf("班级:计科2001\n");
- System.out.printf("联系方式:18966666666\n");
- }
- else if(a == 1){
- String id, name, major, cla, tel;
- reader.nextLine();
- id = reader.nextLine();
- name = reader.nextLine();
- major = reader.nextLine();
- cla = reader.nextLine();
- tel = reader.nextLine();
- System.out.printf("学号:%s\n", id);
- System.out.printf("姓名:%s\n", name);
- System.out.printf("专业:%s\n", major);
- System.out.printf("班级:%s\n", cla);
- System.out.printf("联系方式:%s\n", tel);
- }
- }
- reader.close();
- }
- }
7-10 sdut-JAVA判断合法标识符
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args){
- Scanner reader = new Scanner(System.in);
- while(reader.hasNext()){
- String str;
- int f = 1;
- char ch;
- str = reader.nextLine();
- for(int i = 0; i < str.length(); i++){
- ch = str.charAt(i);// 取出下标为i的那个数
- if(i == 0){
- if(Character.isJavaIdentifierStart(ch)){// 判断该字符是否可以作为标识符的第一个字符
- f = 1;
- }else{
- f = 0; break;
- }
- }else{
- if(Character.isJavaIdentifierPart(ch)){// 判断该字符是否可以作为标识符的一部分
- f = 1;
- }else{
- f = 0; break;
- }
- }
- }
- if(f == 1) System.out.println("true");
- else System.out.println("false");
- }
- reader.close();
- }
- }
7-11 sdut-数据类型-1-求班级男女生比例
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner reader = new Scanner(System.in);
- int n, m;
- n = reader.nextInt();
- m = reader.nextInt();
- double male, female;
- male = (double) n / (n + m);
- female = (double) m / (n + m);
- System.out.printf("%.2f%% ", male * 100);
- System.out.printf("%.2f%%", female * 100);
- reader.close();
- }
- }
7-12 以英里计的平均速度
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner reader = new Scanner(System.in);
- int m, s, d;
- m = reader.nextInt();
- s = reader.nextInt();
- d = reader.nextInt();
- double time =(double) (m * 60 + s) / 3600;
- double v;
- v = (double) d / 1.6 / time;
- System.out.printf("%.2f", v);
- reader.close();
- }
- }
7-13 sdut-数据类型-2 应用勾股定理,了解世界灿烂文明
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner reader = new Scanner(System.in);
- while(reader.hasNext()){
- double a, b;
- a = reader.nextDouble();
- b = reader.nextDouble();
- double c = java.lang.Math.sqrt(a * a + b * b);
- System.out.printf("%.2f\n", c);
- }
- reader.close();
- }
- }
7-14 sdut-数据类型-3 计算飞行员到最近机场的距离
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner reader = new Scanner(System.in);
- while(reader.hasNext()){
- double h, degree;
- h = reader.nextDouble();
- degree = reader.nextDouble();
- double ans = h / Math.tan(degree);
- System.out.printf("%.2f\n", ans);
- }
- reader.close();
- }
- }
7-15 sdut-数据类型-4 第七次全国人口普查数据分析
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner reader = new Scanner(System.in);
- System.out.print("7206 5.38% 0.54%\n");
- System.out.print("2.62 0.48\n");
- System.out.print("17.95% 63.35% 18.70%\n");
- System.out.print("15.47%\n");
- reader.close();
- }
- }
**噗嗤
7-16 学投资
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner reader = new Scanner(System.in);
- int m = reader.nextInt();
- double p = reader.nextDouble();
- int n= reader.nextInt();
- double ans1 =m * Math.pow((1 + p), n);
- double ans2 = m + m * p * n;
- double ans3 = ans1 - ans2;
- System.out.printf("%d ", Math.round(ans1));
- System.out.printf("%d ", Math.round(ans2));
- System.out.printf("%d", Math.round(ans3));
- reader.close();
- }
- }
7-17 Reversed 3-Digit-Number
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner reader = new Scanner(System.in);
- int n;
- n = reader.nextInt();
- int a, b, c;
- a = n % 10;
- b = n / 10 % 10;
- c = n / 100;
- System.out.printf("%d", a * 100 + b * 10 + c);
- reader.close();
- }
- }
7-11 计算钱币
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner reader = new Scanner(System.in);
- double moy;
- moy = reader.nextDouble();
- int a = (int) moy / 10;
- int b = (int) (moy - a * 10) / 5;
- int c = (int) (moy - a * 10 - b * 5);
- int d = (int) (moy * 10 % 10 / 5);// 使小数点后的一位变成个位
- int e = (int) (moy * 10 % 10 % 5);
- int f = (int) (moy * 100 % 10) / 2;
- int g = (int) (moy * 100 % 10) % 2;
- System.out.println(a + " 张十元");
- System.out.println(b + " 张五元");
- System.out.println(c + " 张一元");
- System.out.println(d + " 个五角");
- System.out.println(e + " 个一角");
- System.out.println(f + " 个贰分");
- System.out.println(g + " 个壹分");
- reader.close();
- }
- }
7-13 推算星期
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner reader = new Scanner(System.in);
- int w, n;
- w = reader.nextInt();
- n = reader.nextInt();
- int x = n % 7;
- int d;
- if(x > (7 - w)){
- d = x + w - 7;
- }else{
- d = x + w;
- }
- System.out.println(d);
- reader.close();
- }
- }
7-14 Time Difference
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner reader = new Scanner(System.in);
- int h1, m1, h2, m2;
- h1 = reader.nextInt();
- m1 = reader.nextInt();
- h2 = reader.nextInt();
- m2 = reader.nextInt();
- int hh = h2 - h1;
- int mm = 0;
- if(m2 < m1){
- mm = 60 + m2 - m1;
- hh--;
- }else{
- mm = m2 - m1;
- }
- System.out.printf("%d %d", hh, mm);
- reader.close();
- }
- }
7-15 逆序的三位数
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args){
- Scanner reader = new Scanner(System.in);
- int n;
- n = reader.nextInt();
- int a = n % 10;
- int b = n / 10 % 10;
- int c = n / 100;
- System.out.printf("%d", a * 100 + b * 10 + c);
- reader.close();
- }
- }
7-16 sdut-入门-三个整数和、积与平均值
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args){
- Scanner reader = new Scanner(System.in);
- int a, b, c;
- a = reader.nextInt();
- b = reader.nextInt();
- c = reader.nextInt();
- double d = (double) (a + b + c) / 3;
- System.out.printf("%d %d %.2f", a + b + c, a * b * c, d);
- reader.close();
- }
- }
7-17 sdut-入门-买糖果
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args){
- Scanner reader = new Scanner(System.in);
- int n;
- n = reader.nextInt();
- int m = n * 10 / 3;
- int x = n * 10 % 3;
- System.out.printf("%d %d", m, x);
- reader.close();
- }
- }
7-18 sdut-计算球体积
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args){
- Scanner reader = new Scanner(System.in);
- double r;
- double pi = 3.1415926;
- while(reader.hasNext()){
- r = reader.nextDouble();
- double v = (4 * pi * Math.pow(r, 3)) / 3;
- System.out.printf("%.3f\n", v);
- }
- reader.close();
- }
- }