0

我有一个正在尝试运行的程序,但我不知道如何结束扫描仪输入。在作业中,我们有这个注释,我们可以简单地按 Windows 的 (ctrl+z) 和 linux 的 (ctrl+d) 但我使用的是 mac,我已经尝试过但没有用。我也尝试过 (cmd +d ) 和 (cmd+z) 但没有用。这是我的程序:

package F1;

import java.io.PrintStream;
import java.util.Scanner;



public class HatzelKlatzer{
public static final int STARTING_YEAR=1950,
                           FINAL_YEAR=2050;
PrintStream out;

HatzelKlatzer(){
    out=new PrintStream(System.out);
}

//method which will print the percentage
void printPercentage(double percentage){
    out.printf("The percentage is: %d",percentage);
}


//TODO Make a method which will read the input if its in range and returns the result
int readInRange(Scanner input,int startDate,int endDate){
    int result=input.nextInt();

    if(result>endDate || result<startDate){
        System.exit(1);
    }

    return result;
    }



//TODO method if month is odd
Boolean oddMonth(Scanner input){

    readInRange(input,1,31);

    int month=readInRange(input,1,12);

    readInRange(input,STARTING_YEAR,FINAL_YEAR);



    return month%2 !=0;
}

void start()
{
    Scanner in=new Scanner(System.in);
    int seizures=0,
    numberInOddMonths=0;

    //while loop to count the seizures and months odd
    while(in.hasNext()){
        String date=in.nextLine();

        Scanner dateReader=new Scanner(date);

        if(oddMonth(dateReader)){
            numberInOddMonths +=1;
        }
        seizures +=1;
    }

    //calculating the percentage
    double percentage=((double)numberInOddMonths/seizures)*100 ;
    printPercentage(percentage);
}

public static void main(String[] args){
    new HatzelKlatzer().start();

}
}

它需要像这样的输入

12 1 2005
1 1 1995
....etc

但我不知道如何让扫描仪知道这是输入的结尾。谢谢你,请指导我怎么做......我只是一个初学者。

谢谢

猫。

4

0 回答 0