program is suppose to ask for an input from 1-8, if the user enters a number outside of this range than it will continue to ask until it does, something is wrong and i cant seem to figure it out, i am stuck. Also my if else to see if the user catches the wave seems right to me but only works like half the time. I just really want to know what i am doing wrong here. -
import javax.swing.*; // gets option pane
-
import java.text.DecimalFormat; //formats decimals
-
-
public class Lab8 {
-
-
public static final int G = 32; //a constant
-
public static double waveSpeed; //a class double variable
-
public static String userInput;
-
// method that generates a random wave
-
public static double randomWaveSpeed () {
-
int depth = randomNumber(1, 20);
-
int length = randomNumber(1, 15);
-
waveSpeed = depth / length;
-
-
// determining which wave is selected
-
if (depth >= .5){
-
-
return deepWaveSpeed(length);
-
}
-
else
-
-
return shallowWaveSpeed(depth);
-
}
-
// method that generates a random number
-
public static int randomNumber(int low , int hi ){
-
-
return (low + (int)(Math.random() * hi));
-
}
-
-
// method that determines the speed of a wave in shallow water
-
public static double shallowWaveSpeed(int depth ){
-
-
return Math.sqrt((G*depth));
-
}
-
// method that determines the speed of a wave in deep water
-
public static double deepWaveSpeed(int length ){
-
-
return Math.sqrt((G*length)/(2*Math.PI));
-
}
-
-
// method that displays whether or not the user caught the wave from
-
// the paddle speed entered
-
public static void displayRideOutcome(double paddleSpeed ){
-
String output;
-
-
if(paddleSpeed > waveSpeed){
-
-
DecimalFormat twoDigits = new DecimalFormat("0.00");
-
-
// generating output
-
output = "Wave speed = " + twoDigits.format(randomWaveSpeed()) + "(ft/sec)" + "\n";
-
output += "Paddle speed = " + twoDigits.format(paddleSpeed) + "(ft/sec)" + "\n";
-
output += "You were able to catch the wave.";
-
}
-
-
else{
-
-
DecimalFormat twoDigits = new DecimalFormat("0.00");
-
-
// generating output
-
output = "Wave speed = " + twoDigits.format(randomWaveSpeed()) + "(ft/sec)" + "\n";
-
output += "Paddle speed = " + twoDigits.format(paddleSpeed) + "(ft/sec)" + "\n";
-
output += "You were not able to catch the wave.";
-
}
-
-
//outputing with JOptionPane
-
JOptionPane.showMessageDialog(null, output, "Lab8 (Edward Powers)", JOptionPane.PLAIN_MESSAGE);
-
}
-
-
// main method
-
public static void main (String[] args) {
-
String userInput;
-
double paddleSpeed;
-
int option;
-
-
// a do while loop til the user clicks no
-
do {
-
do{
-
userInput = JOptionPane.showInputDialog("What is your paddle speed (ft/sec)?" + "\n" +
-
"Valid Range is (1.0 to 8.0)");
-
-
// if statement to cancel prog with error message if the user click cancel
-
if (userInput == null)
-
{
-
JOptionPane.showMessageDialog(null, "User clicked cancel. The program will exit.",
-
"Lab8 (Edward Powers)", JOptionPane.ERROR_MESSAGE );
-
System.exit(0);
-
}
-
-
paddleSpeed = Double.parseDouble(userInput);
-
if (paddleSpeed < 1 | paddleSpeed > 8){
-
userInput = JOptionPane.showInputDialog("What is your paddle speed (ft/sec)?" + "\n" +
-
"Valid Range is (1.0 to 8.0)");}
-
else{
-
displayRideOutcome ( paddleSpeed);
-
}
-
option = JOptionPane.showConfirmDialog(null, "Would you like to continue?", "Lab7 (Edward Powers)", JOptionPane.YES_NO_OPTION );
-
-
}
-
while (option == JOptionPane.YES_OPTION);
-
-
}
-
while ( paddleSpeed >= 1 | paddleSpeed <=8);
-
}
-
}
-
-
1 1957
I'm no expert, but I have a working way to use if/else logic and have it reject numbers that are out of the range. - String^ aString = System::Convert::ToString(textBox1->Text);
-
float a = System::Convert::ToSingle(textBox1->Text);
-
-
if (aString->Length == 0)
-
{
-
MessageBox::Show(" Unable to calculate");
-
}
-
-
else if (a > 8)
-
{
-
MessageBox::Show("Unable to calculate");
-
}
-
else
-
{
-
MessageBox::Show("Good answer!");
-
}
Make sure that you use the brackets exactly the way i did, or it won't work right.
If there is only one line of code after the if/else, then you don't need brackets.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Sharif Tanvir Karim |
last post by:
Can anyone send me a php.ini file that is used by almost by all hosts? My
damn computer's (winxp) mini server is messed up and I am starting over so I
am trying ot get php to behave on my system. SO please, if you can, please
send me the php.ini file used by most hosts.
--
Sharif Tanvir Karim
http://www.onlyonxbox.net
|
by: Marc S. Gibian |
last post by:
I am putting together a Perl tool to manage a large set of tasks
related to building a rather complex set of software. One of the goals
for moving to Perl is to provide very rigorous error checking and
handling. One area that has me puzzled is error checking some of the
file system operations. Just simple things such as open, rename,
unlink, print and some others ... all the sample code I've seen uses
them in a form of:
open(...) || die...
|
by: Steven Scaife |
last post by:
Below is my ASP page, I have changed the update to read DRIAL which doesn't
exist, shouldnt this throw an error, or if the connection cannot be made
shouldnt it throw an error as well
thanks in advance
<%@language="VBScript"%>
<% option explicit %>
<% response.buffer = True %>
|
by: Christopher Benson-Manica |
last post by:
All right, since my previous idea (calling functions through a
wrapper) was apparently so awful no one could suggest any
improvements, I'll try a different tack. My end goal is to make
detecting and reporting script errors easy and painless. This could
be accomplished easily if window.onerror were ubiquitous, but it is
not. My new idea is to hack together something superficially
resembling C's assert() macro, perhaps something like
...
|
by: eric |
last post by:
Hello,
Does anyone know how to turn off the constant error checking that
VB.NET performs in the background while code is being edited? I am
working on a migration project and VS finds thousands of errors in my
solution. It slows down my PC so badly that it is hard to get anything
done at all. I have a 2.8GHz P4 with 2gig of RAM.
I have searched everywhere I can think, including this newsgroup and
haven't found anything yet.
| |
by: lawrence k |
last post by:
I've made it habit to check all returns in my code, and usually, on
most projects, I'll have an error function that reports error messages
to some central location. I recently worked on a project where someone
suggested to me I was spending too much time writing error messages,
and that I was therefore missing the benefit of using a scripting
language. The idea, apparently, is that the PHP interpreter writes all
the error messages that are...
|
by: Bryan Crouse |
last post by:
I am looking a way to do error checking on a string at compile time,
and if the string isn't the correct length have then have the compiler
throw an error.
I am working an embedded software that will require individual builds
for each device so that the device serial number is contained in the
program memory. To do this, the C application must be compiled with
the serial number assigned to a variable within the source code file.
I...
|
by: KA NMC |
last post by:
I have One Datagrid with a dataset that calls two tables - the First table is the table the user will be editing - which edited on the grid or in a textbox - as the user edits the grid - that info is automatically updated onto the database - My problem is - when user goes to the nested table the application is still looking for parameters to update the database - which is causing errors - how will I prevent this from happening on the second...
|
by: liam sheerin |
last post by:
Hi
i am fairly new to vba and have a taxi booking database.
every time you book a job the cust_name field must be filled out and same if account job for account_ref and account_password fields but everytime the message box appears and you select ok the form closes
does any body no of a solution the code for the ok button is:
Sub ClickOKButton()
Dim rsJobs As DAO.Recordset
Dim rsCust As DAO.Recordset
Dim NewCustId As Long
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |