First flowchart
Second Flowchart
It looks very complicated and lacks visual attraction, but I tried and learned at least!
Third Flowchart after Feedback
After realizing "Think twice, Code once" is important, these are my first flow charts. I thought three times and coded twice, but it helped me to understand how my algorithms have to be.
First Code
import java.util.Scanner;
//Temperature Converter Far -> Cel / Cel -> Far
public class TempConverter {
public static void main(String[] args) {
// TODO Auto-generated method stub
String num1;
int tem, press;
double num2, num3;
// boolean tof = false;
Scanner sc = new Scanner(System.in);
System.out.println("Press 1 to convert Fahrenheit to Celsius, Press 2 to convert from Celsius to Fahrenheit. ");
press = sc.nextInt();
// while (true) {
try {
while (true) {
if (press == 1) {
System.out.println("Enter Fahrenheit.");
sc.nextLine();// The "Enter" key after inputting the number is considered as one input.
// By inserting this line, you can avoid the output of "Not a number" right
// away.
num1 = sc.nextLine();
// while (true) {
try {
num2 = Double.parseDouble(num1);
// To check if int is inserted or not.
// formula: (32°F − 32) × 5/9 = 0°C
num3 = ((((num2 - 32.0) * 5.0) / 9.0));
System.out.printf("%.2f", num3);
System.out.println();
return;
} catch (NumberFormatException n) {
System.out.println("Not a number. Please enter again.");
sc.nextLine();
}
} else if (press == 2) {
System.out.println("Enter Celsius.");
sc.nextLine();
num1 = sc.nextLine();
try {
num2 = Double.parseDouble(num1);
// To check if int is inserted or not.
// (0°C × 9/5) + 32 = 32°F
num3 = (((num2 * 9) / 5) + 32);
System.out.printf("%.2f", num3);
System.out.println();
} catch (NumberFormatException nn) {
System.out.println("Not a number.");
}
}
}
} catch (Exception e) {
System.out.println("Invalid Access. Please enter again.");
}
// }
}
}
Second Code(Final)
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class TempConverter2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
System.out.println("From Fahrenheit to Celsius press 1, vice versa, press 2.");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String inputString = br.readLine();
int num = Integer.parseInt(inputString);
if (num == 1) {
farToCel();
} else if (num == 2) {
celToFar();
} else {
System.out.println("Not a valid access.");
}
} catch (Exception eo) {
System.out.println("Not a number.");
}
}
public static void farToCel() {
while (true) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter number.");
String inputString = br.readLine();
double far = Double.parseDouble(inputString);
double result1 = ((((far - 32.0) * 5.0) / 9.0));
System.out.printf("%.2f", result1);
System.out.println();
System.out.println("To repeat press anything, to finish, press F or f.");
String inputString2 = br.readLine();
if (inputString2.equals("F") || inputString2.equals("f")) {
System.out.println("Finish the program.");
break; // to go back to the main menu
} else {
continue;
}
} catch (Exception e) {
System.out.println("Not a number.");
}
}
}
public static void celToFar() {
while (true) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter number.");
String inputString = br.readLine();
double cel = Double.parseDouble(inputString);
double result2 = (((cel * 9) / 5) + 32);
System.out.printf("%.2f", result2);
System.out.println();
System.out.println("To go repeat press anything, to finish, press F or f.");
String inputString3 = br.readLine();
if (inputString3.equals("F") || inputString3.equals("f")) {
System.out.println("Finish the program.");
break;
} else {
continue;
}
} catch (Exception ei) {
System.out.println("Not a number.");
}
}
}
}
'Codes & Projects' 카테고리의 다른 글
JSP/ Java / Html / javaScript/ jQuery) Bulletin Board with file attatchment function - DTO, DAO Class (0) | 2022.09.21 |
---|---|
JSP) Model2 - Java Servlet 2 (0) | 2022.09.16 |
HTML / JSP) Multiplication table - <select> (0) | 2022.09.04 |
Java) ATM - While loops / If statements (0) | 2022.09.03 |
JSP / CSS) Log In - JavaBean, Action tags (0) | 2022.09.02 |