我正在嘗試創建3個不同的程序,並將它們放在一個類中。我的教授已經說過我必須這樣做,但我不知道如何去做。我不是在這裏尋找出手,只是一些我如何能夠快速有效地做到這一點。我也想弄清楚如何從每個程序的同一個掃描器調用,或者如果我只需要創建多個程序。試圖將多個程序結合在一起
import java.util.Scanner;
public class AssignmentOneFahrenheit {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello, I can convert Fahrenheit to Celsius!");
System.out.println("Please enter the degrees Fahrenheit you want converted.");
double degreesF;
double degreesC;
Scanner keyboard = new Scanner(System.in);
degreesF = keyboard.nextDouble(); //Allows user to input decimal number
keyboard.close();
System.out.println("The temperature in Degrees Celsius is: ");
degreesC = 5*(degreesF - 32)/9;
System.out.printf("%.2f", degreesC);
}
import java.util.Scanner;
public class AssignmentOneHate {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter a line containing 'hate'.");
String text = keyboard.nextLine();
System.out.println("I have changed that line to read: ");
System.out.println(text.replaceFirst("hate", "love"));
keyboard.close();
}
import java.util.Scanner;
public class AssignmentOneVerticalDisplay {
public static void main(String[] args) {
// TODO Auto-generated method stub
int userInput;
System.out.println("Please enter a 4 digit integer.");
Scanner keyboard = new Scanner(System.in);
userInput = keyboard.nextInt();
System.out.println(userInput/1000);
userInput = userInput % 1000;
System.out.println(userInput/100);
userInput = userInput % 100;
System.out.println(userInput/10);
System.out.println(userInput % 10);
keyboard.close();
}
}
我基本上只是複製並粘貼了2個我創建的程序。如果有人能幫助我在正確的方向上引導我,那將是非常棒的。
現在你有一個文件有兩個類定義和兩個main()方法。改變它,使你只有一個類定義和一個main()方法。此外,導入位於頂部(我認爲,對於Java),您應該只有一次導入每行。 –
我在這裏發佈的內容只是複製了一個程序並複製了另一個程序。我試圖弄清楚如何將其中的2個結合起來。 –
是的,我的評論爲您提供了一些關於如何開始的一般指導。 –