该程序应该在一个代表屏幕分辨率的矩形中打印出点。输入屏幕分辨率后,点不会打印出来,静态变量screenCount不会跟踪制作的屏幕数量。它只停留在1.
public class Screen
{
private static int screenCount;
private int height;
private int width;
public Screen()
{
screenCount = 0;
height = 0;
width = 0;
}
public Screen(int newHeight, int newWidth)
{
height = newHeight;
width = newWidth;
}
public int getHeight()
{
return height;
}
public int getWidth()
{
return width;
}
public boolean setHeight(int newHeight)
{
if (newHeight <= 1080 && newHeight >= 720)
{
height = newHeight;
return true;
}
else
{
System.out.println("ERROR height");
return false;
}
}
public boolean setWidth(int newWidth)
{
if (newWidth <= 1920 && newWidth <= 1280)
{
width = newWidth;
return true;
}
else
{
System.out.println("ERROR width");
return false;
}
}
public void draw()
{
screenCount++;
for(int i = 0; i < height; i++)
{
for(int j = 1; j <= width; j++)
{
System.out.print(".");
}
System.out.println();
}
}
public static int getScreenCount()
{
return screenCount;
}
}
// Tester
import java.util.Scanner;
public class ScreenBuilder
{
public static void main(String[] args)
{
String again = "";
do
{
Screen myScreen = new Screen();
int myScreenHeight = 0;
int myScreenWidth = 0;
Scanner kb = new Scanner(System.in);
do
{
System.out.println("Enter desired height of screen (720 - 1080)");
myScreenHeight = kb.nextInt();
System.out.println();
}
while(!(myScreen.setHeight(myScreenHeight)));
System.out.println();
do
{
System.out.println("Enter desired width of screen (1280 - 1920)");
myScreenHeight = kb.nextInt();
System.out.println();
}
while(!(myScreen.setWidth(myScreenWidth)));
myScreen.draw();
System.out.println("There were " + Screen.getScreenCount() + " Screens Printed");
Scanner answer = new Scanner(System.in);
System.out.println("Would you like to build another screen (Y/N)?");
again = answer.nextLine();
}
while ("y".equalsIgnoreCase(again));
}
}
答案 0 :(得分:0)
私有静态变量只能在该类中访问,尝试使用public static int Screencount,它可能有帮助。