I am working on a program that incorperates many little programs in to it. The user is asked what they would like to run and based on that the program will run that class. I am not having any luck calling classes and i am not sure what i am doing wrong. I also know that you can import classes with java and have tried this with no success. Any help is appreciated thanks.
import java.util.Scanner;
public class bundle{
public static void main(String[] args){
int selection = 0;
System.out.println("1. Run Temp Conversion program");
Scanner in = new Scanner(System.in);
selection = in.nextInt();
if(selection == 1){
temp();
}
}
public class temp{
public static void CtF(){
int CtF;
int C;
System.out.println("Enter a Celcius Temp: ");
Scanner in = new Scanner(System.in);
C = in.nextInt();
in.close();
CtF = C*9/5+32;
System.out.printf("Your Temp in Fahrenheit is: ");
System.out.print(CtF);
}
public static void FtC(){
int FtC;
int F;
System.out.println("Enter a Fahrenheit Temp: ");
Scanner in = new Scanner(System.in);
F = in.nextInt();
in.close();
FtC = (F - 32)*5/9;
System.out.printf("Your Temp in Celcius is: ");
System.out.print(FtC);
}
public static void main(String[] args){
int select = 0;
System.out.println("1. If you wish to convert from Celcius to Fahrenheit");
System.out.println("2. If you wish to convert from Fahrenheit to Celcius");
Scanner in = new Scanner(System.in);
select = in.nextInt();
if(select == 1){
CtF();
}
if(select == 2){
FtC();
}
if(select == 3){
System.exit(0);
}
}
}
}
Question
mtber
I am working on a program that incorperates many little programs in to it. The user is asked what they would like to run and based on that the program will run that class. I am not having any luck calling classes and i am not sure what i am doing wrong. I also know that you can import classes with java and have tried this with no success. Any help is appreciated thanks.
import java.util.Scanner; public class bundle{ public static void main(String[] args){ int selection = 0; System.out.println("1. Run Temp Conversion program"); Scanner in = new Scanner(System.in); selection = in.nextInt(); if(selection == 1){ temp(); } } public class temp{ public static void CtF(){ int CtF; int C; System.out.println("Enter a Celcius Temp: "); Scanner in = new Scanner(System.in); C = in.nextInt(); in.close(); CtF = C*9/5+32; System.out.printf("Your Temp in Fahrenheit is: "); System.out.print(CtF); } public static void FtC(){ int FtC; int F; System.out.println("Enter a Fahrenheit Temp: "); Scanner in = new Scanner(System.in); F = in.nextInt(); in.close(); FtC = (F - 32)*5/9; System.out.printf("Your Temp in Celcius is: "); System.out.print(FtC); } public static void main(String[] args){ int select = 0; System.out.println("1. If you wish to convert from Celcius to Fahrenheit"); System.out.println("2. If you wish to convert from Fahrenheit to Celcius"); Scanner in = new Scanner(System.in); select = in.nextInt(); if(select == 1){ CtF(); } if(select == 2){ FtC(); } if(select == 3){ System.exit(0); } } } }Link to comment
Share on other sites
2 answers to this question
Recommended Posts