Chapter Exercises (1)

The third chapter title a bit more, I picked some important writing.

 Let's analyze the problem:

Data : abc delt r1 r2
steps :
1. The user is prompted to input three parameters abc
2. Compute delt = * a c b * b -4 *
value of 3. Analyzing delt
    > 0 3.1 delt
        output two solutions
    3.2 delt == 0
        outputting a solution
    3.3 delt <0
        no real solutions

import java.util.Scanner;
class  Demo03_01{
    public static void main(String[] args){
        //1.
        Scanner scanner=new Scanner(System.in);
        System.out.print("请输入a,b,c:");
        double a=scanner.nextDouble();
        double b=scanner.nextDouble();
        double c=scanner.nextDouble();
        //2.
        double delt=b*b-4*a*c;
        //3.
        if(delt>0){
            double r1=(-b+Math.sqrt(delt))/(2*a);
            double r2=(-b-Math.sqrt(delt))/(2*a);
            System.out.printf("r1=%.2f,r2=%.2f",r1,r2);
        }else if(delt==0){
            double r=(-b-Math.sqrt(delt))/(2*a);
            System.out.printf("r=%.2f",r);
        }else{
            System.out.println("无实数解!");
        }
    }
}

Enter a few set of values, a look at the running instance

请输入a,b,c:1.0 3 1
r1=-0.38r2=-2.62

step:

1. The user is prompted to enter a three-digit

2. If you look at triple-digit, it is that if the first and third as compared palindrome

3. Output

To expand what was originally a positive sequence, we split this number, put it in reverse order makes up a number, such as two equal numbers, it is a palindrome.

Take for 12345;

12345
12345%10=5 12345/10=1234
1234%10=4  1234/10=123
123%10=3   123/10=12
12%10=2    12/10=1
1%10=1     1/10=0
54321
5*10000+4*1000+3*100+2*10+1
(5*1000+4*100+3*10+2)*10+1
((5*100+4*10+3)*10+2)*10+1
(((5*10+4)*10+3)*10+2)*10+1
((((0*10+5)*10+4)*10+3)*10+2)*10+1

I 0 =
I = I * 10 + 5; 5 //
I'm * = 10 + 4; 54 //
I = I * 10 + 3; 543 //
I = I * 10 + 2; 5432 //
I'm * 10 = +1; // 54321

import java.util.Scanner;
class Demo03_04{
    public static void main(String[] args){
        //1.输入一个数字
        Scanner scanner=new Scanner(System.in);
        System.out.print("请输入一个数字:");
        int num=scanner.nextInt();
        int temp=num;
        //2.拼接出该数字的反序
        int sum=0;
        sum=sum*10+num%10;
        num/=10;
        sum=sum*10+num%10;
        num/=10;
        sum=sum*10+num%10;
        num/=10;
        if(sum==temp){
            System.out.println("是回文");
        }else{
            System.out.println("不是回文");
        }
    }
}

Enter a three-digit number, look at the results:

请输入一个数字:121
是回文
请输入一个数字:234
不是回文

Let's analyze the problem:

Data: a digital input of a digital computer user com randomly generated usr
    0. 1 2
    rock paper scissors
steps:
1. The user is prompted to enter a number
2 computer generate a random number
3. comparing the two numbers, winning or losing points
    draw com == usr
    user win usr = 0 com = 2 | usr = 1 com = 0 | usr = 2 com = 1
    the user input is a user input rest

Random number
    Math.random () [0,1.0). 3 * -> [0,3.0) -> (int) [0,3.0)
    the Random the nextInt (n-)

Here is the code:

import java.util.*;
class Demo03_06{
    public static void main(String[] args){
        //1.
        Scanner scanner=new Scanner(System.in);
        System.out.print("请输入 剪刀0 石头1 布2:");
        int usr=scanner.nextInt();
        //2.
        Random random=new Random();
        int com=random.nextInt(3);
        String usrStr="";
        String comStr="";
        switch(usr){
            case 0: //if usr==0
                usrStr="剪刀"; 
                break;
            case 1:// if usr==1
                usrStr="石头";
                break;
            case 2:// if usr==2
                usrStr="布";
                break;
        }
        switch(com){
            case 0: 
                comStr="剪刀"; 
                break;
            case 1:
                comStr="石头";
                break;
            case 2:
                comStr="布";
                break;
        }
        //3.
        if(usr==com){
            System.out.printf("用户是%s,电脑是%s,平局",usrStr,comStr);
        }else if(usr==0&&com==2 || usr==1&&com==0 || usr==2&&com==1){
            System.out.printf("用户是%s,电脑是%s,用户赢",usrStr,comStr);
        }else{
            System.out.printf("用户是%s,电脑是%s,用户输",usrStr,comStr);
        }
    }
}

