试图将多个程序组合在一起

时间:2015-05-26 02:10:22

标签: java class methods java.util.scanner

我正在尝试将我创建的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个程序。如果有人可以帮助指导我在这方面做出正确的指导,那就太棒了。

3 个答案:

答案 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
     */
}

答案 1 :(得分:0)

我不确定你想要完成什么,但是如果确实有必要将所有三个类组合在一起尝试使用Java内部类

答案 2 :(得分: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及其三种方法。这使主程序更易于阅读和维护。还有改进的余地,但我认为第一种方法是可以的。