How to show 1 label box at a time - forms

I sm very new to C#~ as you will soon see. I am trying to make a for that allows me to enter a gpa and a test score. I have two label boxes, one says you are accepected if you meet the criteria and the other says rejected. How can I make just one label box show up when it is called? This is what I have done so far.
private void button1_Click(object sender, EventArgs e)
{
const double lowestGPA = 3.0;
const int lowestTest = 60;
const int highestTest = 80;
double gpa;
double test;
test = Convert.ToDouble(textBox2.Text);
gpa = Convert.ToDouble(textBox1.Text);
label3.Visible = true;
if ((gpa > lowestGPA) && (test > lowestTest))
label3.Text = "Accepted!";
else
label4.Text = "Rejected!";
if ((gpa < lowestGPA) && (test > highestTest))
label3.Text = ("Accepected!");
else
label4.Text = "Rejected!";
}

im not sure that I understand but I would do it like this
if (((gpa > lowestGPA) && (test > lowestTest)) || ((gpa < lowestGPA) && (test > highestTest)))
label3.Text = "Accepted!";
else
label3.Text = "Rejected!";

Related

multiply or dividing doesn't works correctly when the second number is negative

im trying to write windows 7s calculator but i have problems just in multiply and divide. here im writing the codes that are connected to multiply so you can get the reason.
double input1;
double input2;
double result;
string amalgar;
amalgar means + or - or * or /
private void button14_Click(object sender, EventArgs e)
{
input1 = Convert.ToDouble(textBox1.Text);
textBox1.Clear();
amalgar = "*";
}
it was for * button.
this is for negativation button :
private void button20_Click(object sender, EventArgs e)
{
input1 = Convert.ToDouble(textBox1.Text);
input1 = input1 * (-1);
textBox1.Text = input1.ToString();
}
and this is for equal button:
input2 = Convert.ToDouble(textBox1.Text);
if (amalgar == "*")
{
result = (input1 * input2);
textBox1.Text = Convert.ToString(result);
}
here is some examples for results:
2*6=12 Right
2*(-2)=4 Wrong
(-2)*2=-4 R
4*(-5)=25 W
8*(-7)=49 W
3*(-6)=36 W
8/2=4 R
8/(-2)=1 W
8/(-3)=1 W
It's because when you hit the negative button, you overwrite what you had in input1 with the negative of the contents of the textbox.
private void button20_Click(object sender, EventArgs e)
{
input1 = Convert.ToDouble(textBox1.Text); // These lines overwrite
input1 = input1 * (-1); // anything in input1
textBox1.Text = input1.ToString();
}
So that when you go to the equals code, input 2 and input 1 are always the same number if the last thing you pressed was the negative button.
input2 = Convert.ToDouble(textBox1.Text); // this equals input1 if the last thing
// you pressed was the negative button
if (amalgar == "*")
{ // ....
In button20_Click you need to modify the contents of textBox1 without overwriting input1. Something you could try is using a local variable to do all your calculations on:
double modifiedInput = Convert.ToDouble(textBox1.Text);
modifiedInput = modifiedInput * (-1);
textBox1.Text = modifiedInput.ToString();
i have solved it .it was an easy mistake.
the problem was in negativation button with i tried to multiply input1 by -1.
i have changed the code to :
input3 = Convert.ToDouble(textBox1.Text);
qarine = input3 * (-1);
textBox1.Text = qarine.ToString();
in that button and some clauses in equal button :
else if (amalgar == "*")
{
if (input1 > 0 && input2 > 0)
{
result = (input1 * input2);
}
else if (input1 < 0 && input2 < 0)
{
result = (input1 * input2);
}
else if (input1 < 0 && input2 > 0)
{
result = (qarine * input2);
}
else if (input1 > 0 && input2 < 0)
{
result = (input1 * qarine);
}
textBox1.Text = Convert.ToString(result);
}

Two instances of class are getting mixed up?

I am creating a sort of game with Arduino and Processing. In my code, I use Daniel Shiffman's class Timer, but would like to create two different Timers using two different instances of the class.
My problem is that these two instances seem to be getting mixed up, with each one doing parts of what the other should be doing.
For example, timer should run for 10 seconds and correctTimer should run for 3 seconds, but they both run for 10 seconds. Additionally, when timer is finished, it should set the background to red and when correctTimer is finished, it should set the background to blue. However, both Timers set the background to blue when they are finished.
Does anyone have any ideas of how to fix this?
import processing.serial.*;
int end = 10;
String serial;
Serial port;
float[] array;
// --------------------------------------------------
PImage img;
PImage correct;
PImage incorrect;
float thumb;
float index;
float middle;
float ring;
float pinky;
// --------------------------------------------------
String alphabet;
int randomNum;
String letter;
// --------------------------------------------------
int savedTime;
int totalTime;
int passedTime;
boolean quit = false;
class Timer {
Timer(int tempTotalTime) {
totalTime = tempTotalTime;
}
void start() {
savedTime = millis();
//quit = false;
}
boolean isFinished() {
passedTime = millis() - savedTime;
if (passedTime > totalTime) {
return true;
} else {
return false;
}
}
}
Timer timer;
Timer correctTimer;
// --------------------------------------------------
boolean checkLetter(String letterPicked, float flexR_THUMB, float flexR_INDEX, float flexR_MIDDLE, float flexR_RING, float flexR_PINKY) {
if (letterPicked == "A") {
if (flexR_THUMB > 12000 && flexR_THUMB < 22000 &&
flexR_INDEX > 27958 && flexR_INDEX < 38500 &&
flexR_MIDDLE > 26035 && flexR_MIDDLE < 41650 &&
flexR_RING > 16492 && flexR_RING < 26000 &&
flexR_PINKY > 37528 && flexR_PINKY < 53500) {
return true;
} else {
return false;
}
}
return false; }
// --------------------------------------------------
void setup() {
size(1280, 950);
background(255);
port = new Serial(this, "/dev/tty.usbmodem1421", 9600);
port.clear();
serial = port.readStringUntil(end);
serial = null;
correct = loadImage("img/RIGHT.png");
incorrect = loadImage("img/WRONG.png");
correctTimer = new Timer(3000);
startOver();
}
// --------------------------------------------------
void startOver() {
background(255);
letter = "A";
img = loadImage("img/" + letter +".png");
image(img, 0, 0, 1280, 950);
timer = new Timer(10000);
timer.start();
}
// --------------------------------------------------
void draw() {
while(port.available() > 0) {
serial = port.readStringUntil(end);
}
if (serial != null) {
float[] array = float(split(serial, ','));
thumb = array[0];
index = array[1];
middle = array[2];
ring = array[3];
pinky = array[4];
}
if (checkLetter(letter, thumb, index, middle, ring, pinky) == true && quit == false) {
image(correct, 0, 0, 1280, 950);
quit = true;
correctTimer.start();
} else if (timer.isFinished() && quit == false) {
background(255, 0, 0);
quit = true;
correctTimer.start();
}
if (correctTimer.isFinished()) {
background(0, 0, 255);
}
}
Please try to post a MCVE instead of your whole project. Just put together a small example that demonstrates the problem. That makes it much easier for us to help you.
But your problem is caused by your savedTime, totalTime, and passedTime variables being outside the Timer class. Basically that means they're shared between all instances of the Timer class. You can use println() statements to confirm this.
To fix your problem, just move those variables inside the Timer class, so each instance has its own copy of them.
If you're still having trouble, please post a MCVE in a new question post, and we'll go from there. Good luck.

Object does not return to original position when released cs4

I am making a drag and drop activity where multiple items are to be placed at the same target. I have action code on the main time line and then code attached to a sprite.
Problems:
When I release the object if it is correct it should snap to the top of that section. If it is incorrect it should return to its original orientation to be picked up again. Unfortunately it stays where I placed it, and does not move back to original place when it is incorrect. I have fixed other issues with this flash file as to if code is supposed to be on the main time line or in the sprite, but this does not seem to have an effect after the object is released. Near the end of the script there is _root.rlamp1.gotoAndPlay(1) this is a buzzer that goes off if the answer is incorrect. This is working correctly.
I do not know if these are related Items but if the correct placement is given I want the next item to start in the original position, but it is starting where my mouse is instead of the original position. I am relatively new to coding and am trying to make an activity for my science class to get instant feedback to see if they understand to concept.
Thanks for your help.
// [Action in Frame 1]
answername = Array();
answerdest = Array();
answername[0] = "gravel";
answerdest[0] = "1";
answername[1] = "nuts and bolts";
answerdest[1] = "1";
answername[2] = "oxygen";
answerdest[2] = "2";
answername[3] = "helium";
answerdest[3] = "2";
answername[4] = "gold";
answerdest[4] = "2";
dbCount = 0;
dbutton.duplicateMovieClip("dbutton" + dbCount,dbCount * 100);
dbutton.visible = false;
dbutton0.answer = answerdest[dbCount];
dbutton0.theText.text = answername[dbCount];
// This code is on the sprite and not on the main actionscript
onClipEvent (load)
{
this.defx = _x;
this.defy = _y;
if (this.theText.text.length > 20)
{
this.theText._height = 31;
this.theBox._height = 27;
}
else
{
this.theText._height = 19;
this.theBox._height = 19;
} // end else if
}
on (press)
{
if (this.noDrag !=true)
{
startDrag (this,false);
}
}
on (release)
{
if (this.noDrag != true)
{
stopDrag ();
if(this.hitTest(_root["dz" + this.answer]))
{
totalHeight = 0;
for (v = 0; v < _root.dbCount; v++)
{
if (_root["dbutton" + v].answer == this.answer)
{
totalHeight = totalHeight + _root["button" + v].theBox._height ;
} // end if
} // end of for
++_root.dbCount;
this .duplicateMovieClip("dbutton" + _root.dbCount, _root.dbCount * 100);
_root["dbutton" + _root.dbCount]._x = this.defX;
_root["dbutton" + _root.dbCount]._y = this.defY;
_root["dbutton" + _root.dbCount].answer = _root.answerdest[_root.dbCount + 1];
_root["dbutton" + _root.dbCount].theText.text = _root.answername[_root.dbCount +1];
if (_root["dbutton" + _root.dbCount].theText.text == "undefined")
{
_root["dbutton" + _root.dbCount].theText.text = "Finished!";
_root["dbutton" + _root.dbCount].noDrag = true;
} // end if
this.noDrag = true;
this._y = _root["dz" + this.answer]._y + totalHeight;
this._x = _root["dz" + this.answer]._x - _root["dz" + this.answer]._width / 2;
++_root["dz" + this.answer].numItems;
_root.glamp1.gotoAndPlay (1);
}
else
{
this.X = this.defX;
this._Y = this.defY;
_root.rlamp1.gotoAndPlay(1);
} // end if
} // end else if
}
The problem is that there are misspelled variable names and properties in the sprite code.
Replace these lines
this.X = this.defX;
this._Y = this.defY;
with those lines
this._x = this.defx;
this._y = this.defy;
and your code should work.

Cannot find symbol error with scanner

I'm trying to make this program run, but I have one last error that I can't fix. I get an error message when trying to get user input in my lasTal() function. I am surprised about this error because the exact same line of code has worked for me in my other program.
import java.util.Scanner;
public class Nastaord{
public static int bFinal, cFinal;
public static int[] tallistaFinal;
private static int[] lasTal(){
int[] tallista; //Det vi ska ha talföljden i
int i = 0; //räknare för tallista
while(true){
System.out.print("Ange tal, eller tryck enter om du är klar: ");
String input = scanner.nextLine();
int nytt_tal = Integer.parseInt(input);
if(input == ""){
return tallista;}
tallista[i] = nytt_tal;
i++;
}
}
private static boolean bcFinns(int[] tallista){
boolean OK = true;
for(int b = -9; b <= 9; b++){
for(int c = -9; c <= 9; c++){
for(int i = tallista.length; i > 0;i--){
OK = tallista[i] == tallista[i-1]*b+c;
if(OK == false){
break;}
}
if(OK == true){
bFinal = b;
cFinal = c;
tallistaFinal = tallista;
return true;}
}
}
return false;
}
public static void main(String[] args){
boolean OK = bcFinns(lasTal());
if (OK == true){
System.out.print(tallistaFinal[tallistaFinal.length-1]*bFinal+cFinal);
}
if (OK == false){
System.out.print("No");
}
}
}
This is my error message:
Nastaord.java:13: error: cannot find symbol
String input = scanner.nextLine();
^
symbol: variable scanner
location: class Nastaord
Thanks
you did not creat a Scanner Object. Use this in lasTal() method at starting to take input from keyboard and u need to initialize "tallista " array.
Scanner scanner = new Scanner(System.in);

Error for Heap Space Cap

my code is for some reason giving me an out of memory error. I have tried increasing the amount of memory allocated to ecplise but it seems to do nothing. This is my code that the error is coming from:
public static ArrayList<Integer> getSortedEndPoints(ArrayList<Interval>leftSortedIntervals, ArrayList<Interval> rightSortedIntervals) {
int left;
int size = leftSortedIntervals.size();
Interval interval;
ArrayList <Integer> sortedEndPoints = new ArrayList<Integer>();
for (int i = 0; i < size; i++){
interval = leftSortedIntervals.get(i);
left = interval.leftEndPoint;
//System.out.println("Left Endpoint at " + i + "is : " + left);
if (sortedEndPoints.size() == 0){
//System.out.println("Entered size 0");
sortedEndPoints.add(left);
} else if (searchForDuplicate(sortedEndPoints, left) == false){
//System.out.println("Search For Duplicates is False");
sortedEndPoints.add(left);
}
}
System.out.println("Sorted End Points after left search : " + sortedEndPoints);
int right;
size = rightSortedIntervals.size();
for (int i = 0; i < size; i++){
interval = rightSortedIntervals.get(i);
right = interval.rightEndPoint;
if (sortedEndPoints.size() == 0){
sortedEndPoints.add(right);
} else if (searchForDuplicate(sortedEndPoints, right) == false){
System.out.println("Search For Duplicates is False");
size = sortedEndPoints.size()-1;
System.out.println("Size= " + size);
System.out.println("sortedEndPoints.get(size) " + sortedEndPoints.get(size));
if (right > sortedEndPoints.get(size)){
sortedEndPoints.add(right);
} else {
for (int t = 0; t < sortedEndPoints.size(); t++){
if (right < sortedEndPoints.get(t)){
sortedEndPoints.add(t, right);
}
}
}
}
}
System.out.println("Sorted End Points after right search : " + sortedEndPoints);
return sortedEndPoints;
}
private static boolean searchForDuplicate(ArrayList<Integer>sortedEndPoints, int left){
for (int track = 0; track < sortedEndPoints.size(); track++){
if (left == sortedEndPoints.get(track)){
return true;
}
}
return false;
}
and this is the error I am getting
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2760)
at java.util.Arrays.copyOf(Arrays.java:2734)
at java.util.ArrayList.ensureCapacity(ArrayList.java:167)
at java.util.ArrayList.add(ArrayList.java:370)
at structures.Sorter.getSortedEndPoints(Sorter.java:148)
at structures.IntervalTree.<init>(IntervalTree.java:39)
at apps.IntervalTreeDriver.main(IntervalTreeDriver.java:19)
I have no idea what to do now...
Values in the eclipse.ini change the memory that Eclipse uses itself, not what it uses when you run a program.
To change what your program uses when you run it open the 'Run > Run Configurations' dialog. Find your program in the 'Java Programs' section. On the 'Arguments' tab add your memory arguments in the 'VM arguments' section.