| | |
refining a programme
![]() |
•
•
Join Date: Jul 2007
Posts: 57
Reputation:
Solved Threads: 2
okay I have been working on a little project of my own for a while.
At the moment it prints out the chapters read and once 100% of the chapters read is greater than 100 it shows on the next line of my code the number of times the book is read.
Now problem is that when the book is read 4 times it shows as %400. I am sure this can be done with either an if statement or a for loop but I wouldnt know where to start. I also know it involves my 2 variables b and show. So any ideas or if you could point me in the right direction that would be most helpful.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class percentbible extends JFrame implements ActionListener
{
public percentbible()
{
setLayout(new BorderLayout(550,550));
JButton bibone =new JButton ("percentage of bible complete /n calculation ");
add(bibone,BorderLayout.NORTH);
bibone.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String a;
//enter in number for a string
a=JOptionPane.showInputDialog(null,"enter chapters of bible read","",JOptionPane.INFORMATION_MESSAGE);
//turn string into number
long num = Long.parseLong(a);
long chapno = 1189;
double calc= ((double)num/(double)chapno)*100.0;
String show;
//convert integer into string
show = Double.toString(calc);
//display calculation
String b; // string for printing out chapters read
long chapread = num; //the number taken in from the chapters read
long timesbookread = chapread/chapno; //calulation to work out number of times read bible
b = Long.toString(timesbookread); // number goes into the string
JOptionPane.showMessageDialog(null,show+"\n no of times reading whole book"+b,"percentage of chapters read",JOptionPane.INFORMATION_MESSAGE);
}
}
At the moment it prints out the chapters read and once 100% of the chapters read is greater than 100 it shows on the next line of my code the number of times the book is read.
Now problem is that when the book is read 4 times it shows as %400. I am sure this can be done with either an if statement or a for loop but I wouldnt know where to start. I also know it involves my 2 variables b and show. So any ideas or if you could point me in the right direction that would be most helpful.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class percentbible extends JFrame implements ActionListener
{
public percentbible()
{
setLayout(new BorderLayout(550,550));
JButton bibone =new JButton ("percentage of bible complete /n calculation ");
add(bibone,BorderLayout.NORTH);
bibone.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String a;
//enter in number for a string
a=JOptionPane.showInputDialog(null,"enter chapters of bible read","",JOptionPane.INFORMATION_MESSAGE);
//turn string into number
long num = Long.parseLong(a);
long chapno = 1189;
double calc= ((double)num/(double)chapno)*100.0;
String show;
//convert integer into string
show = Double.toString(calc);
//display calculation
String b; // string for printing out chapters read
long chapread = num; //the number taken in from the chapters read
long timesbookread = chapread/chapno; //calulation to work out number of times read bible
b = Long.toString(timesbookread); // number goes into the string
JOptionPane.showMessageDialog(null,show+"\n no of times reading whole book"+b,"percentage of chapters read",JOptionPane.INFORMATION_MESSAGE);
}
}
You just need to use the mod operator on your percentComplete calculation.
Please use code tags when you post code like this, as it will make it much more readable with the formatting preserved. You should also consider making your variable names more clear instead of using abbreviations. It will help your own clarity of the program as well as others who are reading it.
Java Syntax (Toggle Plain Text)
int chaptersRead = 1500; int chaptersInBook = 1189; System.out.println("total percent read = "+(chaptersRead/(float)chaptersInBook*100f)); // with mod operation to reduce to percent complete for the current reading float percentOfBook = (chaptersRead/(float)chaptersInBook*100f) % 100f; System.out.println("percentOfBook = "+percentOfBook); // or a variation on the same calc percentOfBook = (chaptersRead%chaptersInBook) / (float)chaptersInBook * 100f; System.out.println("percentOfBook = "+percentOfBook);
Please use code tags when you post code like this, as it will make it much more readable with the formatting preserved. You should also consider making your variable names more clear instead of using abbreviations. It will help your own clarity of the program as well as others who are reading it.
![]() |
Similar Threads
- point me in the right direction - Exam/quiz etc result calculating programme (C++)
- Developing an Auto Dial up CD Installer Programme on windows plateforms (Visual Basic 4 / 5 / 6)
- Trying To Create a Palindrome Programme (Pascal and Delphi)
- Ending programme p when shutting down on XP (Windows NT / 2000 / XP)
- need help in interfacing my programme (C++)
- help please programme to build (Database Design)
- Help with getting a programme written (Geeks' Lounge)
- Faulty programme!!! (C++)
Other Threads in the Java Forum
| Thread Tools | Search this Thread |
6 @param actuate android api applet application arc array arrays automation balls binary bluetooth bold business byte c++ chat class client code codesnippet collections compare component coordinates database defaultmethod detection doctype dragging ebook eclipse educational error file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework hql html ide ideas image ingres input integer internet intersect invokingapacheantprogrammatically j2me java javaexcel javaprojects jni jpanel jtextarea julia linux list map method methods mobile mysql netbeans newbie nextline parameter php pong problem program programming project recursion recursive scanner sell server set sms sort sql string sun swing swt terminal threads tree web websites windows






