大3,刚学java不久,下面的程序貌似很长,拜托各位大侠ctrl+c一下帮我看一下。我想单独建一个书库类,为什么构造不起来哩?也许是我的概念没弄懂吧。
谢谢,祝各位大侠新年好运~~~
import java.io.*;
import java.util.*;
public class stock
{
String name;
String author;
int price;
stock(String a,String b,int c)
{
name=a;
author=b;
price=c;
}
void print()
{
System.out.println("name is "+name);
System.out.println("author is "+author);
System.out.println("price is "+price);
}
void get()
{
int N=100;
stock []a=new stock[N];
a[0]=new stock("Java","PlayGrrrr...",12);
a[1]=new stock("C#","Bill Gates",13);
a[2]=new stock("Delphi","Boxer",14);
a[3]=new stock("Oracle","zy",15);
}
}
class process
{
void step1()
{
System.out.println("do what?");
System.out.println("a.search    b.add   c.exit");
char ch=0;
try
{
ch=(char)System.in.read();
System.in.skip(2);
}catch(IOException z){}
if(ch=='a')
{
System.out.println("a.by name    b.by author");

char ch2=0;
try
{
ch2=(char)System.in.read();
System.in.skip(2);
}catch(IOException z){}
if(ch2=='a')
{
    System.out.print("plz input the book's name:");
    InputDate fuck=new InputDate();
    fuck.findbookname();
}

if(ch2=='b')
{
System.out.print("plz input the author's name:");
InputDate fuck2=new InputDate();
fuck2.findauthorname();
}
}
if(ch=='b')
{
InputDate fuck3=new InputDate();
fuck3.add();
}
if(ch=='c')
{
System.out.println("c u");
}
}
}
class InputDate 
{
static private String s;
static public void input()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try{
s=br.readLine();
}catch(IOException e){}

}
static public String getString()
{
input();
return s;

}
static public int getInt()
{
input();
return Integer.parseInt(s);
}
public void findbookname()
{
    
/*int N=100;
stock []a=new stock[N];
a[0]=new stock("Java","PlayGrrrr...",12);
a[1]=new stock("C#","Bill Gates",13);
a[2]=new stock("Delphi","Boxer",14);
a[3]=new stock("Oracle","zy",15);*/
     stock mis=new stock();
     mis.get();
     s=InputDate.getString();
     Vector names=new Vector();
     for(int i=0;i<a.length&&a[i+1]!=null;i++)
     {
     names.add(a[i].name);
     }
     if(names.indexOf(s)!=-1)
     {
     System.out.println("u can fint it here!");
     }
     else
     System.out.println("sorry,there is not such a book!");
    
}
    public void findauthorname()
{
     s=InputDate.getString();
    
     /*int N=100;
stock []a=new stock[N];
a[0]=new stock("Java","PlayGrrrr...",12);
a[1]=new stock("C#","Bill Gates",13);
a[2]=new stock("Delphi","Boxer",14);
a[3]=new stock("Oracle","zy",15);*/
     Vector authornames=new Vector();
     for(int i=0;i<a.length&&a[i+1]!=null;i++)
     {authornames.add(a[i].author);}
     if(authornames.indexOf(s)!=-1)
     {
    
     System.out.println("there is such a book!");}
     else
     System.out.println("i didn't hear that pl at all!");
    
    }
    public void add()
    {
     /*int N=100;
stock []a=new stock[N];
a[0]=new stock("Java","PlayGrrrr...",12);
a[1]=new stock("C#","Bill Gates",13);
a[2]=new stock("Delphi","Boxer",14);
a[3]=new stock("Oracle","zy",15);*/
System.out.println("plz input info:");
String ab,b;
int c;
ab=InputDate.getString();
b=InputDate.getString();
c=InputDate.getInt();
int i=0;
while(a[i]!=null)i++;
a[i]=new stock(ab,b,c);
System.out.println("successfully added");
process a1=new process();
a1.step1();
    }
}
class main
{
public static void main(String args[])
{
process a1=new process();
a1.step1();
}
}

