待修改的java程序
麻烦各位帮我看看这个程序到底错在什么地方,具体思路是从键盘接收一个int型的数赋给num,p1调用jiech方法计算,代码如下:
import java.io.*;
public class People {
public static void main(String args[]){
int num;
try{
System.out.print("请输入一个数:");
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
num=Integer.parseInt(input.readLine());
}catch(IOException e){}
People p1=new People();
p1.jiech(num);
}
public void jiech(int i){
for(int j=1;j<=i;j++){
for(int n=1;n<=j;n++){
System.out.print(n+"*"+j+"="+n*j+" ");
}
System.out.println();
}
}
}
[解决办法]
用Scanner实现吧,这个类更合适
import java.io.*;import java.util.Scanner;public class People { public static void main(String args[]){ int num=0; try{ System.out.print("请输入一个数:");// BufferedReader input=new BufferedReader(new InputStreamReader(System.in));// num=Integer.parseInt(input.readLine()); Scanner scanner=new Scanner(System.in); num=scanner.nextInt(); }catch(Exception e){ e.printStackTrace(); } People p1=new People(); p1.jiech(num); } public void jiech(int i){ for(int j=1;j<=i;j++){ for(int n=1;n<=j;n++){ System.out.print(n+"*"+j+"="+n*j+" "); } System.out.println(); } }}
[解决办法]