Java 通过扫描程序声明变量?

Java 通过扫描程序声明变量?,java,Java,我试图将我的int“n”设置为由用户输入定义。然而,它从来没有设置,我不知道什么是错的。我不太擅长java,这是我的家庭作业。我认为我的问题很基本,但我被卡住了。 所以,重申我的问题。为什么我不能将int n设置为用户输入?“n”问题不是实际的家庭作业,但为了让我的家庭作业正常工作,必须设置“n” package printer.java; import java.util.Queue; import java.util.LinkedList; import java.util.Timer;

我试图将我的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();
    }
}
package printer.java;
导入java.util.Queue;
导入java.util.LinkedList;
导入java.util.Timer;
导入java.util.TimerTask;
导入java.util.Random;
导入java.util.Scanner;
公共类PrinterJava{
//需要声明ints
整数计数=0;
int jobCount=0;
int done=0;
整数时间=0;
int jobTimerDelay=1000;
int jobTimerPeriod=1000;
int TIMERDELAY=1000;
int TIMERPERIOD=n*60*1000;
//声明需要计时器
Timer jobTimerTimer=新计时器();
定时器定时器定时器=新定时器();
//这是一个计时器,应该每5秒创建一个新的“页面”。
//页面必须是随机的“大小在1到5页之间”
公开作废作业计时器(){
jobTimerTimer.scheduleAtFixedRate(新TimerTask(){
@凌驾
公开募捐{
计数++;
随机dom=新随机();
int p=dom.nextInt(5)+1;
如果(计数%5==0){
pages page=new pages();//每5秒创建一个新页面
page.pages=p;
jobCount++;
jobQueue.offer(page);//将新创建的页面推送到队列中
System.out.println(“已创建新作业!作业队列大小:+jobQueue.size());
System.out.println(“创建的作业总数:“+jobCount”);
}如果(!jobQueue.isEmpty()&&count>2&&count%2==0),则为else{
完成++;
jobQueue.remove();
System.out.println(“作业打印成功!打印的作业总数:“+完成”);
}
}
},jobTimerDelay,jobTimerPeriod);
}
//这是保存页面的队列
Queue jobQueue=new LinkedList();
公共类页面{//pages
整版;
//建造师
公共网页(){
}
公共页面(整数){
this.pages=NumPages;
}
}
公共无效时间wo(){
timertimer.scheduleAtFixedRate(新的TimerTask(){
@凌驾
公开募捐{
PrinterJava runOne=new PrinterJava();//创建我的页面创建者的实例
runOne.jobTimer();
System.out.println(“请将运行时间以分钟为单位输入为整数:”;
扫描仪扫描=新扫描仪(System.in);
int n=scan.nextInt();
}
},TimerDelay,TimerPeriod);
}
公共静态void main(字符串[]args){
PrinterJava runTwo=new PrinterJava();//创建我的页面创建者的实例
runTwo.timerTwo();
}
}

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


如果在
int n=scan.nextInt()行之后设置一个断点会怎么样
并查看是否在那里设置了它(或者,您可以使用
System.out.println()
将其打印出来。

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

int timeTimerPeriod = n * 60 * 1000;
但是,如果这样做的话,也不会像预期的那样起作用:

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

因为<代码> n>代码>定义在代码> TimeTrase.Run()/Cux>方法中。

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);
}
int-timerperiod=60*1000;//实例变量
int n=0;
...
公共无效时间wo(){
System.out.println(“请将运行时间以分钟为单位输入为整数:”;
扫描仪扫描=新扫描仪(System.in);

n=scan.nextInt();//您有任何错误吗?您也设置了
n
,但您没有使用它。记住它的作用域是什么。在我声明变量的顶部,我将“时间段”设置为n*60*1000。谢谢您!几个小时后我自己解决了这个问题,但这正是我解决问题的方式(不客气!)我是个秘密的心灵感应者!哈哈@BrandonCook