【问题标题】:Updated util.scanner repeating method [duplicate]更新了 util.scanner 重复方法 [重复]
【发布时间】:2018-04-30 13:20:25
【问题描述】:

我已修复原始代码中的错误并正确格式化。但是,代码现在在执行最后一个方法之前重复 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");
    }


    }

【问题讨论】:

    标签: java string methods return java.util.scanner


    【解决方案1】:

    据我所知,方法 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:不兼容的类型:java.lang.String 无法转换为 int
    • 我明白了.. 您应该将字符串转换为整数。我现在重构代码
    • @ErikBrabo 我已经添加了转换
    • 感谢您的更新,但错误是:java: variable days may not have been initialized
    猜你喜欢
    • 1970-01-01
    • 2020-12-18
    • 2011-09-27
    • 2014-01-10
    • 1970-01-01
    • 2019-07-31
    • 2020-04-19
    • 2022-01-02
    • 1970-01-01
    相关资源
    最近更新 更多