java - 更新了 util.scanner 重复方法

标签 java string methods return java.util.scanner

我已经修复了原始代码中的错误并正确格式化了它。但是,代码现在在继续执行最后一个方法之前重复 String next()。我以为我明白为什么,但当我尝试修复它时,程序再次失败。感谢您的宝贵时间!

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.println("Great! "+destination +" sounds like a great trip");
     }
    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;
    }
    public static void third() {
        int days = Integer.valueOf(next());
        int hours = days * 24;
        int minutes = hours * 60;
        System.out.println("If your are travelling for " + days + " days that is the same as " + hours + " hours or " + minutes + " minutes");
    }


    }

最佳答案

据我所知,方法 next() 从 main 方法中调用一次,然后在第三个方法中再次调用

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

也许您应该创建一个名为 days 的实例变量,并将 next() 的值存储在其中。然后在third()方法中使用变量的值。即

import java.util.Scanner;

public class LearnScanner {
    private static int days = 0;

    public static void main (String[] args) {
        first();
        days = Integer.parseInt(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.println("Great! "+destination +" sounds like a great trip");
     }
    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;
    }
    public static void third() {
        int tempDays = Integer.valueOf(days);
        int hours = days * 24;
        int minutes = hours * 60;
        System.out.println("If your are travelling for " + tempDays + " days that is the same as " + hours + " hours or " + minutes + " minutes");
    }

关于java - 更新了 util.scanner 重复方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47339219/

相关文章:

python - 如何从我的 Python 主函数返回多个字典

class - 是否可以在 Visio 2007 中显示方法/操作返回和对象数组(而不仅仅是单个对象)?

java - 日期格式在 Java 中不起作用

java - JSoup:如何选择包含多个表的页面中的第一个表

Java - split(regex, limit) 方法实际上是如何工作的?

java - 在 Python 中使用 Unicode 保持 Java 字符串偏移量一致

java - 一个类在实例化另一个类后如何调用方法?

java - java中使用While循环计算列表的元素数量

java - 在命令行上将多个 jar 添加到类路径

javascript - 如何在 JavaScript 中替换特定索引处的字符?