简体   繁体   English

如何让我的正整数变为负数并保持负数直到我的行动结束?

[英]How do I make my positive integer become negative and stay negative until the end of my action?

I'm trying to test out removing an image from the screen by converting its x and y values into negative values (my screen is a 500 by 500 and cannot be resized) and then returning it to the screen in the next slide by converting the x and y values back into positives so I can then convert them back to negative to remove them again. 我试图通过将其x和y值转换为负值(我的屏幕是500 x 500并且无法调整大小)来测试从屏幕中删除图像,然后通过转换将其返回到下一张幻灯片中的屏幕x和y值重新变为正数,因此我可以将它们转换回负值以再次删除它们。

In one class (which shows a red pointer on the screen that I can move to the boundaries of other items) I have this: 在一个类(在屏幕上显示一个红色指针,我可以移动到其他项目的边界)我有这个:

import java.awt.Rectangle;
public void checkBoundariesTouching() {
    NextSlide next = MainFrame.getSlide();

    for (int i = 0; i < 1; i++) {
        if (getBounds().intersects(next.getBounds())) {
            MainFrame.slide += 1;

In my MainFrame, this slide increase changes the background to make my slides interesting and changes the writing shown. 在我的MainFrame中,此幻灯片增加会更改背景以使我的幻灯片变得有趣并更改显示的书写。

I have another class that produces black rectangles that I remove one by one to uncover a bullet point of writing that I then explain. 我有另一个产生黑色矩形的类,我逐个删除它以揭示我写下的一个要点。 I remove the rectangles like this: 我删除这样的矩形:

import java.awt.Rectangle;
public class BlackRectangle extends Entity {

public BlackRectangle(int x, int y) {
    super(x, y);

}    
public void update() {
    int slide = MainFrame.getSlide();
    checkBoundariesTouching();
    if (slide >= 2) {
        if (x < 0) {
            x = -x;
        }
        if (y < 0) {
            y = -y;
        }
        checkBoundariesTouching();
    }
}

public void checkBoundariesTouching() {

    Pointer pointer = MainFrame.getPointer();
    NextSlide next = MainFrame.getSlide();
    if (getBounds().intersects(pointer.getBounds())) {
        x = -x;
        y = -y;


    }

When the slide is equal to 1, I can remove the black squares without problem. 当幻灯片等于1时,我可以毫无问题地删除黑色方块。 However, when I have increased to the next slide (from slide 2 onwards) when the pointer is touching a black rectangle, it is moved off screeen but once I move the pointer away from it, the black rectangle returns and covers the information. 但是,当我已经增加到下一张幻灯片时(从幻灯片2开始),当指针触摸黑色矩形时,它会被屏幕移开,但是一旦我将指针移离它,黑色矩形将返回并覆盖信息。 Is there a way I can fix this code as it stands only changing the update method? 有没有办法可以修复此代码,因为它只是更改更新方法?

I admit I do not know very much Java at all and started two days ago but I am trying to use Java code for my presentation to prove that I can grasp basic concepts of a program in a short space of time and build on it as I go along (I need to prove this to get into my course at uni that has nothing to do with Computer Science.) 我承认我根本不太了解Java并且两天前就开始了,但我正在尝试使用Java代码进行演示,以证明我可以在很短的时间内掌握程序的基本概念并在此基础上构建它(我需要证明这一点,以便进入与计算机科学无关的大学课程。)

I would seriously counsel you NOT to do this. 我会认真地劝告你不要这样做。

If the prerequisites for the course are that you have a basic grasp of programming ... and you don't have that basic grasp ... then you are going to get into real problems down the track. 如果课程的先决条件是您已经掌握了编程的基本知识......并且您没有基本的掌握......那么您将在轨道上遇到真正的问题。 Especially, since if this is not a CS course, they are UNLIKELY to help you out if you run into problems due to poor programming skills. 特别是,因为如果这不是一门CS课程,如果你因为编程技巧不好而遇到问题,他们就不能帮助你。

OTOH, if the problem is that you are competent at (say) C# and not at Java, then the right solution is to fork out the money for a developer license. OTOH,如果问题是你能胜任(比方说)C#而不是Java,那么正确的解决方案是为开发人员许可付出代价。 'Cos you are going to need it in order to do the development work as part of your course ... or in the future. “因为你需要它才能将开发工作作为课程的一部分......或将来。

(But surely you can find a cheaper way of getting a Visual Studio license. I can get "Visual Studio Professional 2012 with MSDN" from the MS Store for less than $1.2k AUD ... with no student discount.) (但你肯定能找到一种获得Visual Studio许可证的更便宜的方法。我可以从MS Store获得“Visual Studio Professional 2012 with MSDN”,价格低于$ 1.2k澳元......没有学生折扣。)


I acknowledge your point that they are not specifically testing your Java competence. 我承认你的观点是他们没有专门测试你的Java能力。 They are, however testing, you on your general competence as a programmer. 然而,他们正在测试您作为程序员的一般能力。 And surely that includes your ability to choose the right tool for the job ... which includes not trying to do some programming to a tight deadline in a programming language that you are not familiar with!! 当然,这包括你为工作选择合适工具的能力......其中包括不尝试在你不熟悉的编程语言的紧迫期限内做一些编程!

If the code works fine when slide = 1, I would suggest removing this: 如果在slide = 1时代码工作正常,我建议删除它:

if (slide >= 2) {
    if (x < 0) {
        x = -x;
    }
    if (y < 0) {
        y = -y;
    }
    checkBoundariesTouching();
}

If you need that code to move the boxes back on the screen, you can put the if statements inside a separate method and call that method when you change slides. 如果需要该代码在屏幕上移回框,则可以将if语句放在单独的方法中,并在更改幻灯片时调用该方法。

Check for an additional condition within the update method. 检查更新方法中的其他条件。 The method must look like this one below:- 该方法必须如下所示: -

        public void update() {
            int slide = MainFrame.getSlide();
            checkBoundariesTouching();
            if (slide >= 2) {
                if (x < 0) {
                    x = x*-1;
                }
                if (y < 0) {
                    y = y*-1;
                }


            }
        }


public void checkBoundariesTouching() {

    Pointer pointer = MainFrame.getPointer();
    NextSlide next = MainFrame.getSlide();
    if (getBounds().intersects(pointer.getBounds())) {
    if(x>0){
        x = -x;
           }
    if(y>0){
        y = -y;
           }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM