我正在尝试将我的 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();
}
}