Java 静态变量未递增且未绘制屏幕
该程序应该打印出代表屏幕分辨率的矩形点。输入屏幕分辨率后,点不会打印出来,静态变量screenCount也不会跟踪制作了多少屏幕。它只是停留在1Java 静态变量未递增且未绘制屏幕,java,Java,该程序应该打印出代表屏幕分辨率的矩形点。输入屏幕分辨率后,点不会打印出来,静态变量screenCount也不会跟踪制作了多少屏幕。它只是停留在1 public class Screen { private static int screenCount; private int height; private int width; public Screen() { screenCount = 0; height = 0; wid
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));
}
}
私有静态变量只能在该类中访问。尝试使用公共静态int Screencount可能会有所帮助。您的输入和输出是什么?首先,您应该设置newWidth,我将其设置为newWidth=1280,现在它不允许我输入宽度值。它打印错误宽度消息并要求另一个宽度值。请记住在输入高度后输入kb.nextLine。kb.nextInt不计算换行符。你在用myScreenWidth检查宽度的同时给myScreenHeight加上了宽度。啊,谢谢。我修正了这个问题。现在,这些点像矩形一样打印,但是printing Screen.getScreenCount每次只给我1。