Java chapter exercises

 

analysis

Values: a b c delt Solutions

step:

1, a b c prompted

2, the value calculated delt

3, using the equation solving equation

import java.util.Scanner;

class Demo3_1{
    public static void main(String[] args){

    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入a b c的值:");
    double a = scanner.nextDouble();
    double b = scanner.nextDouble();
    double c = scanner.nextDouble();

    double delt=b*b-4*a*c;

    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("无实属根!");
    }
}
}

analysis

Value: constant x value y value in the equations a * db * c

step:

1, prompt the user to enter the value of abcdef

2, the value calculated according to the formula x y

3, computing values ​​a * db * c

4. The value of a * db * c print result

import java.util.Scanner;
class Demo3_2{
    public static void main(String[] args){

        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入a,b,c,d,e,f的值:");
        double a = scanner.nextDouble();
        double b = scanner.nextDouble();
        double c = scanner.nextDouble();
        double d = scanner.nextDouble();
        double e = scanner.nextDouble();
        double f = scanner.nextDouble();

        double x = (e*d-b*f)/(a*d-b*c);
        double y = (a*f-e*c)/(a*d-b*c);
        double i = a*d-b*c;
        if(i == 0){
            System.out.print("方程式无解");
        }else{
            System.out.printf("x=%.2f,y=%.2f",x,y);
        }
    }
}

analysis:

Value: This is the week after next days 

step:

1, today is prompted for the week

2, prompt for the next few days

3, after the next few days is calculated according to the formula of the week

4, the output value of the week into character

import java.util.Scanner;

class Demo3_3{
    public static void main(String[] args){

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入今天是周几:");
        int today = scanner.nextInt();
        System.out.println("未来几天后:");
        int future = scanner.nextInt();

        int futureDay = (today+future)%7;

        String todayStr = "";
        String futureDayStr = "";

        if(today==0){
            todayStr="周日";
        }else if(today==1){
            todayStr="周一";
        }else if(today==2){
            todayStr="周二";
        }else if(today==3){
            todayStr="周三";
        }else if(today==4){
            todayStr="周四";
        }else if(today==5){
            todayStr="周五";
        }else if(today==6){
            todayStr="周六";
        }
    
        switch(futureDay){
            case 0:
            futureDayStr="周日";
            break;
            case 1:
            futureDayStr="周一";
            break;
            case 2:
            futureDayStr="周二";
            break;
            case 3:
            futureDayStr="周三";
            break;
            case 4:
            futureDayStr="周四";
            break;
            case 5:
            futureDayStr="周五";
            break;
            case 6:
            futureDayStr="周六";
            break;
        }
        System.out.println("今天是"+todayStr+"未来"+future+"天后是"+futureDayStr);
    }   
}     

analysis:

Value: Enter the reverse order of each number obtained

step:

1, prompts the user to enter a number

2, using modulo arithmetic and the number of the bits required in addition

3, obtains the digital reverse order

4, and the comparative numbers in reverse order of the digital

5, it is equal to a palindrome, not otherwise

import java.util.Scanner;
class Demo3_4{
    public static void main(String[] args){

        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入一个三位数字");
        int num = scanner.nextInt();
        int temp=num;
        int sum = 0;
        sum = sum*10+num%10;
        num/=10;
        sum = sum*10+num%10;
        num/=10;
        sum = sum*10+num%10;

        if(sum==temp){
            System.out.println("这个数字是回文数字!");
        }else{
            System.out.println("这个数字不是回文数字!");
        }

    }
}

analysis:

Value: After a two-digit numbers split the two computer users and a two-digit random number entered by the user get

step:

1, let the program randomly generates a double-digit

2, the user is prompted to enter a two-digit

3, the two digital computers and users split

4, the digital comparison

5, print results

import java.util.*;

class Demo3_5{
    public static void main(String[] args){

        Random random = new Random();
        int com = random.nextInt(90)+10;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个两位整数:");
        int user = scanner.nextInt();

        int a = com % 10;
        int b = com / 10;
        int c = user % 10;
        int d = user / 10;

        if(com==user){
            System.out.print("恭喜您,获得奖金10000美元!");
        }else if(a==d&&b==c){
            System.out.print("恭喜您,获得奖金3000美元!");
        }else if(a==c || a==d || b==c || b==d){
            System.out.print("恭喜您,获得奖金1000美元!");
        }else{
            System.out.print("很遗憾,您未中奖!");
        }

    }
}

analysis:

Values: a computer user to input an integer of 0 to randomly generated integers 0 stone scissors 1 a 0-2 cloth 2

step:

1, prompt a user to enter an integer of 0 to 2

2, a computer randomly generates an integer of 0 to 2

3, comparison of two numbers, comparing winners and losers

