1

我正在尝试将我的 int "n" 设置为由用户输入定义。但是它从未设置过,我不确定出了什么问题。我不太擅长java,这是家庭作业。我认为我的问题是非常基本的,但我被卡住了。所以,重申我的问题。为什么我不能将我的 int n 设置为用户输入?“n”问题不是实际的作业,但为了让我的作业正常工作,必须设置“n”。

package printer.java;

import java.util.Queue;
import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Random;
import java.util.Scanner; 

public class PrinterJava {
// Declaring ints needed
int count = 0;
int jobCount = 0;
int done = 0;
int time = 0;
int jobTimerDelay = 1000;
int jobTimerPeriod = 1000;
int timeTimerDelay = 1000;
int timeTimerPeriod = n * 60 * 1000;

// declaring timers needed
    Timer jobTimerTimer = new Timer();
    Timer timeTimerTimer = new Timer();
// This is a timer that is supposed to create new "pages" every 5 seconds. 
//the pages have to be a random "size between 1 and 5 pages long"

public void jobTimer() {
    jobTimerTimer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            count++;
            Random dom = new Random();
            int p = dom.nextInt(5) + 1;
            if (count % 5 == 0) {
                pages page = new pages();  // Creates a new page every 5 seconds
                page.pages = p;
                jobCount++;
                    jobQueue.offer(page); // pushes the newly created pages into the queue
                    System.out.println("A new Job has been created! Job queue size: " + jobQueue.size());
                    System.out.println("Total Jobs created: " + jobCount);
                } else if (!jobQueue.isEmpty() && count > 2 && count % 2 == 0) {
                    done++;
                    jobQueue.remove();
                    System.out.println("Job printed successfully! total jobs printed: " + done);
                }
            }
        }, jobTimerDelay, jobTimerPeriod);


   }
    // this is the queue that holds the pages
    Queue<Object> jobQueue = new LinkedList<Object>();

    public class pages { // pages

        int pages;
        // constructor

        public pages() {
        }

        public pages(int NumPages) {
            this.pages = NumPages;
        }
    }

    public void timerTwo() {
timeTimerTimer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                PrinterJava runOne = new PrinterJava(); // creats an instance of my page creator
                runOne.jobTimer();
                System.out.println("Please Enter Run time in minutes as an integer: ");
                Scanner scan = new Scanner(System.in); 
                int n = scan.nextInt(); 
            }
        }, timeTimerDelay, timeTimerPeriod);


    }

    public static void main(String[] args) {
        PrinterJava runTwo = new PrinterJava(); // creats an instance of my page creator
        runTwo.timerTwo();
    }
}
4

2 回答 2

0

当您说int n您正在声明一个新变量时,因此在下一行之后您不再引用它。另外,我没有看到n声明为实例变量或其他任何地方。

如果你在该行之后设置一个断点int n = scan.nextInt();并查看它是否被设置在那里(或者你可以使用System.out.println()它来打印它。

于 2013-11-11T20:43:13.177 回答
0

此行不会编译,因为n尚未定义:

int timeTimerPeriod = n * 60 * 1000;

但是,如果它也不会按预期工作:

timeTimerTimer.scheduleAtFixedRate(new TimerTask() {...}, timeTimerDelay, timeTimerPeriod);

因为n是在方法内部定义的TimerTask.run()。要解决此问题,请考虑进行以下更改:

int timeTimerPeriod = 60 * 1000; // instance variable
int n = 0;

...

public void timerTwo() {
    System.out.println("Please Enter Run time in minutes as an integer: ");
    Scanner scan = new Scanner(System.in); 
    n = scan.nextInt(); // <-- read n here for first time

    timeTimerTimer.scheduleAtFixedRate(new TimerTask() {...}, timeTimerDelay, timeTimerPeriod * n);
}
于 2013-11-11T20:51:20.260 回答