|
Page 1 of 1 |
pancake
Posts: 1091
Location: England
|
Posted: Mon, 6th Feb 2006 23:24 Post subject: java help |
|
 |
Had my first class of java last week at college and our homework was to finish the little program we were writing.
Had some trouble setting up jcreator on my pc and getting the stuff i did at college to compile and run on my machine (was fine at college), to cut a long story short i lost my code .
Its just a real basic program to add together 3 exam grades, find the average then output a grade based on pre defined amounts.
I have got the 3 dialog boxes to appear so i can enter the grades but i cant remeber how to use the Float command to store the inputed grade so that i can then add the 3 together after to get the average.
So far i have :
Code: |
import javax.swing.JOptionPane;
public class Grade
{
public static void main(String[] args) {
float exam1;
float exam2;
float exam3;
String exams1 = JOptionPane.showInputDialog("grade 1");
String exams2 = JOptionPane.showInputDialog("grade 2");
String exams3 = JOptionPane.showInputDialog("grade 3");
Im missing the Float bit that goes here, as far as i remeber it was
Float.parsefloat or something like that
mean =(exams1+exams2+exams3)/3;
Then grading works out from if/else, but not sure how to get the mean from the 3 exams to the final grade
if (mean >= 90) {
grade = 'A';
} else if (mean >= 80) {
grade = 'B';
} else if (mean >= 70) {
grade = 'C';
} else if (mean >= 60) {
grade = 'D';
} else {
grade = 'F';
}
|
Thanks for any help , im tired and getting cranky
|
|
Back to top |
|
 |
[sYn]
[Moderator] Elitist
Posts: 8374
|
Posted: Mon, 6th Feb 2006 23:32 Post subject: |
|
 |
Float isnt a command, its a data type. FLOAT: A floating point number with a decimal place. You should consider using INT which is a standard number.
Either way they are used to create what is known as a variable using said data type, a variable is an area within a program where values can be stored and then called.
You havent declaired the variable "grade" either (that I can see..) this should be next to the other variable declirations:
char grade;
the "parse.float" bit is no doubt the routine to transform your floats into int's for calculation, but its hard to say seen as it could be doing ANYTHING at all.. you should just re-write the code.
|
|
Back to top |
|
 |
pancake
Posts: 1091
Location: England
|
Posted: Mon, 6th Feb 2006 23:47 Post subject: |
|
 |
this is what i have now
Code: |
import javax.swing.JOptionPane;
public class Grade
{
public static void main(String[] args) {
float exam1;
float exam2;
float exam3;
String exams1 = JOptionPane.showInputDialog("grade 1");
String exams2 = JOptionPane.showInputDialog("grade 2");
String exams3 = JOptionPane.showInputDialog("grade 3");
char grade;
{
exam1 = Float.parseFloat(exams1);
exam2 = Float.parseFloat(exams2);
exam3 = Float.parseFloat(exams3);
}
Mean = (exams1 + exams2 + exams3)/3;
|
it all goes fine up until the finding the average of the 3 values
i get 2 errors " cant resolve symbol variable mean"
and " operator / cannot be applied to java.lang.string,int"
|
|
Back to top |
|
 |
|
Posted: Tue, 7th Feb 2006 00:10 Post subject: |
|
 |
you are using the the string variables (exams1...) for the calculation
-> (exam1 + exam2 + exam3)/3
and you have to declare the variable "mean" before you are using it
-> float mean;
(it's a good idea to declare variables at the start of the function/method)
edit:
these {} are unnecessary in that case:
{
exam1 = Float.parseFloat(exams1);
exam2 = Float.parseFloat(exams2);
exam3 = Float.parseFloat(exams3);
}
|
|
Back to top |
|
 |
pancake
Posts: 1091
Location: England
|
Posted: Tue, 7th Feb 2006 00:21 Post subject: |
|
 |
thanks tjuma your the man, its been driving me mad all evening, was only my first class last week so im real sketchy
|
|
Back to top |
|
 |
|
Posted: Tue, 7th Feb 2006 00:38 Post subject: |
|
 |
such errors happen hehe
try to give your variables a more descriptive name,
for example:
exams -> stringExams
exam -> floatExams
this minimizes those reading errors
|
|
Back to top |
|
 |
pancake
Posts: 1091
Location: England
|
Posted: Tue, 7th Feb 2006 01:11 Post subject: |
|
 |
ok ill keep it in mind when i make a new app or something.
|
|
Back to top |
|
 |
|
Posted: Sat, 18th Feb 2006 23:05 Post subject: |
|
 |
tjuma wrote: | such errors happen hehe
try to give your variables a more descriptive name,
for example:
exams -> stringExams
exam -> floatExams
this minimizes those reading errors | I prefer not to use the name of the (primitive/reference) type in the name of the variable or reference. If for some reason you decide to change the type (from float to double for instance) you have to rename all other stuff too. And words expressing multiplicity (eg. exams, cars, books) should be reserved for collections or arrays. For single items use the single (can't come up with the correct English word) word (eg. exam, car, book).
|
|
Back to top |
|
 |
pancake
Posts: 1091
Location: England
|
Posted: Thu, 2nd Mar 2006 18:49 Post subject: |
|
 |
On to my next little app now which outputs a times table from 1 to 12 and how many multiplications you input from 0 to 12 , got everything running ok, but as i have it the final multiplications are output one at a time into a dialog box, so i have to hit enter every time to get it to show the next one.
How can i alter it to get it to show me the whole output in one dialog box, i got it showing in the command propmt by using system.out.println, but want to know how to get it in a message dialog.
Code: |
{
public static void main(String[] args)
{
int num1;
int num2;
int count;
int run;
do
{do
{
String snum1 = JOptionPane.showInputDialog(null, " Enter Number from 1 to 12 "," Enter Number from 1 to 12 ", JOptionPane.INFORMATION_MESSAGE );
num1 = Integer.parseInt(snum1);
if (num1 < 1 || num1 > 12)
{
JOptionPane.showMessageDialog(null, "Please enter a number from 1 to 12 ", "Please enter a number from 1 to 12 ", JOptionPane.ERROR_MESSAGE);
}
}while (num1 < 1 || num1 > 12);
do
{
String snum2 = JOptionPane.showInputDialog(null, " Enter amount of times to calculate ", " Enter amount of times to calculate ", JOptionPane.INFORMATION_MESSAGE);
num2 = Integer.parseInt(snum2);
if (num2 < 0 || num2 > 12)
{
JOptionPane.showMessageDialog(null,"Please enter a number from 0 to 12 ", "Please enter a number from 0 to 12 ", JOptionPane.ERROR_MESSAGE );
}
}while (num2 < 0 || num2 > 12);
for (count=1; count <= num2; count++)
{
JOptionPane.showMessageDialog(null, " The times table is \n" +count+ "x" + num1 + "=" +count * num2);
}
String srun = JOptionPane.showInputDialog(null, "Would you like to run again? press 1", "Would you like to run again? press 1", JOptionPane.QUESTION_MESSAGE );
run = Integer.parseInt(srun);
}while (run==1);
}
}
|
|
|
Back to top |
|
 |
|
Posted: Wed, 8th Mar 2006 11:49 Post subject: |
|
 |
Code: |
{
public static void main(String[] args)
{
int num1;
int num2;
int count;
int run;
do
{do
{
String snum1 = JOptionPane.showInputDialog(null, " Enter Number from 1 to 12 "," Enter Number from 1 to 12 ", JOptionPane.INFORMATION_MESSAGE );
num1 = Integer.parseInt(snum1);
if (num1 < 1 || num1 > 12)
{
JOptionPane.showMessageDialog(null, "Please enter a number from 1 to 12 ", "Please enter a number from 1 to 12 ", JOptionPane.ERROR_MESSAGE);
}
}while (num1 < 1 || num1 > 12);
do
{
String snum2 = JOptionPane.showInputDialog(null, " Enter amount of times to calculate ", " Enter amount of times to calculate ", JOptionPane.INFORMATION_MESSAGE);
num2 = Integer.parseInt(snum2);
if (num2 < 0 || num2 > 12)
{
JOptionPane.showMessageDialog(null,"Please enter a number from 0 to 12 ", "Please enter a number from 0 to 12 ", JOptionPane.ERROR_MESSAGE );
}
}while (num2 < 0 || num2 > 12);
String output = "The times table is \n";
for (count=1; count <= num2; count++)
{
output += count + "x" + num1 + "=" + count * num2 + "\n";
}
JOptionPane.showMessageDialog(null, output);
String srun = JOptionPane.showInputDialog(null, "Would you like to run again? press 1", "Would you like to run again? press 1", JOptionPane.QUESTION_MESSAGE );
run = Integer.parseInt(srun);
}while (run==1);
}
}
|
enjoy 
|
|
Back to top |
|
 |
pancake
Posts: 1091
Location: England
|
Posted: Thu, 16th Mar 2006 11:18 Post subject: |
|
 |
Thanks, completely forgot i posted this .
on the bottom part of my code were its set to accept a 1 to run the program again or terminate how do i get around it throwing out a error if a letter is put in??
Does anyone have a link for somewere that i could learn this from??
Code: |
String srun = JOptionPane.showInputDialog(null, "Would you like to run again? press 1", "Would you like to run again? press 1", JOptionPane.QUESTION_MESSAGE );
run = Integer.parseInt(srun);
}while (run==1);
|
edit:
Ok i have found the JOptionPane.showConfirmDialog which pops up a box with a yes and a no button, now how do i make it loop if yes is chose or quit if no is chosen, because as it is they dont do a damn thing!!
Code: |
JOptionPane.showConfirmDialog(null, "Click yes to run again", "Would you like to run again?",JOptionPane.YES_NO_OPTION );
|
Thats all i have for it so far!!
|
|
Back to top |
|
 |
|
Posted: Sat, 29th Apr 2006 18:36 Post subject: |
|
 |
The showConfirmDialog method returns an int with the user's choice. Whenever you need info on things like this, check out the Sun's API documentation. All classes and their methods are described there.
|
|
Back to top |
|
 |
Page 1 of 1 |
All times are GMT + 1 Hour |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group
|
|
 |
|