大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();
}
}
谢谢,祝各位大侠新年好运~~~
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();
}
}
解决方案 »
- IO,咋运行不出来
- WebService的原理和过程是怎样的?
- 我们是大二开始学JAVA的,本来充满信心,但现在对未来感到不知所措,谁能指点下我们!
- 我的这个返回时间构造方法,返回的类形是什么啊????
- 请问这些文件在那个包里
- 获取对应的验证码图片,只要图片!!!
- 我的一种实现:把图片文件写进数据库并读取显示,欢迎批评指正。
- 如何将一个jdbc数据源绑定到JNDI树上。请教!谢谢!
- ------>>请教:如何实现jar文件加密?
- 如何在JAVA APPLET中显示messagebox对话框?
- 谁有漂亮的LookAndFeel第三方包,麻烦给小弟发个,或是给个下载地址! 谢谢啦!^_^
- 急问:eclipse3.2里开发的SWT程序怎么生成能部署的jar
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();
}
}
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();
}
}
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();
}
}
注意: main.java 使用了未经检查或不安全的操作。
注意: 要了解详细信息,请使用 -Xlint:unchecked 重新编译。输出完成 (耗时 1 秒) - 正常终止
为什么编译的时候会有这样的情况??
是那里有问题了!~
我按照你的需求在你原来上面改动了一下。我把代码给你,自己看看吧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();
}
}