解决方案 »

  1.   

    樓主把注釋去掉不就能運行了嗎.你拷一下看看:import java.io.*;
    import java.util.*;class stock
    {
    String name;
    String author;
    int price; stock()
    {
    System.out.println("沒有參數的構造函數");
    } stock(String a,String b,int c)
    {
    name=a;
    author=b;
    price=c;
    } void print()
    {
    System.out.println("name is "+name);
    System.out.println("author is "+author);
    System.out.println("price is "+price);
    } void get()
    {
    int N=100;
    stock []a=new stock[N];
    a[0]=new stock("Java","PlayGrrrr...",12);
    a[1]=new stock("C#","Bill Gates",13);
    a[2]=new stock("Delphi","Boxer",14);
    a[3]=new stock("Oracle","zy",15);
    }
    }
    class process
    {
    void step1()
    {
    System.out.println("do what?");
    System.out.println("a.search    b.add   c.exit");
    char ch=0; try
    {
    ch=(char)System.in.read();
    System.in.skip(2);
    }catch(IOException z){} if(ch=='a')
    {
    System.out.println("a.by name    b.by author");
    char ch2=0; try
    {
    ch2=(char)System.in.read();
    System.in.skip(2);
    }catch(IOException z){} if(ch2=='a')
    {
    System.out.print("plz input the book's name:");
    InputDate fuck=new InputDate();
    fuck.findbookname();
    } if(ch2=='b')
    {
    System.out.print("plz input the author's name:");
    InputDate fuck2=new InputDate();
    fuck2.findauthorname();
    }
    } if(ch=='b')
    {
    InputDate fuck3=new InputDate();
    fuck3.add();
    } if(ch=='c')
    {
    System.out.println("c u");
    }
    }
    }class InputDate 
    {
    static private String s; static public void input()
    {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    try{
    s=br.readLine();
    }catch(IOException e){}
    } static public String getString()
    {
    input();
    return s;
    }

    static public int getInt()
    {
    input();
    return Integer.parseInt(s);
    } public void findbookname()
    {

    int N=100;
    stock []a=new stock[N];
    a[0]=new stock("Java","PlayGrrrr...",12);
    a[1]=new stock("C#","Bill Gates",13);
    a[2]=new stock("Delphi","Boxer",14);
    a[3]=new stock("Oracle","zy",15);
    //stock mis = new stock();
    //mis.get();
    s=InputDate.getString();
    Vector names=new Vector(); for(int i=0;i<a.length&&(a[i+1]!=null);i++)
    {
    names.add(a[i].name);
    } if(names.indexOf(s)!=-1)
    {
    System.out.println("u can fint it here!");
    }
    else
    System.out.println("sorry,there is not such a book!");

    }    public void findauthorname()
    {
    s=InputDate.getString();
        
    int N=100;
    stock []a=new stock[N];
    a[0]=new stock("Java","PlayGrrrr...",12);
    a[1]=new stock("C#","Bill Gates",13);
    a[2]=new stock("Delphi","Boxer",14);
    a[3]=new stock("Oracle","zy",15);
        
    Vector authornames=new Vector();
    for(int i=0;i<a.length&&a[i+1]!=null;i++)
    {authornames.add(a[i].author);}

    if(authornames.indexOf(s)!=-1)
    {    
    System.out.println("there is such a book!");}
    else
    System.out.println("i didn't hear that pl at all!");
    }    public void add()
        {
    int N=100;
    stock []a=new stock[N];
    a[0]=new stock("Java","PlayGrrrr...",12);
    a[1]=new stock("C#","Bill Gates",13);
    a[2]=new stock("Delphi","Boxer",14);
    a[3]=new stock("Oracle","zy",15);
    System.out.println("plz input info:");
    String ab,b;
    int c;
    ab=InputDate.getString();
    b=InputDate.getString();
    c=InputDate.getInt();
    int i=0; while(a[i]!=null)i++;
    a[i]=new stock(ab,b,c);
    System.out.println("successfully added");
    process a1=new process();
    a1.step1();
    }
    }public class Tain
    {
    public static void main(String args[])
    {
    process a1=new process();
    a1.step1();
    }
    }
      

  2.   

    LZ错误地方和原因帮你标出,建议多看下书
    package huangwei;
    import java.io.*;
    import java.util.*;
     class stock
    {
    String name;
    String author;
    int price;
    stock(String a,String b,int c)
    {
    name=a;
    author=b;
    price=c;
    }
    void print()
    {
    System.out.println("name is "+name);
    System.out.println("author is "+author);
    System.out.println("price is "+price);
    }
    void get()
    {
    int N=100;
    stock []a=new stock[N];
    a[0]=new stock("Java","PlayGrrrr...",12);
    a[1]=new stock("C#","Bill Gates",13);
    a[2]=new stock("Delphi","Boxer",14);
    a[3]=new stock("Oracle","zy",15);
    }
    }
    class process
    {
    void step1()
    {
    System.out.println("do what?");
    System.out.println("a.search    b.add   c.exit");
    char ch=0;
    try
    {
    ch=(char)System.in.read();
    System.in.skip(2);
    }catch(IOException z){}
    if(ch=='a')
    {
    System.out.println("a.by name    b.by author");char ch2=0;
    try
    {
    ch2=(char)System.in.read();
    System.in.skip(2);
    }catch(IOException z){}
    if(ch2=='a')
    {
        System.out.print("plz input the book's name:");
        InputDate fuck=new InputDate();
        fuck.findbookname();
    }if(ch2=='b')
    {
    System.out.print("plz input the author's name:");
    InputDate fuck2=new InputDate();
    fuck2.findauthorname();
    }
    }
    if(ch=='b')
    {
    InputDate fuck3=new InputDate();
    fuck3.add();
    }
    if(ch=='c')
    {
    System.out.println("c u");
    }
    }
    }
    class InputDate 
    {
    static private String s;
    static public void input()
    {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    try{
    s=br.readLine();
    }catch(IOException e){}}
    static public String getString()
    {
    input();
    return s;}
    static public int getInt()
    {
    input();
    return Integer.parseInt(s);
    }
    public void findbookname()
    {int N=100;
    stock []a=new stock[N];
    a[0]=new stock("Java","PlayGrrrr...",12);
    a[1]=new stock("C#","Bill Gates",13);
    a[2]=new stock("Delphi","Boxer",14);
    a[3]=new stock("Oracle","zy",15);
       stock mis=new stock("Think in JAVA","LXX",100);
       //stock中的构造方法的参数是3个,你却直接不加参
       //每个类都有一个默认的无参的构造方法,当你建立新的构造方法时,原先默认的无参方法就没了
        // stock mis=new stock();
        mis.get();
        s=InputDate.getString();
        Vector names=new Vector();
        for(int i=0;i<a.length&&a[i+1]!=null;i++)
        {
        names.add(a[i].name);
        }
        if(names.indexOf(s)!=-1)
        {
        System.out.println("u can fint it here!");
        }
        else
        System.out.println("sorry,there is not such a book!");}
        public void findauthorname()
    {
        s=InputDate.getString();   int N=100;
    stock []a=new stock[N];
    a[0]=new stock("Java","PlayGrrrr...",12);
    a[1]=new stock("C#","Bill Gates",13);
    a[2]=new stock("Delphi","Boxer",14);
    a[3]=new stock("Oracle","zy",15);
        Vector authornames=new Vector();
        for(int i=0;i<a.length&&a[i+1]!=null;i++)
        {authornames.add(a[i].author);}
        if(authornames.indexOf(s)!=-1)
        {    System.out.println("there is such a book!");}
        else
        System.out.println("i didn't hear that pl at all!");    }
        public void add()
        {
        int N=100;
    stock []a=new stock[N];
    a[0]=new stock("Java","PlayGrrrr...",12);
    a[1]=new stock("C#","Bill Gates",13);
    a[2]=new stock("Delphi","Boxer",14);
    a[3]=new stock("Oracle","zy",15);
    System.out.println("plz input info:");
    String ab,b;
    int c;
    ab=InputDate.getString();
    b=InputDate.getString();
    c=InputDate.getInt();
    int i=0;
    while(a[i]!=null)i++;
    a[i]=new stock(ab,b,c);
    System.out.println("successfully added");
    process a1=new process();
    a1.step1();
        }
    }
    class main
    {
    public static void main(String args[])
    {
    process a1=new process();
    a1.step1();
    }
    }
      

  3.   

    import java.io.*;
    import java.util.*;/**
     * 改动过的地方
     * 你这类用的是public 修饰符,
     * 如果你写的这么多类都是在同一.java原文件下,
     * 那么你的public要加在 你的入口类中(这里指的是你写的 main 类)
     */
    class stock { 
    String name; String author; int price;

    /**
     * 改动过的地方,你的stock类只有带参的构造函数
     * 但是你实例化时用的是空参构造,所以你要再写一个不带参的构造函数 
     */
    public stock(){} 

    stock(String a, String b, int c) {
    name = a;
    author = b;
    price = c;
    } void print() {
    System.out.println("name is " + name);
    System.out.println("author is " + author);
    System.out.println("price is " + price);
    }
    /*
     * 改动过的地方,你希望在 findbookname方法中 
     * 用到 stock 类的数组,你那种直接用的话,编译器无法认出
     * 
     * 改动方案,将此类写成返回值为 stock 类数组的方法。
     * */
    stock [] get() {
    int N = 100;
    stock[] a = new stock[N];
    a[0] = new stock("Java", "PlayGrrrr...", 12);
    a[1] = new stock("C#", "Bill Gates", 13);
    a[2] = new stock("Delphi", "Boxer", 14);
    a[3] = new stock("Oracle", "zy", 15);

    return a;
    }
    }class process {
    void step1() {
    System.out.println("do what?");
    System.out.println("a.search    b.add   c.exit");
    char ch = 0;
    try {
    ch = (char) System.in.read();
    System.in.skip(2);
    } catch (IOException z) {
    }
    if (ch == 'a') {
    System.out.println("a.by name    b.by author"); char ch2 = 0;
    try {
    ch2 = (char) System.in.read();
    System.in.skip(2);
    } catch (IOException z) {
    }
    if (ch2 == 'a') {
    System.out.print("plz input the book's name:");
    InputDate fuck = new InputDate();
    fuck.findbookname();
    } if (ch2 == 'b') {
    System.out.print("plz input the author's name:");
    InputDate fuck2 = new InputDate();
    fuck2.findauthorname();
    }
    }
    if (ch == 'b') {
    InputDate fuck3 = new InputDate();
    fuck3.add();
    }
    if (ch == 'c') {
    System.out.println("c u");
    }
    }
    }class InputDate {
    static private String s; static public void input() {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try {
    s = br.readLine();
    } catch (IOException e) {
    } } static public String getString() {
    input();
    return s; } static public int getInt() {
    input();
    return Integer.parseInt(s);
    } public void findbookname() { stock mis = new stock(); 
    stock a [] = mis.get();  // 这里就用到了上面改动过的 get方法
    s = InputDate.getString();
    Vector names = new Vector();
    for (int i = 0; i < a.length && a[i + 1] != null; i++) {
    names.add(a[i].name);
    }
    if (names.indexOf(s) != -1) {
    System.out.println("u can fint it here!");
    } else
    System.out.println("sorry,there is not such a book!"); } public void findauthorname() {
    s = InputDate.getString();
    /* 你用到 a , 但是却没提供,如果你要多次用到,
     * 建议你将 stock 类数组(这里指 a ,和上面的findbookname方法 a以及下面的add方法中的a)
     * 作为你此类的一个属性,在构造函数初始化,
     * 或者在声明属性时赋值 */
    stock mis = new stock(); 
    stock a [] = mis.get();
    Vector authornames = new Vector();
    for (int i = 0; i < a.length && a[i + 1] != null; i++) {
    authornames.add(a[i].author);
    }
    if (authornames.indexOf(s) != -1) { System.out.println("there is such a book!");
    } else
    System.out.println("i didn't hear that pl at all!"); } public void add() {

    System.out.println("plz input info:");
    String ab, b;
    int c;
    ab = InputDate.getString();
    b = InputDate.getString();
    c = InputDate.getInt();
    int i = 0;

    /*
     * 改动过的地方,原因同上
     * */
    stock mis = new stock(); 
    stock a [] = mis.get();

    while (a[i] != null)
    i++;
    a[i] = new stock(ab, b, c);
    System.out.println("successfully added");
    process a1 = new process();
    a1.step1();
    }
    }/*
     * 改动过的地方 , 加了 public 修饰符.原因见上
     * */
    public class main {
    public static void main(String args[]) {
    process a1 = new process();
    a1.step1();
    }
    }
      

  4.   

    谢谢各位大侠,貌似shenglijay(探索者) 弄明白了我想要问什么,回答得最好。西西。谢谢大家,祝大家新年好运。
      

  5.   

    ---------- javac ----------
    注意: main.java 使用了未经检查或不安全的操作。
    注意: 要了解详细信息,请使用 -Xlint:unchecked 重新编译。输出完成 (耗时 1 秒) - 正常终止
    为什么编译的时候会有这样的情况??
    是那里有问题了!~
      

  6.   

    按照shenglijay(探索者)的改法,我新加入的书在查询是查不到的,就是我的加书功能并没有实现还是怎么回事,目前正在调试中。
      

  7.   

    我汗啊,几天没上csdn,就让人说了一顿。你新加的书查不到,是你的逻辑问题啊。你很多地方都是重新实例化然后在操作,你所引用的对象都不是同一个啊.......有多处都是这样哦!
      我按照你的需求在你原来上面改动了一下。我把代码给你,自己看看吧import java.io.*;
    import java.util.*;class stock {

    private String name; private String author; private int price;

    // 所有书籍
    private ArrayList books = new ArrayList() ;

    public stock() {
    // 初始化时添加书
    this.books.add(new stock("Java", "PlayGrrrr...", 12));
    this.books.add(new stock("C#", "Bill Gates", 13));
    this.books.add(new stock("Delphi", "Boxer", 14));
    this.books.add(new stock("Oracle", "zy", 15));
    } public stock(String a, String b, int c) {
    name = a;
    author = b;
    price = c;
    }

    /* 为所有属性公开getter和setter  */
    public void print() {
    System.out.println("name is " + name);
    System.out.println("author is " + author);
    System.out.println("price is " + price);
    } public String getAuthor() {
    return author;
    } public void setAuthor(String author) {
    this.author = author;
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public int getPrice() {
    return price;
    } public void setPrice(int price) {
    this.price = price;
    } public ArrayList getBooks() {
    return books;
    } public void setBooks(ArrayList books) {
    this.books = books;
    }
    }class process {
    private InputDate fuck = new InputDate(); // 操作同一个对象
    void step1() {
    BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
    String res = "";
    char c[]={};
    try {
    do
    {
    System.out.println("do what?");
    System.out.println("1.search    2.add   3.exit");
    int flag = Integer.parseInt(read.readLine());
    switch(flag)
    {
    case 1:
    System.out.println("a.by name    b.by author");
    char ch2 = 0;
    try {
    ch2 = (char) System.in.read();
    System.in.skip(2);
    } catch (IOException z) {
    }
    if (ch2 == 'a') {
    System.out.print("plz input the book's name:");
    fuck.findbookname();
    } if (ch2 == 'b') {
    System.out.print("plz input the author's name:");
    fuck.findauthorname();
    }
    break;
    case 2:
    fuck.add();
    break;
    case 3:
    System.out.println("c u");
    break;
    default:
    System.out.println("your input has false!");
    break;
    }
    System.out.println("again?y/n");
    res = read.readLine();
    c = res.toCharArray();
    }
    while(c[0] =='Y' || c[0] =='y');
    System.out.println("Welcome Next!");
    } catch (NumberFormatException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }class InputDate {

    private stock mis = new stock(); // 操作同一对象

    static private String s;
    static public void input() {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try {
    s = br.readLine();
    } catch (IOException e) {
    }
    } static public String getString() {
    input();
    return s; } static public int getInt() {
    input();
    return Integer.parseInt(s);
    }
    /**
     * 按名称查找书籍
     * 检索stock类的所有书籍books
     */
    public void findbookname() {

    s = InputDate.getString();

    ArrayList books = mis.getBooks(); 

    for(Iterator iter = books.iterator();iter.hasNext();)
    {
    if(((stock)iter.next()).getName().equals(s))
    {
    System.out.println("u can fint it here!");
    return;
    }
    }
    System.out.println("sorry,there is not such a book!");
    }
      // 按作者查找书籍
    public void findauthorname() {

    s = InputDate.getString();

    ArrayList books = mis.getBooks(); 

    for(Iterator iter = books.iterator();iter.hasNext();)
    {
    if(((stock)iter.next()).getAuthor().equals(s))
    {
    System.out.println("u can fint it here!");
    return;
    }
    }
    System.out.println("sorry,there is not such a book!"); }
    // 添加书籍
    public void add() { System.out.println("plz input info:");
    String ab, b;
    int c;
    ab = InputDate.getString();
    b = InputDate.getString();
    c = InputDate.getInt();

    ArrayList books = mis.getBooks();
    books.add(new stock(ab,b,c));
    System.out.println("successfully added");
    }
    }public class main {
    public static void main(String args[]) {
    process a1 = new process();
    a1.step1();
    }
    }