Java programming problem (5)

This is my 5th to share questions, 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_17

Here Insert Picture Description
Prior to look at this question, we have to understand a knowledge
of what is a prime number is in addition to 1 and itself no other numbers can be divisible
if this number is num, then m is a number between 2 ~ num,
if this number m meet: num% m == 0 then num is not a prime number
if not one find the words that number is prime this is my thinking
here is my code demonstrates:

class Demo03_17{
    public static void main(String[] args){
        int count=0;  //当前素数的个数
        boolean flag=true;
        for(int num=2;num<=1000;num++){
            for(int m=2;m<=num-1;m++){
                if(num%m==0){
                    flag=false;
                    break;
                }
            }
            if(flag){
                count++;
                System.out.print(num+" ");
                if(count%8==0){ //8 16 24 32
                    System.out.println();
                }
            }
            flag=true;
        }
    }
}

Demo03_18

Here Insert Picture Description
This question is for a non-stop cycle, enter how many times, how much will the next cycle, each set formula,
nothing difficult,
there is little skill, is that you see him molecule, is a power operation, the operation in wait until the computer is running on large numbers, and there may Caton, the computer did not count here, if I change it to run every time another symbol, you will find, calculate faster.
Here is the code:

import java.util.Scanner;
class Demo03_18{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        System.out.print("请输入一个数字:");
        int imax=scanner.nextInt();
        double sum=0;
        double flag=1;
        for(int i=1;i<=imax;i++){
            //sum+=Math.pow(-1.0,i+1);/(2*i-1);
            sum+=flag/(2*i-1);
            flag=-flag;
        }
        double pi=sum*4;
        System.out.println(pi);
        // 累乘 Math.pow(-1.0,i+1) for -1*-1*-1....
    }
}

Demo03_19

Here Insert Picture Description
The problem with the above title is almost the same, is not the same calculation formula. This simple, for which the change, then, is not difficult
when the following code:

import java.util.Scanner;
class Demo03_19{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        System.out.print("请输入一个数字:");
        int imax=scanner.nextInt();
        double e=1;
        double item=1.0;
        for(int i=1;i<=imax;i++){
            item=item*i;    //n!=(n-1)*n;
            e+=1/item;
        }
        System.out.println(e);
    }
}

Demo03_20

Here Insert Picture Description
The question in my title on a set of the same type which has a
look at this question, we must first understand what is the next leap year.
Leap year and leap year is divided into ordinary century leap year.
Common leap: calendar year is a multiple of 4, and is not a multiple of 100, a leap year. ;
Leap year century: calendar year is the 100th number, must be a multiple of 400 is the century leap year;
this is the basic concept of leap year, the following simple.
Conditions 1 to
2 satisfying the condition output
3 in the line wrap.
Here is the code:

class Demo03_20{
    public static void main(String[] args){
        int count=0;
        for(int i=101;i<=2100;i++){
            if(i%4==0&&i%100!=0 || i%400==0){
                count++;
                System.out.print(i+" ");
                if(count%10==0){
                    System.out.println();
                }
            }
        }
        System.out.println("\n闰年一共"+count+"个");
    }
}

Demo03_21

Here Insert Picture Description
The question is:
1. to be 6 to 10000 for cycle
2. Add a for loop in the cycle, from 1 to half this number
3. Adoption of the rules of operation to find out the number of qualifying
Here is my code:

class Demo03_21{
    public static void main(String[] args){
        int sum=0;
        for(int n=2;n<=10000;n++){   
            for(int i=1;i<=n/2;i++){
                if(n%i==0){
                    sum+=i;
                }
            }
            if(n==sum){
                System.out.println("完全数"+n);
            }
            sum=0;
        }
    }
}

Demo03_22

Here Insert Picture Description
The question on a topic where there have been set, nothing more than added a condition to establish two more than the number, when there is a number equal to two, and then the game ends, the output statement,
the following is my code:

import java.util.*;
class Demo03_22{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        Random random=new Random();
        int usrWin=0;
        int comWin=0;
        while(true){
            System.out.print("请输入剪刀0 石头1布2:");
            int usr=scanner.nextInt();
            int com=random.nextInt(3);
            String usrStr = "";
            String comStr = "";
            switch(usr){
                case 0:
                    usrStr = "scissor"; 
                    break;
                case 1:
                    usrStr = "rock";
                    break;
                case 2:
                    usrStr = "paper";
                    break;
            }
            switch(com){
                case 0: 
                    comStr = "scissor"; 
                    break;
                case 1:
                    comStr = "rock";
                    break;
                case 2:
                    comStr = "paper";
                    break;
            }
            if(usr==com){
                System.out.printf("The computer is %s.you are %s too.It is a draw\n",comStr,usrStr);
            }else if(usr==0&&com==2 || usr==1&&com==0 || usr==2&&com==1){
                System.out.printf("The computer is %s.you are %s.You won\n",comStr,usrStr);
                usrWin++;
            }else{
                System.out.printf("The computer is %s.you are %s.You lost\n",comStr,usrStr);
                comWin++;
            }
            if(usrWin==2||comWin==2){
                break;
            }
        }
        if(usrWin==2){
            System.out.println("最终玩家赢!");
        }else{
            System.out.println("最终电脑赢!");
        }
    }
}

Demo03_23

Here Insert Picture Description
The problem, you know binary decimal turn in the end how to turn:
that is, to a decimal number, stop by 2 to obtain a remainder, recorded, but also to get a result, then divide the result by 2, getting a the remainder, a result, in addition to non-stop, until the results of 0 to whom, stop operation, to each of more than a number from the back, in order to write, is his binary.
Know the future, you can implement your program.
Here is my code:

import java.util.Scanner;
class Demo03_23{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        System.out.print("请输入一个数字:");
        int num=scanner.nextInt();
        String binStr="";
        while(true){
            binStr=num%2+binStr;//"1100"
            num/=2;
            if(num==0){
                break;
            }
        }
        System.out.println(binStr);
    }
}

Demo03_24

Here Insert Picture DescriptionThis question is to compare your proficiency with the assignment to understand Shashi Hou who to assign appropriate, and more appear every one to the number of count + 1; get away, looking very long, in fact, very simple,
Here is my code:

import java.util.Scanner;
class Demo03_24{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        int num=0;
        int max=0;
        int count=0;
        System.out.print("请输入数字:");
        while(true){
            num=scanner.nextInt();
            if(num==0){
                break;
            }else if(num>max){
                max=num;
                count=1;
            }else if(num==max){
                count++;
            }
        }
        System.out.println("max="+max+",count="+count);
    }
}

Thank you for reading, if my share to help you, walk like a point okay, thank you.

Published 38 original articles · won praise 51 · views 1206

Guess you like

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