【Java语言程序设计(基础篇)】第三章编程练习题

时间:2023-02-22 21:53:34

3.1

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4 
 5     public static void main(String[] args) {
 6         Scanner input = new Scanner (System.in);
 7         
 8         System.out.print("Enter a, b, c: ");
 9         double a = input.nextDouble();
10         double b = input.nextDouble();
11         double c = input.nextDouble();
12         
13         double pbs = b * b - 4 * a * c;
14         
15         if (pbs < 0)
16             System.out.println("The equation has no real roots");
17         else if (pbs == 0)
18             System.out.println("The equation has one root " + -b/(2*a));
19         else
20             System.out.println("The equation has two roots " + (-b+Math.pow(pbs, 0.5))/(2*a)
21                     + " and " + (-b-Math.pow(pbs, 0.5))/(2*a));
22     }
23 }

3.3

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4 
 5     public static void main(String[] args) {
 6         Scanner input = new Scanner (System.in);
 7         
 8         System.out.print("Enter a, b, c, d, e, f: ");
 9         double a = input.nextDouble();
10         double b = input.nextDouble();
11         double c = input.nextDouble();
12         double d = input.nextDouble();
13         double e = input.nextDouble();
14         double f = input.nextDouble();
15         
16         double fm = a * d - b * c;
17         if(fm == 0)
18             System.out.println("The equation has no solution");
19         else {
20             double x = (e*d - b*f) / fm;
21             double y = (a*f - e*c) / fm;
22             System.out.println("x is " + x + " and y is " + y);
23         }
24     }
25 }

3.4

 1 package hello;
 2 
 3 public class Main {
 4 
 5     public static void main(String[] args) {
 6         int m = (int)(Math.random() * 12 + 1);
 7         String yue = "0";
 8         switch(m) {
 9             case 1: yue = "January";    break;
10             case 2: yue = "February";    break;
11             case 3: yue = "March";    break;
12             case 4: yue = "April";    break;
13             case 5: yue = "May";    break;
14             case 6: yue = "June";    break;
15             case 7: yue = "July";    break;
16             case 8: yue = "Auguse";    break;
17             case 9: yue = "September";    break;
18             case 10: yue = "October";    break;
19             case 11: yue = "November";    break;
20             case 12: yue = "December";    break;
21         }
22         
23         System.out.println(yue);
24     }
25 }

才开始忘记了break;造成每次都是输出December。