一、三元表示式
如果用三元表示式的話,只用一行就可以搞定,類似於if-else語句console.
writelien
("輸入姓名");
string name = console.
readline()
;if(name ==
"小楊"
)else
console.
readkey()
;
「?」 前是判斷的條件,」?"後, ":"前 是 條件成立執行的語句, 「:」 後是 條件不成立執行的語句
這樣也可以,又省了一句string temp = name ==
"小楊"
?"此人很純潔"
:"此人很**"
;console.
writeline
(temp)
;console.
readkey()
;
二、隨機數console.
writeline
( name ==
"小楊"
?"系統提示此人很純潔"
:"此人很**");
console.
readkey()
;
會顯示0-9的隨機數
三、結構——一次性宣告多個不同型別的變數console.
writeline
("顯示隨機數");
while
(true)
1、結構宣告語法:
[訪問修飾符] struct 結構名
2、列舉宣告語法:
[訪問修飾符] enum 列舉名
3、例子:定義乙個結構叫mycolor,有三個成員,分別定義為int型別的red,green,blue、public struct person
public enum gender
class program
}
宣告乙個mycolor 型別的變數,並對其成員賦值,使mycolor可以表示成乙個紅色
☺mycolor color;
color._red =
255;
color._green =0;
color._blue =
0;
四、陣列——一次性宣告多個相同型別的變數
①宣告1、陣列宣告第一種
長度為10,索引為0-9,int 型別
2、第二種int
nums = new int[10
];
此陣列只能存5個值,長度為5,下標為0-4
3、第三種int
nums2 = new int
;
長度為3,索引為0-2
4、第四種——比較簡化的方式int
nums3 = new int[3
];
②陣列的儲存int
nums4 =
;
nums[5]
=23;nums[7]
=100
;console.
writeline
(nums[5]
);console.
writeline
(nums[7]
);console.
readkey()
;
賦值int
nums = new int[5
];nums[0]
=10;nums[1]
=20;nums[2]
=30;nums[3]
=40;nums[5]
=50;
取值for
(int i=
0; i<
5; i++
)
遍歷
陣列的存值和取值都是通過 下標, 或者 索引 來進行 賦值 和 取值 的for
(int i =
0; i<
5; i++
)console.
readkey()
;
C 三元表示式和隨機數
三元表示式 bool result 5 3?true false console.writeline 輸入姓名 string name console.readline if name 高山 else console.readkey 三元表示式,只需一行就能解決 string temp name 高...
型別轉換 三元表示式 列舉 隨機數
1型別轉換 將string型別的123轉為int型別的123 方式1 int number1 convert.toint32 123 console.writeline number1 方式2 int number2 int.parse 456 console.writeline number2 轉...
C 三元表示式詳情
if else語句非常常用,但在進行一些簡單邏輯判斷的時候,會顯得有些不太簡潔。特別是在初始化的時候,比如我們有乙個變數,某種情況下賦值成a,另外的情況下賦值成b。使用if else語句寫出來就是 int cur if condition else 這當然是沒問題的,只是在大量使用的時候會顯得有些繁...