import java.util.*;
class RectangeDemo
{
Scanner sc = new Scanner(System.in);
public static void main(String[] args)
{
System.out.println("请输入矩形行数:");//获取矩形行数
int m = sc.nextInt();
System.out.println("请输入矩形列数:");//获取矩形列数
int n = sc.nextInt();
rec(5,6); //调用函数实现矩形的输出
} public static void rec(int x,int y)//实现矩形输出
{
for (int a=0;a<x ;a++ )
{
for (int b=0;b<y ;b++ )
{
System.out.print("*");
}
System.out.println();
}
}
}
class RectangeDemo
{
Scanner sc = new Scanner(System.in);
public static void main(String[] args)
{
System.out.println("请输入矩形行数:");//获取矩形行数
int m = sc.nextInt();
System.out.println("请输入矩形列数:");//获取矩形列数
int n = sc.nextInt();
rec(5,6); //调用函数实现矩形的输出
} public static void rec(int x,int y)//实现矩形输出
{
for (int a=0;a<x ;a++ )
{
for (int b=0;b<y ;b++ )
{
System.out.print("*");
}
System.out.println();
}
}
}
忘了改回来了
static Scanner sc = new Scanner(System.in);
而你在静态方法(main)中使用了sc
sc不是静态变量所以编译错误!
只需要在 “Scanner sc = new Scanner(System.in);”前面加static修饰即可
“static Scanner sc = new Scanner(System.in);”
1.Scanner前面加static
2.把Scanner sc这一句移到main里面
import java.util.*;public class test7 { public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入矩形行数:");// 获取矩形行数
int m = sc.nextInt();
System.out.println("请输入矩形列数:");// 获取矩形列数
int n = sc.nextInt();
rec(m, n); // 调用函数实现矩形的输出
} public static void rec(int x, int y)// 实现矩形输出
{
for (int a = 0; a < x; a++) {
for (int b = 0; b < y; b++) {
System.out.print("*");
}
System.out.println();
}
}
}