初心者向けのビジュアルC#。 講義5.型変換。 列挙、構造、配列

みなさんこんにちは!



最後に、彼は初心者向けにVisual C#で5回目の講義を行いました。 この講義では、変数タイプの変換について説明します。 次に、列挙、構造、および配列について学習します。 もちろん、私は非常に長い間講義がなかったことをおtoびしたいと思います。 これは、私が大学でMicrosoft TechDaysテクノロジーを日間も編成して費やしたためであり、この方向で作業する時間はまったくありませんでした。 また、この講義はおそらく今年最後の講義です。 私は多くの解決された仕事を受け取りました。それを送って解決しようとしたすべての人にとても感謝しています。 電子メールを送信できなかった正しい答えは、以下の投稿にあります。 また、講義の記録に使用されるソースコードは、ビデオの下にあります。



以前の講義へのリンク


講義1.はじめに

講義2.こんにちは、World! Visual C#Express 2010を理解する

講義3.変数と式

講義4.条件とサイクル



以前の宿題への回答:


1.変数var1とvar2に2つの整数が格納されている場合、どちらか(両方ではない)が10より大きいかどうかを調べるには、どのようなブールチェックを実行する必要がありますか?



(var1> 10)^(var2> 10)



2.ユーザーから2つの数字を受け取って画面に表示するが、両方の数字が10より大きい場合はオプションを拒否し、この場合は他の2つの数字を入力することを提案するアプリケーションを作成します。



static void Main( string [] args)

{

double var1, var2;

Console .Write( "First: " ); Double.TryParse( Console .ReadLine(), out var1);

Console .Write( "Second: " ); Double.TryParse( Console .ReadLine(), out var2);



while (var1 > 10 && var2 > 10)

{

Console .WriteLine( "More than 10. Again." );

Console .Write( "First: " ); Double.TryParse( Console .ReadLine(), out var1);

Console .Write( "Second: " ); Double.TryParse( Console .ReadLine(), out var2);

}



Console .WriteLine( "First: {0}, second: {1}. Congrats!" , var1, var2);



Console .ReadKey();

}




* This source code was highlighted with Source Code Highlighter .








3.次のコードの何が問題になっていますか(Visual Studioを使用せずにこのタスクを解決してみてください)。

「if((i%2)= 0)」の代わりに「if((i%2)== 0)」にする必要があります



素敵な景色を!







講義資料


実験室1号 型変換。

static void Main( string [] args)

{

short shortResult, shortVal = 4;

int integerVal = 67;

long longResult;

float floatVal = 10.5f;

double doubleResult, doubleVal = 99.999;

string stringResult, stringVal = "17" ;

bool boolVal = true ;



Console .WriteLine( "Variable Conversion Examples\n" );



doubleResult = floatVal*shortVal;

Console .WriteLine( "Implicit, -> double: {0} * {1} -> {2}" , floatVal, shortVal, doubleResult);



shortResult = ( short ) floatVal;

Console .WriteLine( "Explicit, -> short: {0} -> {1}" , floatVal, shortResult);



stringResult = Convert .ToString(boolVal) + Convert .ToString(doubleVal);

Console .WriteLine( "Explicit, -> string: \"{0}\" + \"{1}\" -> {2}" , boolVal, doubleVal, stringResult);



longResult = integerVal + Convert .ToInt64(stringVal);

Console .WriteLine( "Mixed, -> long: {0} + {1} -> {2}" , integerVal, stringVal, longResult);



Console .ReadKey();

}




* This source code was highlighted with Source Code Highlighter .








実験室作業2。 乗り換え

enum orientation : byte

{

north = 1,

south = 2,

east = 3,

west = 4

}



static void Main( string [] args)

{

byte directionByte;

string directionString;

orientation myDirection = orientation.north;

Console .WriteLine( "myDirection = {0}" , myDirection);

directionByte = ( byte ) myDirection;

directionString = Convert .ToString(myDirection);

Console .WriteLine( "byte equivalent = {0}" , directionByte);

Console .WriteLine( "string equivalent = {0}" , directionString);



Console .ReadKey();

}




* This source code was highlighted with Source Code Highlighter .








実験室作業3。 構造。

enum orientation : byte

{

north = 1,

south = 2,

east = 3,

west = 4

}



struct route

{

public orientation direction;

public double distance;

}



static void Main( string [] args)

{

route myRoute;

int myDirection = -1;

double myDistance;

Console .WriteLine( "1) North\n2) South\n3) East\n4) West" );

do

{

Console .WriteLine( "Select a direction:" );

myDirection = Convert .ToInt32( Console .ReadLine());

} while ((myDirection < 1) || (myDirection > 4));

Console .WriteLine( "Input a distance:" );

myDistance = Convert .ToDouble( Console .ReadLine());

myRoute.direction = (orientation) myDirection;

myRoute.distance = myDistance;

Console .WriteLine( "myRoute specifies a direction of {0} and a distance of {1}" ,

myRoute.direction, myRoute.distance);



Console .ReadKey();

}




* This source code was highlighted with Source Code Highlighter .








実験室番号4。 配列

static void Main( string [] args)

{

string [] friendNames = { "Robert" , "Mike" , "Jeremy" };

Console .WriteLine( "Here are {0} of my friends:" , friendNames.Length);



foreach ( string friendName in friendNames)

{

Console .WriteLine(friendName);

}



Console .ReadKey();

}




* This source code was highlighted with Source Code Highlighter .








新しい宿題


1.暗黙的に実行できない操作は次のとおりです。

a)要するにint

b) intが短い

c)文字列のブール

d) floatのバイト



2. 4つの異なる色を含む色をリストするためのshortタイプに基づいてコードを作成します。 そのような列挙はバイトに基づいているのでしょうか?



3.次のコードはコンパイルされますか?その理由は?

string [] blab = new string [5]

string [5] = 5th string ;



* This source code was highlighted with Source Code Highlighter .








PS明けましておめでとう、友達!

また、 Vimeoチャンネルに登録して、新しいビデオのリリースについて最初に知ることができます。



All Articles