所以我写了一个程序,它创建一个红黑树并确定树中红色节点的百分比。现在我正在制作它的主要方法。所以这就是我现在所拥有的:
public static void main(String[] args) {
Scanner s;
if (args.length > 0){
try{
s = new Scanner(new File(args[0]));
} catch(java.io.FileNotFoundException e){
System.out.printf("Unable to open %s\n",args[0]);
return;
}
System.out.printf("Reading input values from %s.\n",args[0]);
} else {
s = new Scanner(System.in);
System.out.printf("Enter a list of non-negative integers. Enter a negative value to end the list.\n");
}
RedBlackBST<String, Integer> st = new RedBlackBST<String, Integer>();
int i = 0;
while ((s.hasNextInt())){
int key = s.nextInt();
st.put(key, i);
i++;
}
double percent = percentRed();
System.out.println("There are " + redcount + " red nodes so");
System.out.println(percent + "% of the nodes are red");
}
我尝试做的是根据整数文件创建一个树(如果用户通过键入&#34运行程序; java RedBlackBST test10.txt&#34;这将包含10个值插入到树中)或者如果用户没有指定文件,那么它将提示用户键入他自己的值并在结尾处放置负值以结束列表。现在输入你自己的值不起作用,但是如果你传入.txt数字文件,那么它的工作方式与预期完全一致。现在至于输入你自己的值,我想改变while循环看起来像这样:
while ((s.hasNextInt()) && (s.nextInt()) >= 0){
所以这应该是通过值列表,如果你在列表中找到负值,那么它将停止读取值。这个问题是由于某种原因(即使我传入一个文件)它只读取整数中任何数组的一半值。那么如何更改while循环现在使程序只读取了一半的数组值?
我调用的put
方法也就是将值插入树中的insert方法。
答案 0 :(得分:1)
假设你确实做了你提到的精确改变,你的循环最终会看起来像:
import pygame
import random
display_height = 600
display_width = 1000
dis_screen = pygame.display.set_mode((display_width, display_height))
FPS = 30
clock = pygame.time.Clock()
img = pygame.image.load('foo.png')
crouchimg = pygame.image.load('crouchimg.png')
# Simple player object
class Player(object):
def __init__(self, x, y, image):
self.x = x
self.y = y
self.image = image
# Method to draw object
def draw(self):
dis_screen.blit(self.image, (self.x, self.y))
# Method to move object
def move(self, speedx, speedy):
self.x += speedx
self.y += speedy
class MainRun(object):
def __init__(self, displayw, displayh):
self.dw = displayw
self.dh = displayh
self.Main()
def Main(self):
# Put all variables up here
stopped = False
x_move = 0
y_move = 0
p1_y_loc = 200
p1_x_loc = 200
x = pygame.Rect().x
greg = Player(p1_x_loc, p1_y_loc, img)
# Main Loop
while not stopped:
print(x)
dis_screen.fill((255, 255, 255)) # Tuple for filling display... Current is white
# Event Tasking
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
y_move = 0
x_move = 5
elif event.key == pygame.K_LEFT:
y_move = 0
x_move = -5
elif event.key == pygame.K_UP:
y_move = -5
x_move = 0
elif event.key == pygame.K_DOWN:
p1_y_loc = 300
p1_x_loc = 0
greg = Player(p1_x_loc, p1_y_loc, crouchimg)
if event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN:
p1_y_loc = 200
greg = Player(p1_x_loc, p1_y_loc, img)
if event.key == pygame.K_UP:
y_move = 0
if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
x_move = 0
greg.move(x_move, y_move)
greg.draw()
pygame.display.update()
clock.tick(FPS)
pygame.quit()
quit()
run = MainRun(display_width, display_height)
run.Main()
每次迭代调用while ((s.hasNextInt()) && (s.nextInt()) >= 0){
int key = s.nextInt();
st.put(key, i);
i++;
}
两次,当然会跳过其他每个值,因为nextInt()
会消耗输入。
这里的一种典型方法是在循环外声明nextInt()
,以便在条件范围内使它可用,然后一次性分配和测试它,如:
key
因此,每次迭代一次int key;
while ((s.hasNextInt()) && (key = s.nextInt()) >= 0){ // <- key is assigned *and* tested
st.put(key, i);
i++;
}
。