Java获取用户详细信息

时间:2010-03-19 12:51:12

标签: java

我是Java的新手,我必须编写一个程序来获取用户详细信息,如下所示:


Author’s Details
****************
Name: J. Beans

YOB: 1969

Age: 41

Book Details

************

       Title: *Wonderful Java*
       ISBN: *978 0 470 10554 9*
       Publisher: *Wiley*

这就是我所做的,但它不起作用,任何人都可以帮我找出问题吗?

import java.util.Scanner ;
public class UserDetails
{
public static void main(String args[])
{
    System scan = new Scanner(System.in);
    input sname, fname, born, title, isbn, publisher;
    System.out.print("Please enter author's surname:");
    sname = input.nextLine();
    System.out.print("Please the initial of author's first name:");
    fname = input.nextLine();
    System.out.print("Please enter the year the author was born:");
    born = input.nextLine();
    System.out.print("Please enter the author's book title:");
    title = input.nextLine();
    System.out.print("Please enter the book's ISBN:");
    isbn = input.nextLine();
    System.out.print("Please enter the publisher of the book:");
    publisher = input.nextLine;

    System.out.println("Author's detail");
    System.out.println("**********************");
    System.out.println("Name:" + fname + sname);
    System.out.println("YOB:" + born);
    System.out.println("Age" + born);
    System.out.println("Book Details");
    System.out.println("**********************");
    System.out.println("Title:" + "*" + title + "*");
    System.out.println("ISBN:" + "*" + isbn + "*");
    System.out.println("Publisher:" + "*" + publisher + "*");
  }
}

4 个答案:

答案 0 :(得分:2)

除此之外,改变这个:

  System scan = new Scanner(System.in);
  input sname, fname, born, title, isbn, publisher;

到此:

  Scanner input = new Scanner(System.in);
  String sname, fname, born, title, isbn, publisher;

另见:

Java语言:

API:

如果您真的非常认真学习如何编程,请遵循每个人的建议并:

  • 阅读书籍。
  • 学习tutorials
  • 使用IDE(Eclipse是一个很好的,它是free to download
  • 练习很多。
  • 提出很多问题。

答案 1 :(得分:2)

嗨,你只是错误地使用了类。你的代码应该是

import java.util.Scanner;

public class UserDetails {
    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        String sname, fname, born, title, isbn, publisher;
        System.out.print("Please enter author's surname:");

        sname = scan.nextLine();
        System.out.print("Please the initial of author's first name:");
        fname = scan.nextLine();
        System.out.print("Please enter the year the author was born:");
        born = scan.nextLine();
        System.out.print("Please enter the author's book title:");
        title = scan.nextLine();
        System.out.print("Please enter the book's ISBN:");
        isbn = scan.nextLine();
        System.out.print("Please enter the publisher of the book:");
        publisher = scan.nextLine();

        System.out.println("Author's detail");
        System.out.println("**********************");
        System.out.println("Name:" + fname + sname);
        System.out.println("YOB:" + born);
        System.out.println("Age" + born);
        System.out.println("Book Details");
        System.out.println("**********************");
        System.out.println("Title:" + "*" + title + "*");
        System.out.println("ISBN:" + "*" + isbn + "*");
        System.out.println("Publisher:" + "*" + publisher + "*");
    }
}

答案 2 :(得分:0)

您的代码充满了语法和语义错误。这有效:

 import java.util.Scanner ;
  public class UserDetails {
  public static void main(String args[])
  {
  Scanner scan = new Scanner(System.in);
  String sname, fname, born, title, isbn, publisher;
  System.out.print("Please enter author's surname:");
  sname = scan.nextLine();
  System.out.print("Please the initial of author's first name:");
  fname = scan.nextLine();
  System.out.print("Please enter the year the author was born:");
  born = scan.nextLine();
  System.out.print("Please enter the author's book title:");
  title = scan.nextLine();
  System.out.print("Please enter the book's ISBN:");
  isbn = scan.nextLine();
  System.out.print("Please enter the publisher of the book:");
  publisher = scan.nextLine();

  System.out.println("Author's detail");
  System.out.println("**********************");
  System.out.println("Name:" + fname + sname);
  System.out.println("YOB:" + born);
  System.out.println("Age" + born);
  System.out.println("Book Details");
  System.out.println("**********************");
  System.out.println("Title:" + "*" + title + "*");
  System.out.println("ISBN:" + "*" + isbn + "*");
  System.out.println("Publisher:" + "*" + publisher + "*");
  }
  }

答案 3 :(得分:0)

你很亲密,但你有几个错误和错字。

系统扫描=新扫描仪(System.in); 这应该是:  扫描仪扫描=新扫描仪(System.in); 你在这里做的是创建一个名为scan的扫描器类型的新对象引用 - 而不是System。

下一个问题:  输入sname,fname,born,title,isbn,publisher; 这又是类型的声明问题。这些变量中的每一个都是一个字符串引用 - 应该将其声明为;  String sname,fname,born,title,isbn,publisher;

最后,你有一个错字:  publisher = input.nextLine; 你忘了()表示这是一个函数调用。应该:  publisher = input.nextLine();

可能还有其他一些更微妙的问题,但这应该让你的代码至少可以编译和运行:)。