2015-05-26 52 views
0

我正在嘗試創建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個我創建的程序。如果有人能幫助我在正確的方向上引導我,那將是非常棒的。

+1

現在你有一個文件有兩個類定義和兩個main()方法。改變它,使你只有一個類定義和一個main()方法。此外,導入位於頂部(我認爲,對於Java),您應該只有一次導入每行。 –

+0

我在這裏發佈的內容只是複製了一個程序並複製了另一個程序。我試圖弄清楚如何將其中的2個結合起來。 –

+0

是的,我的評論爲您提供了一些關於如何開始的一般指導。 –

回答

0

您可以使用Double.parseDouble(String string);函數和try-catch來檢查它是否是輸入中的數字或字符串。

(...) 
String text = keyboard.nextLine(); 
try { 
    //We try and assume that it is a number 
    Double number = Double.parseDouble(text); 
    /** 
    * Do stuff with the number like in the 1st program 
    */ 
} 
catch (NumberFormatException e) 
{ 
    //The input turned out not to be a number. 
    /** 
    * Do stuff here with the string like the 2nd program 
    */ 
} 
0

我不是真的肯定你想要什麼來完成,但如果真的有必要,你所有三類結合在一起嘗試使用Java內部類

0

我認爲你的教授正在尋找一個更加面向對象的解決方案。你可以創建一個包含三個方案,作爲這樣

import java.util.scanner; 

public class AssignmentScanner { 

    public double convertToCelsius() { 
     Scanner keyboard = new Scanner(System.in); 
     double degreesF = keyboard.nextDouble(); 
     keyboard.close(); 
     return 5*(degreesF - 32)/9; 
    } 

    public String replaceHate() { 
     Scanner keyboard = new Scanner(System.in); 
     String text = keyboard.nextLine(); 
     String replacedText = text.replaceFirst("hate", "love"); 
     keyboard.close(); 
     return replacedText; 
    } 

    public int oneVerticalDisplay() { 
     Scanner keyboard = new Scanner(System.in); 

     int 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(); 
    } 
} 

您還需要創建一個使用這個對象這樣的主程序分離方法的類:

public class AssignmentMain { 

    public static void main(String[] args) { 
     AssignmentScanner assignmentScanner = new AssignmentScanner(); 

     System.out.println("Hello, I can convert Fahrenheit to Celsius!"); 
     System.out.println("Please enter the degrees Fahrenheit you want converted."); 
     double degreesC = assignmentScanner.convertToCelsius(); 
     System.out.println("The temperature in Degrees Celsius is: "); 
     System.out.printf("%.2f", degreesC); 

     System.out.println("Please enter a line containing 'hate'."); 
     String replacedText = assignmentScanner.replaceHate(); 
     System.out.println("I have changed that line to read: "); 
     System.out.println(replacedText); 

     System.out.println("Please enter a 4 digit integer."); 
     assigmentScanner.oneVerticalDisplay(); 
    } 
} 

這樣,只有你的主程序瞭解AssignmentScanner及其三種方法。這使得主程序更易於閱讀和維護。還有改進的空間,但我認爲這是第一種方法。