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

时间:2013-02-22 16:20:27

标签: java methods variable-assignment user-input

我写的程序是确定一年是否是闰年。这是一个赋值,所以我需要使用我在程序中编写的四种方法。程序编译并运行,它在适当的位置请求用户输入,但它不会将输入带入程序。也就是说,无论输入什么,这一年都是闰年并且循环。我对于哪里出现错误非常困惑,因为这个程序似乎与我们给出的例子相符。

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.");
        }
    }

}

感谢大家的帮助!编辑后的代码如下所示:

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;
}

}

3 个答案:

答案 0 :(得分:1)

while(repeat=true);

在while循环中应该是:

while(repeat == true);

while(repeat);

除了每个人都注意到这一点之外,你可以注意到你犯了两次错误:

 if (leap = true) {

应该是:

if (leap == true) {

if (leap) { 

答案 1 :(得分:1)

您还可以缩短代码:

    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);  

确实,总是避免像这种着名模式那样的代码冗余:

if(expression) return true; else return false;

变为:return expression;

答案 2 :(得分:0)

改变这个:

while(repeat=true);

while(repeat==true);

while(repeat);

这里while(repeat=true);您要分配一个值,而不是比较。 while(repeat==true);while(repeat);这会比较价值。最好像这样while(repeat);而不是明显的while(repeat==true);进行测试。我希望它有所帮助。

并且您没有获得year的值为-1,因为您从此方法返回getYear(year);但忽略该值。将其更改为:

year = getYear(year);

这应该有用。