Com == user draw

    User Win user = 0 com = 2 | user = 1 com = 0 | user = 2 com = 1

    Users lose the rest are user input

import java.util.Scanner;
import java.util.Random;
class Demo3_6{
    public static void main(String[] args){

        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入:剪刀0,石头1,布2:");
        int user = scanner.nextInt();

        Random random = new Random();
        int com = random.nextInt(3);

        String userStr ="";
        String comStr ="";

        switch(user){
            case 0:
                userStr="剪刀";
                break;
            case 1:
                userStr="石头";
                break;
            case 2:
                userStr="布";    
                break;
        }
            
        switch(com){
            case 0:
                comStr="剪刀";
                break;
            case 1:
                comStr="石头";
                break;
            case 2:
                comStr="布";    
                break;
        }    
    if(user==com){
        System.out.printf("用户是%s,电脑是%s,平局",userStr,comStr);
    }else if(user==0&&com==2 || user==1&&com==0 || user==2&&com==1){
        System.out.printf("用户是%s,电脑是%s,用户赢",userStr,comStr);
    }else{
        System.out.printf("用户是%s,电脑是%s,用户输",userStr,comStr);
    }

    }
}

analysis:

Pow: Enter the years, months, days of the week

step:

1, to remind the user to enter date

2, processed 1, February meaning of the questions

3, according to the formula

4, printout words

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;
        }
    }
}

analysis:

Values: the radius of the coordinate input point coordinates of the circle center

step:

1, the user is prompted to enter a coordinate point

2, calculates the distance to the center point

3, a comparison of the size of the radius of the distance

4, the position of the output point

import java.util.Scanner;
class Demo3_8{
    public static void main(String[] args){

        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入一个点的坐标x y:");
        double x = scanner.nextDouble();
        double y = scanner.nextDouble();

        double x0 = 0;
        double y0 = 0;
        double radius = 10;
        double distance = Math.sqrt(Math.pow((x0-x),2)+Math.pow((y0-y),2));

        if(distance==radius){
            System.out.print("点在圆上");
        }else if(distance>radius){
            System.out.print("点在圆外");
        }else{
            System.out.print("点在圆内");
        }

    }
}

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("点再三角形外");
        }
    }
}

import java.util.*; 
class Demo3_10{    
    public static void main(String[] args){         
        Scanner scanner = new Scanner(System.in);         
        System.out.print("请输入第一个矩形的中心点坐标x,y和长,高:");         
        double x1 = scanner.nextDouble();         
        double y1 = scanner.nextDouble();         
        double w1 = scanner.nextDouble();         
        double h1 = scanner.nextDouble();         
        System.out.print("请输入第二个矩形的中心点坐标x,y和长,高:");         
        double x2 = scanner.nextDouble();         
        double y2 = scanner.nextDouble();         
        double w2 = scanner.nextDouble();         
        double h2 = scanner.nextDouble();         
        //计算第二个矩形中心点的范围         
        double inMinX = x1-(w1-w2)/2;         
        double inMaxX = x1+(w1-w2)/2;         
        double inMinY = y1-(h1-h2)/2;         
        double inMaxY = y1+(h1-h2)/2;         
        double outMinX = x1-(w1+w2)/2;         
        double outMaxX = x1+(w1+w2)/2;         
        double outMinY = y1-(h1+h2)/2;         
        double outMaxY = y1+(h1+h2)/2;         
        //判断是否在内或外或嵌套         
        if(x2>=inMinX&&x2<=inMaxX&&y2>=inMinY&&y2<=inMaxY){             
            System.out.println("在内");         
            }else if(x2>=outMaxX||x2<=outMinX||y2>=outMaxY||y2<=outMinY){             
                System.out.println("在外");         
                }else{             
                    System.out.println("嵌套");        
             }     
         } 
     }

analysis:

step:

1, a digital input
2, and then determines whether a number is 0
3 0 exit if not then continue to

import java.util.Scanner;
class Demo3_11{
    public static void main(String[] args){

        Scanner scanner = new Scanner(System.in);
        double sum=0;
        int positives=0;
        int negatives=0;
        System.out.print("请输入若干个数字:");

        while(true){
            int num = scanner.nextInt();
            if(num!=0){
                sum+=num;
                if(num>0){
                    positives++;
                }else{
                    negatives++;
                }
             }else{
                 break;
             }
        }
        if(positives+negatives==0){
            System.out.println("没有其他的数字,除了0!");
        }else{
            System.out.println("正数的个数为:"+positives);
            System.out.println("正数的个数为:"+negatives);
            System.out.println("数字的总和为:"+sum);
            System.out.println("数字的平均值为:"+sum/(positives+negatives));
        }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

发布了7 篇原创文章 · 获赞 0 · 访问量 85

Guess you like

Origin blog.csdn.net/weixin_42668873/article/details/104255882