【问题标题】:Error with the java code plsjava代码错误请
【发布时间】:2017-08-06 04:59:36
【问题描述】:

我在互联网上有这段代码,但我无法运行它们请告诉我 为什么???

public static void main( String[] args ) {
    keyboard[Scanner] = new Scanner(System.in);

    System.out.print( "Hello. What is your name? " );
    name[String] = keyboard[Scanner].next();

    System.out.print( "Hi, " + name[String] + "! How old are you? " );
    age[int] = keyboard[Scanner].nextInt();

    System.out.println("So you're " + age[int] + ", eh? That's not so old.");
    System.out.print( "How much do you weigh, " + name[String] + "? " );
    weight[double] = keyboard[Scanner].nextDouble();

    System.out.println( weight[double] + "! Better keep that quiet!!" );
    System.out.print("Finally, what's your income, " + name[String] + "? " );
    income[double] = keyboard[Scanner].nextDouble();

    System.out.print( "Hopefully that is " + income[double] + " per hour" );
    System.out.println( " and not per year!" );
    System.out.print( "Well, thanks for answering my rude questions, " );
    System.out.println( name[String] + "." );
}

请帮帮我!!谢谢!!!

【问题讨论】:

  • 你得到什么错误?
  • 那些“类型引用”甚至不是 Java。你是从哪里弄到这个的?
  • 这是一种在 java 中声明变量的新方法吗?您使用的是哪个神圣的 Java 版本?
  • 来自“书”(learnjavathehardway.org/book/ex08.html):我编写了一些代码来展示如果每次使用时都必须添加变量的类型,Java 可能会是什么样子。 不要费心输入它;它不会编译,因为 Java(谢天谢地)不能那样工作。
  • 但是这本书告诉你,你不应该尝试运行它,因为它不是有效的java代码!它已经告诉你它错了,那你想从我们这里得到什么?

标签: java error-handling compiler-errors


【解决方案1】:

来自您在 cmets 中提到的那本书。 .就在这段代码之后……书中的陈述是:

哇!你不是很高兴每次使用变量时都不需要这样做吗??

您需要的内容如下:

public static void main( String[] args ) {
    Scanner keyboard = new Scanner(System.in);

    System.out.print( "Hello. What is your name? " );
    String name = keyboard.next();

    System.out.print( "Hi, " + name+ "! How old are you? " );
    int age = keyboard.nextInt();

    System.out.println("So you're " + age + ", eh? That's not so old.");
    System.out.print( "How much do you weigh, " + name + "? " );
    double  weight = keyboard.nextDouble();

    System.out.println( weight + "! Better keep that quiet!!" );
    System.out.print("Finally, what's your income, " + name + "? " );
    double income = keyboard.nextDouble();

    System.out.print( "Hopefully that is " + income + " per hour" );
    System.out.println( " and not per year!" );
    System.out.print( "Well, thanks for answering my rude questions, " );
    System.out.println( name + "." );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-13
    • 1970-01-01
    相关资源
    最近更新 更多