5. C # basic arrangement (typical for statement exercises-nested with if ),

Source: Internet
Author: User

5. C # basic arrangement (typical for statement exercises-nested with if ),

1,Basic use of the for Loop: After each landing, the ball is played again as the previous 4/5, and the height after the second bounce is calculated.

For (int I = 0; I <5; I ++) {high = high * 4/5;} Console. writeLine ("the height of the ball bounce for 5th times is: {0}", high );

 

Nested use of for loop and if statement:

2. You can guess the box with your computer and win the game in three games. (You have not considered the case of human error. The main reason is to practice the for loop first)

Int sum1 = 0; int sum2 = 0; string ren1; string dn1; for (int I = 0; I <3; I ++) {Random r = new Random (); int dn = r. next (3); // insert a random number from 0, 1, 2 and assign it to the Console. writeLine ("Enter your: 0-Scissors, 1-stone, 2-cloth"); int ren = int. parse (Console. readLine (); if (dn-ren =-1 | dn-ren = 2) sum1 ++; else sum2 ++; if (ren = 0) ren1 = "Scissors"; else if (ren = 1) ren1 = "Stone"; else ren1 = "cloth"; if (dn = 0) dn1 = "Scissors "; else if (dn = 1) dn1 = "Stone"; else dn1 = "cloth"; Console. writeLine ("computer output {0}, you output {1}", dn1, ren1);} if (sum1> sum2) Console. writeLine ("you win"); else if (sum2> sum1) Console. writeLine ("Computer wins"); else Console. writeLine ("Draw ");

3. Sum of 7-related numbers within 100

int sum = 0;for (int i = 0; i < 100; i++){   if (i%7==0||i%10==7||i/10==7)       sum = sum+i;}Console.WriteLine(sum);

4. Calculate the even and, odd and

Int ou = 0; int ji = 0; for (int I = 1; I <101; I ++) {if (I % 2 = 0) ou = ou + I; else ji = ji + I;} Console. writeLine ("even and {0}, odd and {1}", ou, ji );

 

Related Article

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.