Enter a few random numbers, play with this game!

请输入 剪刀0 石头1 布2:1
用户是石头,电脑是布,用户输

3.7 Title:

This question is to analyze:

Step: prompt the user for year, month, day

The formula

Export

Note: in January and February used in the formula are 13 and 14 years, he said at the same time the year was changed to the previous year.

import java.util.Scanner;
class Demo03_07{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        //1.先输入年份
        System.out.print("请输入年份:");
        int year=scanner.nextInt();
        //2.输入月份 1月 2月分别用13 14代替 同时year-1
        System.out.print("请输入月份:");
        int month=scanner.nextInt();
        //3.输入日期
        System.out.print("请输入日期:");
        int day=scanner.nextInt();

        //4.对特殊的1月和2月做处理
        if(month==1||month==2){
            month+=12;
            year-=1;
        }
        //5.套公式
        int h=(day+26*(month+1)/10+year%100+year%100/4+year/100/4+5*year/100)%7;
        
        switch(h){
            case 0:
                System.out.println("是周六");
                break;
            case 1:
                System.out.println("是周日");
                break;
            case 2:
                System.out.println("是周一");
                break;
            case 3:
                System.out.println("是周二");
                break;
            case 4:
                System.out.println("是周三");
                break;
            case 5:
                System.out.println("是周四");
                break;
            case 6:
                System.out.println("是周五");
                break;
        }
    }
}

Output result code is:

请输入年份:2015
请输入月份:1
请输入日期:25
是周日

3.9 Title:

analysis:

It prompts the user for the coordinates of a point;

Control range of x and y;

Output, point outside the triangle,

Point inside the triangle

import java.util.Scanner;
class Demo03_09{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        //1.提示用户输入一个点的坐标
        System.out.print("请输入一个坐标:");
        double x=scanner.nextDouble();
        double y=scanner.nextDouble();
        //2.先大致判断一下坐标的范围
        //3.再精确的判断坐标的范围
        if(x>=0&&x<=200&&y<=-0.5*x+100){
            System.out.println("点再三角形内");
        }else{
            System.out.println("点再三角形外");
        }
    }
}
请输入一个坐标:100.5 25.5
点再三角形内


请输入一个坐标:100. 50.5
点再三角形外

analysis:

To enter the center of the large rectangle, the width and height;

Enter the center of the small rectangle, the width and height;

Paint, small rectangles is determined according to the conditions where a large rectangle;

Export

import java.util.Scanner;
class Demo03_10{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        //1.先输入大矩形的中心,宽和高
        System.out.print("请输入第1个矩形的信息:");
        double x1=scanner.nextDouble();
        double y1=scanner.nextDouble();
        double w1=scanner.nextDouble();
        double h1=scanner.nextDouble();
        //2.再输入小矩形的中心,宽和高
        System.out.print("请输入第2个矩形的信息:");
        double x2=scanner.nextDouble();
        double y2=scanner.nextDouble();
        double w2=scanner.nextDouble();
        double h2=scanner.nextDouble();

        double inXMin=x1-(w1-w2)/2;
        double inXMax=x1+(w1-w2)/2;
        double inYMin=y1-(h1-h2)/2;
        double inYMax=y1+(h1-h2)/2;

        double outXMin=x1-(w1+w2)/2;
        double outXMax=x1+(w1+w2)/2;
        double outYMin=y1-(h1+h2)/2;
        double outYMax=y1+(h1+h2)/2;
        if(x2>=inXMin&&x2<=inXMax&&y2>=inYMin&&y2<=inYMax){
            System.out.println("小矩形在大矩形里面!");
        }else if(x2<=outXMin||x2>=outXMax||y2<=outYMin||y2>=outYMax){
            System.out.println("小矩形在大矩形外面!");
        }else{
            System.out.println("小矩形和大矩形相交!");
        }
    }
}

 Enter several values ​​look at the results:

请输入第1个矩形的信息:2.5 4 2.5 43
请输入第2个矩形的信息:1.5 5 0.5 3
小矩形在大矩形里面!

C:\Users\ASUS\Desktop\xxl.code>javac Demo03_10.java

C:\Users\ASUS\Desktop\xxl.code>java Demo03_10
请输入第1个矩形的信息:1 2 3 5.5
请输入第2个矩形的信息:3 4 4.5 5
小矩形和大矩形相交!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

发布了6 篇原创文章 · 获赞 0 · 访问量 77

Guess you like

Origin blog.csdn.net/weixin_45042315/article/details/104253536