So I have to make program that displays the max/min of a set of 5 user-entered numbers when the corresponding button is pressed.
Seems simple enough but I have very little experience in Java. Would probably be done hours ago if this were javascript.
Here's what I have so far:
This works in the sense that it will run without errors, and when you enter the numbers and press the buttons it gives you a number back. But it only seems to take the max out of the first two numbers....
Not sure if anyone here knows Java but maybe I just did something stupid that I can't seem to recognize.
I've also used this alternate method which seems to give me more random results:
So if anyone wants to lend me a hand here that would be awesome.
Seems simple enough but I have very little experience in Java. Would probably be done hours ago if this were javascript.
Here's what I have so far:
- Spoiler:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.Math.*;
public class finalpart3 extends Applet implements ActionListener
{
TextField statusField1;
TextField statusField2;
TextField statusField3;
TextField statusField4;
TextField statusField5;
TextField resultField;
Button maxButton;
Button minButton;
String rst;
Double v1, v2, v3, v4, v5,v6,v7,v8,v9,v10;
double maxValue= Double.MAX_VALUE;
double minValue= Double.MIN_VALUE;
public void init()
{
setLayout(new FlowLayout());
maxButton= new Button("MAX");
minButton= new Button("MIN");
statusField1= new TextField("Type here");
maxButton.addActionListener(this);
minButton.addActionListener(this);
statusField1.addActionListener(this);
statusField2= new TextField("Type here");
statusField2.addActionListener(this);
statusField3= new TextField("Type here");
statusField3.addActionListener(this);
statusField4= new TextField("Type here");
statusField4.addActionListener(this);
statusField5= new TextField("Type here");
statusField5.addActionListener(this);
resultField = new TextField("Result displayed here");
resultField.setEditable(false);
add(maxButton);
add(minButton);
add(statusField1);
add(statusField2);
add(statusField3);
add(statusField4);
add(statusField5);
add(resultField);
}
public void actionPerformed(ActionEvent act){
v1=Double.parseDouble(statusField1.getText());
v2=Double.parseDouble(statusField2.getText());
v3=Double.parseDouble(statusField3.getText());
v4=Double.parseDouble(statusField4.getText());
v5=Double.parseDouble(statusField5.getText());
if(act.getSource()==maxButton){
v6=Math.max(v1,v2);
v7=Math.max(v6,v3);
v8=Math.max(v7,v4);
v9=Math.max(v8,v5);
resultField.setText(Double.toString(v9));
}
else if(act.getSource()==minButton)
v6=Math.min(v1,v2);
v7=Math.min(v6,v3);
v8=Math.min(v7,v4);
v9=Math.min(v8,v5);
resultField.setText(Double.toString(v9));
}
}
This works in the sense that it will run without errors, and when you enter the numbers and press the buttons it gives you a number back. But it only seems to take the max out of the first two numbers....
Not sure if anyone here knows Java but maybe I just did something stupid that I can't seem to recognize.
I've also used this alternate method which seems to give me more random results:
- Spoiler:
if (v1>=v2&&v1>=v3&&v1>=v4&&v1>=v5){
resultField.setText(Double.toString(v1));
}
else if(v2>=v1&&v2>=v3&&v2>=v4&&v2>=v5){
resultField.setText(Double.toString(v2));
}
else if(v3>=v1&&v3>=v2&&v3>=v4&&v3>=v5){
resultField.setText(Double.toString(v3));
}
else if(v4>=v1&&v4>=v2&&v4>=v3&&v4>=v5){
resultField.setText(Double.toString(v4));
}
else{
resultField.setText(Double.toString(v5));
}
So if anyone wants to lend me a hand here that would be awesome.