如何在其他方法中使用用户输入

时间:2017-11-16 19:46:57

标签: java java.util.scanner

对于以下内容,我很难理解如何使用(天)的用户输入并在最后一行使用它来根据用户输入(天)计算小时数和分钟数。

import java.util.Scanner;

public class LearnScanner {
    public static void main (String[] args) {
        first();
        next();
        third();
    }
    public static void first() {
        Scanner input = new Scanner(System.in);
        System.out.println("Welcome to Vacation Planner!!");
        System.out.print("What is your name?");
        String name = input.nextLine();
        System.out.print("Nice to meet you "+name +", where are you travelling to?");
        String destination = input.nextLine();
        System.out.print("Great! "+destination +" sounds like a great trip");
     }
    public static void next() {
        Scanner input = new Scanner(System.in);
        System.out.print("How many days are you going to spend travelling?");
        String days = input.nextLine();
        System.out.print("How much money in USD are you planning to spend on your trip?");
        String budget = input.nextLine();
        System.out.print("What is the three letter currency symbol for your travel destination?");
        String currency = input.nextLine();
        System.out.print("How many " + currency + " are there in 1 USD?");
        String currencyConversion = input.nextLine();
    }
    public static void third() {
        String days;
        String hours = days * 24;
        String minutes = hours * 60;

        System.out.println("If your are travelling for " + days + " days that is the same as " + hours + " hours or " + minutes + " minutes");
    }
}

2 个答案:

答案 0 :(得分:0)

next()方法返回String,而不是无效。

public static String next() {
    Scanner input = new Scanner(System.in);
    System.out.print("How many days are you going to spend travelling?");
    String days = input.nextLine();
    System.out.print("How much money in USD are you planning to spend on your trip?");
    String budget = input.nextLine();
    System.out.print("What is the three letter currency symbol for your travel destination?");
    String currency = input.nextLine();
    System.out.print("How many " + currency + " are there in 1 USD?");
    String currencyConversion = input.nextLine();
    return days;
}

然后在您的third()方法中更改以下代码行:

String days;

int days = Integer.valueOf(next());

使用以下内容更改接下来的两行代码:

int hours = days * 24;
int minutes = hours * 60;

答案 1 :(得分:0)

首先,程序似乎不应该成功编译。 对象'天' in third()是String类型。将String对象与数值24相乘是不可能的。也可以在下一行上分钟和小时。

此外,'天'在next()中被接受,因此是一个局部变量。它在第三()中没有任何意义。

也许这可能会有所帮助。

import java.util.Scanner;

public class LearnScanner
{
    static String days = null;

    public static void main(String[] args)
    {

        first();
        next();
        third();
    }

    public static void first()
    {

        Scanner input = new Scanner(System.in);
        System.out.println("Welcome to Vacation Planner!!");
        System.out.print("What is your name?");
        String name = input.nextLine();
        System.out.print("Nice to meet you " + name + ", where are you travelling to?");
        String destination = input.nextLine();
        System.out.print("Great! " + destination + " sounds like a great trip");
    }

    public static void next()
    {

        Scanner input = new Scanner(System.in);
        System.out.print("How many days are you going to spend travelling?");
        days = input.nextLine();
        System.out.print("How much money in USD are you planning to spend on your trip?");
        String budget = input.nextLine();
        System.out.print(
                "What is the three letter currency symbol for your travel destination?");
        String currency = input.nextLine();
        System.out.print("How many " + currency + " are there in 1 USD?");
        String currencyConversion = input.nextLine();
    }

    public static void third()
    {

        String hours = String.valueOf(Integer.valueOf(days) * 24);
        String minutes = String.valueOf(Integer.valueOf(hours) * 60);

        System.out.println(
                "If your are travelling for " + days + " days that is the same as "
                        + hours + " hours or " + minutes + " minutes");
    }

}