I have two questions regarding C# -
in submitbutton_click method, i'm trying add code increase score 20 points every correct answer. reason not adding 20 points each time answer question correctly says 0. other question when 2 random variables change when click new , type in right answer marks incorrect. messing somewhere regarding these 2 problems?
here code
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; namespace lab4 { public partial class lab4 : form { public lab4() { initializecomponent(); score = 0; } private int score = 0; private int product = 0; private int answer = 0; private void submitbutton_click(object sender, eventargs e) { try { answer = int.parse(answertextbox.text); if(convert.toint32(answertextbox.text)==product) { score=score+20; messagebox.show("correct!"); } else { messagebox.show("sorry, answer incorrect!"); } } catch { messagebox.show("there error processing answer!"); } } private void newbutton_click(object sender, eventargs e) { random rnd = new random(); int i, var1, var2, product; var1 = rnd.next(1,9); var2 = rnd.next(1,9); promptlabel.text = ("what " + var1 +" . " + var2 + "?"); product = var1 * var2; (i = 10; >= 1; i--) { messagebox.show("you have " + + " sec(s) left!"); timelabel.text = ("time: " + + " sec(s)"); } } private void scorelabel_click(object sender, eventargs e) { } private void answertextbox_textchanged(object sender, eventargs e) { } } }
first problem have declared local variable inside method same name field in form class. both named product
. local variable hiding field , therefore no value assigned product
in class. have remove local 1 or have use this
keyword tell compiler use field inside class.
you dont have declare product
here because dont need it.
int i, var1, var2; //product; dont declare local variable. instead use field available var1 = rnd.next(1,9); var2 = rnd.next(1,9); promptlabel.text = ("what " + var1 +" . " + var2 + "?"); product = var1 * var2; // this.product = var1 * var2;
another problem see timer. timer inside loop finishes instantly. because dont wait second countdown timer.
for (i = 10; >= 1; i--) { messagebox.show("you have " + + " sec(s) left!"); timelabel.text = ("time: " + + " sec(s)"); await task.delay(1000); // wait 1 second }
for have change signature of method async.
private async void newbutton_click(object sender, eventargs e)
this maybe considered poor design count down timer. highly suggest take @ here know how correctly create count down timer.
Comments
Post a Comment