![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 |
|
Programmer
Join Date: Jun 2005
Posts: 34
Rep Power: 0
![]() |
OMFG... it works it works it WORKS. It does EXACTLY what it's supposed to do! Except for one thing
![]() Consider the following code: System.out.println("Next, please enter either \"C\" or \"c\" for a");
System.out.println("Celsius-Fahrenheit conversion, or \"F\" or \"f\"");
System.out.println("for a Fahrenheit-Celsius conversion:");
System.out.println();
// typeOfConversion = (char)typeOfConversion;
typeOfConversion = keyboard.next();
switch (typeOfConversion.charAt(0))
{
case 'C':
case 'c':
result = 5.0 * ((double)degreeValue - 32.0) / 9.0;
System.out.println();
System.out.println(degreeValue + " Fahrenheit is " + result + " Celsius");
break;
case 'F':
case 'f':
result = (9.0 * ((double)degreeValue) / 5.0) + 32.0;
System.out.println();
System.out.println(degreeValue + " Celsius is " + result + " Fahrenheit");
break;
default:
System.out.println();
System.out.println("Oops! Your entry is invalid. Please enter either");
System.out.println("\"F\" or \"C\". The letter case does not matter.");
break;
}After the default OOPS printout... which occurs when the user enters anything but F, f, C, or c... the whole program ends and starts over. Of course, because it's got a break statement there. But how could I loop it in such a way that the user only has to continue from this point of invalid entry? In other words, how do I let the user RETRY/REENTER the correct value, rather than having the whole program end right there and start from the beginning? Thank you |
|
|
|
|
|
#22 |
|
Newbie
Join Date: Jun 2005
Posts: 28
Rep Power: 0
![]() |
*** Messed up a bit. Can't fix just yet. Have to run. Fix later ***
Place it in a while loop. Insert the following:
System.out.println("Next, please enter either \"C\" or \"c\" for a");
System.out.println("Celsius-Fahrenheit conversion, or \"F\" or \"f\"");
System.out.println("for a Fahrenheit-Celsius conversion:");
System.out.println();
// typeOfConversion = (char)typeOfConversion;
typeOfConversion = keyboard.next();
while(typeOfConversion.charAt(0).equalsIgnoreCase(c) || typeOfConversion.charAt(0).equalsIgnoreCase(f))
{
switch (typeOfConversion.charAt(0))
{
case 'C':
case 'c':
result = 5.0 * ((double)degreeValue - 32.0) / 9.0;
System.out.println();
System.out.println(degreeValue + " Fahrenheit is " + result + " Celsius");
break;
case 'F':
case 'f':
result = (9.0 * ((double)degreeValue) / 5.0) + 32.0;
System.out.println();
System.out.println(degreeValue + " Celsius is " + result + " Fahrenheit");
break;
default:
System.out.println();
System.out.println("Oops! Your entry is invalid. Please enter either");
System.out.println("\"F\" or \"C\". The letter case does not matter.");
break;
}
}That should work |
|
|
|
|
|
#23 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
There's irc://irc.freenode.net/#programmingforums - 'tis great.
![]() |
|
|
|
|
|
#24 |
|
Programmer
Join Date: Jun 2005
Posts: 34
Rep Power: 0
![]() |
Holy crap... I did it, boys! I DID IT. The program is 99.99% complete. All I have to do is maybe add a comment or two and edit out the other comments.
It performs well ![]() Here it is, my first kick-ass program!!
import java.util.*;
public class TempConversion
{
public static void main(String[] args)
{
int degreeValue;
String typeOfConversion;
double result;
String answer;
Scanner keyboard = new Scanner(System.in);
do
{
System.out.println("=====TEMPERATURE CONVERSION=====");
System.out.println();
System.out.println("Please enter a whole number for");
System.out.println("conversion. You will be asked");
System.out.println("what type of conversion you want to");
System.out.println("perform in the next step:");
System.out.println();
degreeValue = keyboard.nextInt();
boolean typeOfConversionInputIsInvalid = true;
while (typeOfConversionInputIsInvalid)
{
System.out.println();
System.out.println();
System.out.println("Please enter either \"C\" or \"c\" for a");
System.out.println("Celsius-Fahrenheit conversion, or \"F\" or \"f\"");
System.out.println("for a Fahrenheit-Celsius conversion:");
System.out.println();
typeOfConversion = keyboard.next();
switch (typeOfConversion.charAt(0))
{
case 'C':
case 'c':
result = 5.0 * ((double)degreeValue - 32.0) / 9.0;
System.out.println();
System.out.println(degreeValue + " Fahrenheit is " + result + " Celsius");
typeOfConversionInputIsInvalid = false;
break;
case 'F':
case 'f':
result = (9.0 * ((double)degreeValue) / 5.0) + 32.0;
System.out.println();
System.out.println(degreeValue + " Celsius is " + result + " Fahrenheit");
typeOfConversionInputIsInvalid = false;
break;
default:
System.out.println();
System.out.println("Oops! Your entry is invalid. Let's try that again,");
System.out.println("shall we?");
typeOfConversionInputIsInvalid = true;
break;
}
}
System.out.println();
System.out.println();
System.out.println("Would you like to perform another conversion?");
System.out.println("Please enter \"yes\" or \"no\":");
System.out.println();
answer = keyboard.next();
// if((typeOfConversion == "C") || (typeOfConversion == "c"))
// {
// result = 5.0((double)degreeValue - 32.0)/9.0;
// System.out.println(degreeValue + " Fahrenheit is " + result + " Celsius");
// }
// else if((typeOfConversion == "F") || (typeOfConversion == "f"))
// {
// result = (9.0(degreeValue)/5.0) + 32.0;
// System.out.println(degreeValue + " Celsius is " + result + " Fahrenheit);
// }
}while (answer.equalsIgnoreCase("yes"));
}
}Holy crap... took me ALL DAY! The boolean stuff in the switch statement in order to control/exit the while loop certainly is not the most elegant way to do it, and by all means suggest how it could be done more elegantly/simply!, but that's all I could come up with for now. There must be a way to do it by just using a boolean value at the beginning of the WHILE statement.. while (typeOfConversion does NOT equal F, f, C, or c) Don't know how though... still, this thing WORKS! |
|
|
|
|
|
#25 | |
|
Programmer
Join Date: Jun 2005
Posts: 34
Rep Power: 0
![]() |
Quote:
Because the user is supposed to REDO it if it doesn't. If it DOES equal those chars, which is what your while statement suggests, why would the user want to re-go through the loop?? |
|
|
|
|
|
|
#26 |
|
Programmer
Join Date: Jun 2005
Posts: 34
Rep Power: 0
![]() |
EDIT:
Cropped the damn thing. EDIT2: Found a very nasty logical error. When "c" was pressed the program would do an F-C conversion instead of C-F. Same for when "f" was used. Here it is fixed and completed: Sample output!!! ![]() Last edited by shangnyun; Jun 9th, 2005 at 2:22 PM. |
|
|
|
|
|
#27 |
|
Programmer
Join Date: Jun 2005
Posts: 34
Rep Power: 0
![]() |
Final code:
import java.util.*;
public class TempConversion
{
public static void main(String[] args)
{
int degreeValue;
String typeOfConversion;
double result;
String answer;
Scanner keyboard = new Scanner(System.in);
do
{
System.out.println("=====TEMPERATURE CONVERSION=====");
System.out.println();
System.out.println("Please enter a whole number for");
System.out.println("conversion. You will be asked");
System.out.println("what type of conversion you want to");
System.out.println("perform in the next step:");
System.out.println();
degreeValue = keyboard.nextInt();
boolean typeOfConversionInputIsInvalid = true;
while (typeOfConversionInputIsInvalid)
{
System.out.println();
System.out.println();
System.out.println("Please enter either \"C\" or \"c\" for a");
System.out.println("Celsius-Fahrenheit conversion, or \"F\" or \"f\"");
System.out.println("for a Fahrenheit-Celsius conversion:");
System.out.println();
typeOfConversion = keyboard.next();
switch (typeOfConversion.charAt(0))
{
case 'C':
case 'c':
result = (9.0 * ((double)degreeValue) / 5.0) + 32.0;
System.out.println();
System.out.println(degreeValue + " Celsius is " + result + " Fahrenheit");
typeOfConversionInputIsInvalid = false;
break;
case 'F':
case 'f':
result = 5.0 * ((double)degreeValue - 32.0) / 9.0;
System.out.println();
System.out.println(degreeValue + " Fahrenheit is " + result + " Celsius");
typeOfConversionInputIsInvalid = false;
break;
default:
System.out.println();
System.out.println("Oops! Your entry is invalid. Let's try that again,");
System.out.println("shall we?");
typeOfConversionInputIsInvalid = true;
break;
}
}
System.out.println();
System.out.println();
System.out.println("Would you like to perform another conversion?");
System.out.println("Please enter \"yes\" or \"no\":");
System.out.println();
answer = keyboard.next();
// if((typeOfConversion == "C") || (typeOfConversion == "c"))
// {
// result = 5.0((double)degreeValue - 32.0)/9.0;
// System.out.println(degreeValue + " Fahrenheit is " + result + " Celsius");
// }
// else if((typeOfConversion == "F") || (typeOfConversion == "f"))
// {
// result = (9.0(degreeValue)/5.0) + 32.0;
// System.out.println(degreeValue + " Celsius is " + result + " Fahrenheit);
// }
}while (answer.equalsIgnoreCase("yes"));
}
}Any suggestions for improvements are welcome. Please tell me . I will only learn by reading your responses/suggestions.Thanks a TON for all the help! |
|
|
|
|
|
#28 |
|
Programming Guru
![]() |
I would ask what type of conversion first.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity." - Albert Einstein |
|
|
|
|
|
#29 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Looks fantastic, mate.
|
|
|
|
|
|
#30 |
|
Programmer
Join Date: Jun 2005
Posts: 34
Rep Power: 0
![]() |
Thank you, dear sir!! This is my first big success with Java. Spent ALL day on it and figured a lot of the code out myself. Granted, I had no clue what to do when I started.. but when I got done, I had twice the experience that I had before I started
![]() So nobody cann suggest a more elegant way to control that while loop? I don't know if placing boolean variables in the different cases in the switch statement is the most effective way to do it. And by placing the boolean to true in the default case . I was proud when I figured that out all by myself though! ![]() Thanks again for the kind comment. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|