在main函数内只能调用类静态的方法。
所以要改成
public static void calculate (){
}
public static void input (int number){}表明是类的全局成员。
所以要改成
public static void calculate (){
}
public static void input (int number){}表明是类的全局成员。
调试欢乐多
public static void main(String [] args)
{
test t=new test();
t.input();
t.calculate();
}
{
private int number,n;
public void input (int number)
{
System.out.println("Please input a number:");
//this.number = System.in.read();
this.number = number;
}
public void calculate ()
{
n = number * number;
for(int i = 1;i <= n;i++)
{
if (n % i == 0)
System.out.println(i);
}
}
public static void main (String arg[])
{
test16 mytest = new test16();
mytest.input(8);
mytest.calculate();
}
}===========================================================
import java.io.*;public class test16
{
private int number,n;
public void input ()
{
System.out.println("Please input a number:");
this.number = System.in.read();
//this.number = number;
}
public void calculate ()
{
n = number * number;
for(int i = 1;i <= n;i++)
{
if (n % i == 0)
System.out.println(i);
}
}
public static void main (String arg[]) throws IOException
{
test16 mytest = new test16();
mytest.input();
mytest.calculate();
}
}第一个程序可以运行,
第二个不可以,错误是:
test16.java:10: unreported exception java.io.IOException; must be caught or declared to be thrown this.number = System.in.read();
含义是为了测试这个类`你里面没有这个类的对象`怎么可以运行呢`
然后程序的IO操作还都得有TRYCATCH对吧?还有问题是NUMBER这些要改变的变量不能是STATIC的在MAIN中出现的话,方法还得用对像。变量……好烦啊JAVA那么多事~~用惯了C语言和C++的程序,写JAVA程序老错~~其实这些规矩我都看过了在书上,就是一写就忘
import java.io.*;public class test
{
private int number,n;
public void input ()
{
System.out.println("Please input a number:");
try{System.in.read();}
catch(Exception e){}
this.number = System.in.read();
}
public void calculate ()
{
n = number * number;
for(int i = 1;i <= n;i++)
{
if (n % i == 0)
System.out.println(i);
}
}
public static void main (String arg[]) throws IOException
{
test mytest = new test();
mytest.input();
mytest.calculate();
}
}
{
private int number,n;
public void input ()
{
System.out.println("Please input a number:");
try
{
System.in.read();
this.number = System.in.read();
}
catch(Exception e){System.out.println(e)}
}
public void calculate ()
{
n = number * number;
for(int i = 1;i <= n;i++)
{
if (n % i == 0)
System.out.println(i);
}
}
public static void main (String arg[]) throws IOException
{
test mytest = new test();
mytest.input();
mytest.calculate();
}
}可能产生异常的代码都要放到try{}里面
{
private int number,n;
public void input ()
{
System.out.println("Please input a number:");
try
{
System.in.read();
this.number = System.in.read();
}
catch(Exception e){System.out.println(e)}
}
public void calculate ()
{
n = number * number;
for(int i = 1;i <= n;i++)
{
if (n % i == 0)
System.out.println(i);
}
}
public static void main (String arg[]) throws IOException
{
test mytest = new test();
mytest.input();
mytest.calculate();
}
}
这样读的数字是他的ascii码,比如输入8它当作56来计算,要表达本来的意思应该怎么做呢?
read()函数用法不正确。
{
private int number,n;
public void input ()
{
System.out.print("Please input a number:");
try
{
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
this.number = Integer.parseInt( buf.readLine());
}
catch(Exception e){System.out.println(e);}
}
public void calculate ()
{
n = number * number;
for(int i = 1;i <= n;i++)
{
if (n % i == 0)
System.out.println(i);
}
}
public static void main (String arg[]) throws IOException
{
test mytest = new test();
mytest.input();
mytest.calculate();
}
}
{
test mt=new test();
mt.input(number);
mt.calculate();
}
}
非static类型的属性或方法必须要在实例化之后才能调用,static类型的则可以直接使用而不用实例化。这个是正解。