-
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
????????????int x = 1;
????????????int sum = 0;
????????????while(x <=30)
?????????????{
????????????????????if(x % 2 == 0)
????????????????? ? ????sum += x;
????????????????????x++;
??????????????}
?????????????Console.WriteLine("1-30奇數和:"+sum);
查看全部 -
namespace Test
{
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? for(int x = 1;x <=7;x++)//循環(huán)7行
? ? ? ? ????{
??????????????????for(int y = 1;y <= 7;y++)//循環(huán)7列
????????????????? ?{
????????????????????????if(x == y || x + y ==8)
????????????????????????????Console.Write("O");
????????????????????}
????????????????????else
????????????????????{
????????????????????????Console.Write(".");
????????????????????}? ????
??????????????}
????????????????????? Console.WriteLine();
? ? ? ? }
? ? }
}
查看全部 -
關鍵字?class?,這個關鍵字的用途是聲明類。比如上面例子中,類名叫做Program。
關鍵字?namespace?,這個關鍵字的用途是聲明“命名空間”。比如上面例子中,命名空間叫做MyApp1。
關鍵字?using?,這個關鍵字的用途是導入命名空間。比如這句:using?System.Text;?作用是導入System.Text命名空間中的類。
關鍵字?static?(靜態(tài)的)、?void?(無返回值)、?string?(字符串類型)。常常在Main()方法的聲明中看到:static?void?Main(string[] args)
Main() 方法是 C# 中的特殊方法,是程序的入口,就是說,如果沒有 Main ()方法,程序就無法啟動。
注意:你會發(fā)現(xiàn),所有關鍵字都是由小寫字母組成的,C#語言中,大小寫是嚴格區(qū)分的。
查看全部 -
using System;
using System.Collections.Generic;
using System.Text;
namespace projGetMaxScore
{
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ?string[] name = { "1", "2", "伏晨", "4", "5", "6", "7", "8" };
? ? ? ? ? ??
? ? ? ? ? ? int[] score = { 89, 90, 98, 56, 60, 91, 93, 85 };
? ? ? ? ? ? int max = 0;
? ? ? ? ? ? int cc = 0;? ? ? ? ? ?
? ? ? ? ? ? for (int i = 0; i < score.Length; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (max < score[i])
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? max = score[i];
? ? ? ? ? ? ? ? ? ? cc = i;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? Console.Write("分數最高的是"+name[cc]+",分數是"+max);??
? ? ? ? }
? ? }
}
查看全部 -
?: 就是條件運算符,可以看到它有3個操作數,所以又被稱為三元運算符。它的運算邏輯是:當條件表達式為 true 時,執(zhí)行分支1;當條件表達式為 false 時,執(zhí)行分支2。查看全部
-
條件為 true 時執(zhí)行的分支寫在 if() 后面的{}中;條件為 false 時執(zhí)行的分支寫在 else 后面的{}中。查看全部
-
使用變量分為3步:聲明、賦值、使用
聲明變量的語法: 數據類型 變量名;
給變量賦值的語法:變量名=值;
查看全部 -
class——聲明類
namespace——命名空間
using——導入命名空間
static靜態(tài)的 void無返回值 string字符串類型
Main()方法是C#中的特殊方法,是程序的入口,如果沒有Main()方法,程序就無法啟動。
查看全部 -
namespace 命名空間
class類
Main方法是程序入口
Console.Write>=輸出
查看全部 -
using System;
using System.Collections.Generic;
using System.Text;
namespace projGetMaxScore
{
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? string [] name={"吳松","錢東宇","伏晨","陳陸","周蕊","林日鵬","何昆","關欣"};
? ? ? ? ? ? int []score={89,90,98,56,60,91,93,85};
? ? ? ? ? ? int x=0;
? ? ? ? ? ? int y=0;
? ? ? ? ? ? for(int i=0;i<score.Length;i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if(score[i]>x)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? x=score[i];
? ? ? ? ? ? ? ? ? ? y=i;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? Console.Write("分數最高的是"+name[y]+","+"分數是"+x+",");
? ? ? ? }
? ? }
}
查看全部 -
變量名不能出現(xiàn)其他字符
變量名不能用數字開頭
不能拿關鍵字作為變量名
查看全部 -
?bool a = ++x * x > 3;
①一元運算符( 自增自減符、!邏輯非 )優(yōu)先級最高
②++x,使用變量x前使其值自增1,那么此例中++x*x = 2*2
需要注意的是這里變量x使用前已經自增,而不是只有使用了自增符的變量++x
自增了,因為變量名指向的地址值再使用前就已經自增完成
查看全部 -
Console.WriteLine(19/5);//求19除以5的商,輸出3
Console.WriteLine(19%5);//求19除以5的余數,輸出4(商3余4)
查看全部 -
兩個整數進行運算,結果只保留整數部分
查看全部 -
int x = 0; //0
x++; //1
x += 3; //4
x %= 2; //0查看全部
舉報