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