Jump to content
Sign in to follow this  
jeliey

switch pun masalah...tak paham lorr

Recommended Posts

Ape problem ni?aku rasa cam dah btol je...tp ada error:use of unassigned local variable'grading' yg aku bold kan ni...ada sesapa leh tlg x?aku tau dis is so simple, tp aku mmg dh blank sgt2 ni...



using System;
using System.Collections.Generic;
using System.Text;

namespace MarkGrade
{
class MarkGrade
{
static void Main(string[] args)
{
string mark,grading;

Console.Write("Please enter your mark:");
mark=Console.ReadLine();
//double d = Convert.ToDouble(mark);


switch (mark)
{
case "mark >= 80 && mark <= 100":
grading="Distinction";
break;

case "mark >= 70 && mark < 80":
grading = "Very Good";
break;

case "mark >= 60 && mark< 70":
grading = "Good";
break;

case "mark >= 50&& mark < 60":
grading = "OK";
break;

case "mark >= 40 && mark < 50":
grading = "Average";
break;

case "mark<40":
grading = "Poor";
break;
}
Console.WriteLine();
Console.WriteLine(grading);
Console .ReadLine ();
}
}
}

Share this post


Link to post
Share on other sites
@my thought
The compiler throw the error because it suspected that "grading" might not being assigned with any value in the end of the process.
This happen because all the value assigning process only happen in switch clause.

Try to give an initial value during variable ("grading") declare.
example:

string mark;
string grading = "temp";

Share this post


Link to post
Share on other sites
ada beberapa benda:
1)
Code:
case "mark >= 80 && mark <= 100":

"mark >= 80 && mark <= 100" dah jadi string kat situ, bukan conditioanl expression...compiler akan banding nilai string dalam 'mark' dengan string "mark >= 80 && mark <= 100"...semua nilai tidak sama jadi tak ada switch statement yang di execute menyebabkan nilai 'grading' masih unassigned ketika digunakan dalam
Code:
Console.WriteLine(grading);

2) case label mesti constant-expression yang dapat dinilai semasa compile-time...tidak boleh conditional expression...

3) elok ada "default" supaya kalu user tersilap masuk jenis data atau tersilap keluar dari range markah, sesuatu dapat dilakukan sperti memaparkan "invalid input" dan sebagainya..

jadi kena guna "if...else if" untuk kes ni...dan dis-comment balik double d = Convert.ToDouble(mark); untuk menukarkan jenis data 'mark' tu...gunakan variable 'd' tu semasa membuat perbandingan dalam "if..else if" statement...ataupun terus membuat perbandingan dalam if expression tu sambil menukar jenis data 'mark' tanpa menggunakan variable lain...terpulang...

//dah macam robot ayat aku..susah betul nak tulis skema2 ni..haha

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...