繁体   English   中英

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

[英]Method in Java program won't pick up 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;
}

}
while(repeat=true);

在while循环中应该是:

while(repeat == true);

要么

while(repeat);

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

 if (leap = true) {

应该:

if (leap == true) {

要么

if (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)  //this line makes code shorter 
    } while(repeat);  

确实,总是避免代码冗余,就像这个着名的模式:

if(expression) return true; else return false;

那就变成了: return expression;

改变这个:

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

这应该工作。

暂无
暂无

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

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