Java programming problem (3)

This is my third topic to share, if you want to see previous examples, please open my blog, view job columns. Thanks for watching.

Today we look at some on flow control statements of example, the cycle is that we often use to. Here with a look at the title bar on.

Demo03_01

Here Insert Picture Description
The question we first find the data: abc delt r1 r2.
This problem can generally be divided into these Steps:
1. abc prompt the user three parameters;
2. Compute B = delt B. 4- A * C;
3. delt the determination value;
wherein it delt value differences: he divided three cases: the DELT> 0 || the DELT> 0 || the DELT <0
delt> 0: two Solutions output
delt> 0: output a solution
delt <0: no real solution number

Here is my code:

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

Demo03_02

Here Insert Picture Description
The question we first find data: abcdefxy
substantially the title to be divided into these Steps:
1. abcdef prompt the user for the six parameter;
2. Compute XY;
3. Analyzing the value of ad-bc;
Here is my code:

import java.util.Scanner;
class Demo03_02{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter 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();
     	if((a*d-b*c)==0){
            System.out.println("此方程无解");
        }
        else {
            double m =(e*d-b*f)/(a*d-b*c);
            double n =(a*f-e*c)/(a*d-b*c);
            System.out.println("x =" + m + "y =" + n);
       }
    }
}

Demo03_03

Here Insert Picture Description
The question we first find the data: the number of days the next day of the week next week today, a few
we can think about a formula:
(a few + next few days of the week today)% 7 = future of the week
because the week is every seven days a cycle ? No matter how change, you work out a few, is the representative of the week.
For example:
January 1 Monday 1% 7 = 1
1 Yue 20 Ri 20% 7 = 6
steps:
1. Enter day of the week
2. Enter the next few days
3. Print the next few days of the week

Here is my code: `

import java.util.Scanner;
class Demo03_03{
    public static void main(String[] args){
        //1.
        Scanner scanner=new Scanner(System.in);
        System.out.print("今天是周几:");
        int today=scanner.nextInt();
        //2.
        System.out.print("未来的几天:");
        int future=scanner.nextInt();
        //3.
        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="周六";
        }

        if(futureDay==0){
            futureDayStr="周日";
        }else if(futureDay==1){
            futureDayStr="周一";
        }else if(futureDay==2){
            futureDayStr="周二";
        }else if(futureDay==3){
            futureDayStr="周三";
        }else if(futureDay==4){
            futureDayStr="周四";
        }else if(futureDay==5){
            futureDayStr="周五";
        }else if(futureDay==6){
            futureDayStr="周六";
        }
        System.out.println("今天是"+todayStr+",未来的日子是"+futureDayStr);
    }
}

Demo03_04

Here Insert Picture Description
This problem did not you see what's the law?
In fact, we considered whether a number is a palindrome, you put him upside down to solve
reversed to compare with the original figures, the same palindrome is
roughly divided on the question of these Steps:
1. prompts the user to enter a number;
2. Calculate raw digital reverse order;
3 determines whether palindrome;
Here is my code: '

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;
        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("不是回文");
        }
    }
}

Demo03_06

Here Insert Picture Description
This problem of 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. compare two numbers, points win or lose, a total of three cases:
a draw: com == usr
users win: usr = 0 com = 2 | usr = 1 com = 0 | usr = 2 com = 1
user input: else

Here is the code demonstrates:

import java.util.*;//*表示导入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);
        }
    }
}

Demo03_08

Here Insert Picture Description
Data this question: the coordinates from the center point of the circle radius of the circle.
Step:
1. coordinates of a point prompts the user
2 calculates a distance between the center point to
3. Analyzing the relationship between the distance and the radius
distance> points outside the circle radius
circle radius == point distance on
the distance <radius point within a circle
Here is the code demonstrates:

import java.util.Scanner;
class Demo03_08{
    public static void main(String[] args){
        //1.
        Scanner scanner=new Scanner(System.in);
        System.out.print("请输入坐标点:");
        double x=scanner.nextDouble();
        double y=scanner.nextDouble();
        //2.
        double xo=0;
        double yo=0;
        double radius=10;
        double distance=Math.sqrt(Math.pow(x-xo,2)+Math.pow(y-yo,2));
        //3.
        if(distance>radius){
            System.out.println("点在圆外");
        }else if(distance==radius){
            System.out.println("点在圆上");
        }else{
            System.out.println("点在圆内");
        }
    }
}

This is today's share examples, many examples of this, in three installments publishing, thank you for reading.

Published 38 original articles · won praise 51 · views 1208

Guess you like

Origin blog.csdn.net/xweiwxh/article/details/104266158