简体   繁体   English

Java程序中的方法不会获取用户输入

[英]Method in Java program won't pick up user input

The program I'm writing is to determine whether or not a year is a leap year. 我写的程序是确定一年是否是闰年。 This is an assignment, so I am required to use the four methods I wrote inside the program. 这是一个赋值,所以我需要使用我在程序中编写的四种方法。 The program compiles and runs, it asks for user input at the appropriate places, but it doesn't take the input into the program. 程序编译并运行,它在适当的位置请求用户输入,但它不会将输入带入程序。 Also it's saying that the year is a leap year and looping no matter what has been inputted. 也就是说,无论输入什么,这一年都是闰年并且循环。 I've very confused as to where there error is, since this program seems match the examples we were given. 我对于哪里出现错误非常困惑,因为这个程序似乎与我们给出的例子相符。

import java.util.Scanner;

public class LeapYear {
    public static void main(String[] args) {
        boolean repeat;
        String computeanother, yes="yes";
        Scanner kb=new Scanner(System.in);
        int year = -1;
        boolean leap;

        do
        { 
            displayInstructions();
            getYear(year);
            leap = isLeap(year);
            displayResults(year, leap);
            System.out.println("Would you like to compute another year?");
            computeanother = kb.nextLine();

            if(computeanother.equals(yes))
                repeat=true;
            else 
                repeat=false;
        } while(repeat=true);
    }

    public static void displayInstructions()
    {
        System.out.println("This program is designed to predict whether or not a year is a leap year.");
        System.out.println("When prompted please enter a positive number for the year.");
        System.out.println("Once the program has run completely, it will state the year and whether it is a leap year.");
    }

    public static void getYear(int year)
    {
        Scanner kb = new Scanner(System.in);
        do {
            System.out.println("Please enter the year.");
            year=kb.nextInt();
        } while (year < 0);   
    }

    public static boolean isLeap(int year)
    {
        boolean leap;
        if ((year%4==0 && year%100 != 0) || year%400==0){
            leap = true;
            return true;
        } else {
            leap = false;   
            return false;
        }
    }

    public static void displayResults(int year, boolean leap)
    {
        if (leap = true) {
            System.out.println("The year " +year); 
            System.out.println("is a leap year.");
        } else {
            System.out.println("The year " +year); 
            System.out.println("is not a leap year.");
        }
    }

}

Thanks for everyone's help! 谢谢大家的帮助! The edited code looks like this: 编辑后的代码如下所示:

import java.util.Scanner;

public class LeapYear{
public static void main(String[] args){
boolean repeat;
String computeanother, yes="yes";
Scanner kb=new Scanner(System.in);
int year = -1;
boolean leap;
do
{ 
displayInstructions();
getYear(year);
leap = isLeap(year);
displayResults(year, leap);
System.out.println("Would you like to compute another year?");
computeanother = kb.nextLine();
repeat = computeanother.equals(yes);
}while(repeat);
 }  
public static void displayInstructions()
{
System.out.println("This program is designed to predict whether or not a year is a leap year.");
    System.out.println("When prompted please enter a positive number for the year.");
System.out.println("Once the program has run completely, it will state the year and whether it is a leap year.");
}

public static int getYear(int year)
{
    Scanner kb = new Scanner(System.in);
    do{
        System.out.println("Please enter the year.");
        year=kb.nextInt();
    }while (year < 0);
    return year; 
}

public static boolean isLeap(int year)
{
boolean leap;
year = getYear(year);
if ((year%4==0 && year%100 != 0) || year%400==0){
    leap = true;
    return true;}
else{
    leap = false;   
    return false;}
}

public static int displayResults(int year, boolean leap)
{
year = getYear(year);
if (leap == true){
   System.out.println("The year " +year); 
    System.out.println("is a leap year.");}
else{
   System.out.println("The year " +year); 
    System.out.println("is not a leap year.");}
return year;
}

}
while(repeat=true);

In the while loop should be: 在while循环中应该是:

while(repeat == true);

or 要么

while(repeat);

Aside from this being noted by everyone it can be noted you make this mistake twice: 除了每个人都注意到这一点,你可以注意到你犯了这个错误两次:

 if (leap = true) {

Should be: 应该:

if (leap == true) {

or 要么

if (leap) { 

You can also shorten your code: 您还可以缩短代码:

    do{ 
        displayInstructions();
        getYear(year);
        leap = isLeap(year);
        displayResults(year, leap);
        System.out.println("Would you like to compute another year?");
        computeanother = kb.nextLine();
        repeat = computeanother.equals(yes)  //this line makes code shorter 
    } while(repeat);  

Indeed, always avoid code redundancy like this famous pattern: 确实,总是避免代码冗余,就像这个着名的模式:

if(expression) return true; else return false;

That becomes: return expression; 那就变成了: return expression;

Change this: 改变这个:

while(repeat=true);

to

while(repeat==true);

or 要么

while(repeat);

Here while(repeat=true); 这里while(repeat=true); you are assigning a value, not comparing. 你正在分配一个值,而不是比较。 while(repeat==true); or while(repeat); 或者while(repeat); this will compare the value. 这将比较价值。 It's always better to test like this while(repeat); 这样测试总是更好while(repeat); instead of obvious while(repeat==true); 而不是显而易见的while(repeat==true); . I hope it helps. 我希望它有所帮助。

And you are not getting value for year as -1 because, you are returning from this method getYear(year); 并且你没有获得year价值-1因为,你从这个方法返回getYear(year); but ignoring the value. 但忽略了价值。 Change it to: 将其更改为:

year = getYear(year);

This should work. 这应该工作。

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

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