Java_ Basic Grammar programming exercises

Source: Internet
Author: User

One: Let the computer randomly produce 100 integers between 0 and 100, each representing a student's score, and calculating the average of those scores.

 Public Static void Main (string[] args) {        int sum = 0;          for (int i = 0; i < i++) {            int n = (int) (Math.random () *101) 
    + = n;        }        System.out.println ("average value:" + (sum/100.0));    }

Two: Enter a number of 100-999 to determine if it is a narcissus number

 Public Static voidMain (string[] args)throwsmyexception {System.out.println ("Please enter a three-bit integer greater than 100 less than 1000:"); Scanner SC=NewScanner (system.in); intinput =Sc.nextint (); if(Input < | | input > 1000) {            Throw NewMyException ();//Customize the Exception class to determine whether the input data meets the criteria        }        intA = input/100; intb = INPUT/10% 10; intc = input% 10; if(Input = = A * A * a + b * b * b + c * c *c) {System.out.print (input+ "is narcissus number" + "\ n" + input + "=" + (A + "*" + A + "*" + a) + "+" + (b + "*" + B + "*" +b) + "+" + (c + "*" + C + "*" +c)); } ElseSystem.out.print (Input+ "Not narcissus number"); }

Three: Judging whether a number is odd or even

 Public Static voidMain (string[] args) {//receiving data from the keyboardSystem.out.println ("Please enter an integer:"); Scanner s=NewScanner (system.in); intx =S.nextint (); intA = x% 2; if(A! = 0) {System.out.println (x+ "is odd"); }        Else{System.out.println (x+ "is even"); }    }

Four: The entrance Examination question: English, mathematics C language triple total score is equal to 230, and the English result is greater than equal to 60 to study

 Public Static voidMain (string[] args) {//Entrance Examination Questions: English, Mathematics C language, the total score is greater than or equal to 230, and the English result is more than//to study.System.out.print ("Please input your English score:"); Scanner 中文版=NewScanner (system.in); Doublex =english.nextdouble (); System.out.print ("Please enter your math score:"); Scanner Math=NewScanner (system.in); Doubley =english.nextdouble (); System.out.print ("Please enter your C-language score:"); Scanner Cenglish=NewScanner (system.in); Doublez =cenglish.nextdouble (); //Total of all courses        Doublesum = x + y +Z; System.out.println ("English score is" +x); System.out.println ("Total of all courses" +sum); if(x >= && sum >= 230) {System.out.println ("Congratulations on your success!"); }Else{System.out.println ("I'm sorry, but you need to refuel!!!"); }    }

Five: Enter a number, determine whether it can be 3 full out, and output the corresponding prompt information

 Public Static void Main (string[] args) {        // Enter a number to determine if it can be 3 full, and output the corresponding hint message        System.out.print ("Please enter a number:") ;         New Scanner (system.in);         double x = a.nextdouble ();                 if ((x% 3) = = 0)            {+ "can be divisible by 3");        } Else {            + "cannot be divisible by 3");}    }

Six: Determine the maximum value of two numbers and output

 Public Static voidMain (string[] args) {//determine the maximum value of two numbers and outputSystem.out.print ("Please enter the first number:"); Scanner a=NewScanner (system.in); Doublex =a.nextdouble (); System.out.print ("Please enter a second number:"); Scanner b=NewScanner (system.in); Doubley =b.nextdouble (); //Three mesh operator        Doublez = (x > Y)?x:y; System.out.println ("Maximum of two numbers is:" +z); }

Seven: Use the If-else statement to determine whether the year entered is leap years
Year that can be divisible by 4 but not divisible by 100, or a year that can be divisible by 400

 Public Static voidMain (string[] args) {//use the If-else statement to determine whether the year entered is a leap years//year that can be divisible by 4 but not divisible by 100, or a year that can be divisible by 400System.out.print ("Please enter a year:"); Scanner SC=NewScanner (system.in); intYear =Sc.nextint (); if(((Year percent 4 ==0) && ((year & 100) = 0)) | | (Year% 400 = = 0) {System.out.println ( year+ "is a leap year"); }Else{System.out.println ( year+ "Not leap year"); }    }

Eight: the keyboard input 1-7 of any number, respectively, output the corresponding information

 Public Static voidMain (string[] args) {//Keyboard input 1-7 of any number, respectively output the corresponding informationSystem.out.print ("Please enter any number of 1-7:"); Scanner SC=NewScanner (system.in); intWeek =Sc.nextint (); Switch(week) { Case1: System.out.println ("Today is Monday");  Break;  Case2: System.out.println ("Today is Tuesday");  Break;  Case3: System.out.println ("Today is Wednesday");  Break;  Case4: System.out.println ("Today is Thursday");  Break;  Case5: System.out.println ("Today is Friday");  Break;  Case6: System.out.println ("Today is Saturday");  Break;  Case7: System.out.println ("Today is Sunday");  Break; default: System.out.println ("The number you entered is not valid");    } sc.close (); }

Java_ Basic grammar programming exercises

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.