我正在我的Java在线课程中完成一个项目,我被迫构建一个程序来提供所需的输出,而没有任何线索来帮助我。因此,错误绝对是我的意思,而不是机器的错误。虽然广泛的谷歌搜索让我陷入了死胡同。
这是edX给我的样本输出。
这是我迄今为止设计的代码,我的知识很少。
import java.util.Scanner;
public class planner {
public static void main(String[] args) {
System.out.println("Planner initialized.");
System.out.println("Reading settings..");
System.out.println("Assessing environment..");
System.out.println("Gathering tools..");
System.out.println("****************************************");
System.out.println("");
intro();
}
public static void intro() {
System.out.println("Welcome to the vacation planner.");
System.out.println("What is your name?");
Scanner fn = new Scanner(System.in);
Scanner ln = new Scanner(System.in);
System.out.println("Welcome, " + fn + ln + ". Where are you traveling to?");
Scanner loc1 = new Scanner(System.in);
Scanner loc2 = new Scanner(System.in);
System.out.println("Excellent. " + loc1 + loc2 + " it is.");
System.out.println("*****************************************");
System.out.println("");
calc();
}
public static void calc() {
System.out.println("How many days are you planning to spend away?");
Scanner x = new Scanner(System.in);
System.out.println("How much money, in U.S. dollars, are you planning to spend?");
Scanner m = new Scanner(System.in);
System.out.println("What is the three-letter currency symbol for your destination?(Google may be required)");
Scanner i = new Scanner(System.in);
System.out.println("How many " + i + " are there in USD? (Google may be required)");
Scanner o = new Scanner(System.in);
System.out.println("");
float t = x.nextFloat();
float r = m.nextFloat();
float k = o.nextFloat();
//To calculate the amount of hours
float n = t * 24;
//To calculate the amount of minutes
float u = (t * 24) * 60;
//To calculate daily budget
float y = t / r;
// To calculate currency exchange
float e = k * r;
//To calculate budget in alt currency
float q = t / e;
System.out.println("If you are travelling for " + t + " days, that is the same as " + n + " hours, or " + u + " minutes.");
System.out.println("If you are going to spend " + r + " U.S. dollars per day, you can spend" + y + " dollars per day.");
System.out.println("Your total budget in " + i + "is " + e + i + ", which, per day, is " + q + i);
System.out.println("****************************************");
System.out.println("");
}
}
我得到了输出:
/home/foo/jdk-9.0.4/bin/java -javaagent:/opt/intellij-idea-community/lib/idea_rt.jar=37671:/opt/intellij-idea-community/bin -Dfile.encoding=UTF-8 -classpath /home/samuel/IdeaProjects/Module-test/out/production/Module-test planner
Planner initialized.
Reading settings..
Assessing environment..
Gathering tools..
****************************************
Welcome to the vacation planner.
What is your name?
Welcome, java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\QNaN\E][infinity string=\Q∞\E]java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\QNaN\E][infinity string=\Q∞\E]. Where are you traveling to?
Excellent. java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\QNaN\E][infinity string=\Q∞\E]java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\QNaN\E][infinity string=\Q∞\E] it is.
*****************************************
How many days are you planning to spend away?
How much money, in U.S. dollars, are you planning to spend?
What is the three-letter currency symbol for your destination?(Google may be required)
How many java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\QNaN\E][infinity string=\Q∞\E] are there in USD? (Google may be required)
而且我认为它是[need input=false]
声明,它告诉我问题,但我不明白什么是错的。我可以帮忙吗?
答案 0 :(得分:0)
我认为这种混淆与此有关:
Scanner fn = new Scanner(System.in);
这样做是为了声明一个名为fn的扫描器对象。如果要使用该扫描仪对象从System.in读取,则必须使用以下内容:
Scanner fn = new Scanner(System.in);
String answer = fn.nextLine();
请阅读此处以了解有关扫描仪的更多信息:https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
答案 1 :(得分:0)
如果您使用nextLine()
字符串格式Scanner和Float.parseFloat(String)则会更好
import java.util.*;
import java.lang.*;
public class Planner{
public static void main(String[] args) {
System.out.println("Planner initialized.");
System.out.println("Reading settings..");
System.out.println("Assessing environment..");
System.out.println("Gathering tools..");
System.out.println("****************************************");
System.out.println("");
intro();
}
public static void intro() {
Scanner fn = new Scanner(System.in);
System.out.println("Welcome to the vacation planner.");
System.out.println("What is your first name?");
String name = fn.nextLine();
System.out.println("What is your last name?");
String lastname = fn.nextLine();
System.out.println("Welcome, " + name + " " + lastname);
System.out.println(". Where are you traveling to?");
String traveling = fn.nextLine();
System.out.println("Excellent. " + traveling + " it is.");
System.out.println("*****************************************");
System.out.println("");
calc();
}
public static void calc() {
Scanner scanner = new Scanner(System.in);
System.out.println("How many days are you planning to spend away?");
String x = scanner.nextLine();
System.out.println("How much money, in U.S. dollars, are you planning to spend?");
String m = scanner.nextLine();
System.out.println("What is the three-letter currency symbol for your destination?(Google may be required)");
String i = scanner.nextLine();
System.out.println("How many " + i + " are there in USD? (Google may be required)");
String o = scanner.nextLine();
System.out.println("");
float t = Float.parseFloat(x);
float r = Float.parseFloat(m);
float k = Float.parseFloat(o);
//To calculate the amount of hours
float n = t * 24;
//To calculate the amount of minutes
float u = (t * 24) * 60;
//To calculate daily budget
float y = t / r;
// To calculate currency exchange
float e = k * r;
//To calculate budget in alt currency
float q = t / e;
System.out.println("If you are travelling for " + t + " days, that is the same as " + n + " hours, or " + u + " minutes.");
System.out.println("If you are going to spend " + r + " U.S. dollars per day, you can spend" + y + " dollars per day.");
System.out.println("Your total budget in " + i + "is " + e + i + ", which, per day, is " + q + i);
System.out.println("****************************************");
System.out.println("");
}
}