Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Scanner util不提示输入_Java_Methods_Java.util.scanner - Fatal编程技术网

Java Scanner util不提示输入

Java Scanner util不提示输入,java,methods,java.util.scanner,Java,Methods,Java.util.scanner,我正在我的Java在线课程中做一个项目,我被迫构造一个程序来提供所需的输出,而没有得到任何帮助我的线索。所以错误肯定是我的,而不是机器的。尽管大量的谷歌搜索让我走到了死胡同 这是edX提供给我的样本输出 这是我迄今为止设计的代码,我所知甚少 import java.util.Scanner; public class planner { public static void main(String[] args) { System.out.println("Planner initi

我正在我的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]
声明告诉了我问题所在,但我不明白到底出了什么问题。我能得到一些帮助吗?

我想混乱与此有关:
Scanner fn=新的扫描仪(System.in)
它所做的是声明一个名为fn的scanner对象。如果要使用该扫描仪对象从System.in读取,则必须使用类似以下内容:
Scanner fn=新的扫描仪(System.in)
String-answer=fn.nextLine()
阅读此处了解有关扫描仪的更多信息:

如果您使用
nextLine()
字符串形式,它会更好

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("");
 }
}

您只需要构造一个扫描仪来从控制台读取您自己问:您分配给
fn
的是什么?扫描仪是这样工作的吗?如果是,是什么让你认为是这样的?如果没有,google对这个问题怎么看?这里有很多关于使用Java中的Scanner检索用户输入的帖子。请做一些研究,并阅读其中的一些帖子,以帮助你理清这些问题。开始只需查看for
扫描仪
。这有你需要的所有信息。@YassinHajaj我理解扫描仪的概念
fn
代表名字,只使用过一次,还有
ln
代表姓氏。非常感谢。在使用
string name=fn.nextLine()
声明
fn
字符串,并让欢迎语句使用定义的字符串而不是原始扫描仪输入后,我的问题得到了解决。