【问题标题】:Method in Java program won't pick up user inputJava 程序中的方法不会获取用户输入
【发布时间】:2013-02-08 08:00:26
【问题描述】:

我正在编写的程序是确定一年是否是闰年。这是一个作业,所以我需要使用我在程序中编写的四个方法。程序编译并运行,它在适当的地方要求用户输入,但它不接受输入到程序中。也就是说年份是闰年,无论输入什么都循环。我很困惑哪里有错误,因为这个程序似乎与我们给出的例子相匹配。

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

}

【问题讨论】:

  • 见这个:stackoverflow.com/questions/7056749/…,还要注意if (f(y)) x = true else x = false形式的任何代码都可以简化为x = f(y)
  • 假设这是程序的最终结果,您在 isLeap(int year) 中进行的布尔跳跃 - 方法没有任何作用。
  • @FlorisVelleman 我可以删除布尔值并告诉它返回真还是假吗?
  • @SMoore 是的,你可以:)

标签: java methods variable-assignment user-input


【解决方案1】:
while(repeat=true);

在while循环中应该是:

while(repeat == true);

while(repeat);

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

 if (leap = true) {

应该是:

if (leap == true) {

if (leap) { 

【讨论】:

  • 那么双等号只有在 if 条件下才需要?当我告诉程序跳跃为真时,我只使用一个等号,对吗?
  • == 是一个比较,而 = 用于分配一个值。所以:int x = 5;如果 (x == 12)
【解决方案2】:

改变这个:

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

这应该可行。

【讨论】:

  • 这确实让程序终止谢谢!但它仍然没有接收用户输入或针对闰年条件测试数字。你知道为什么会这样吗?
  • @SavannahMoore 不采摘手段?你能解释更多吗?
  • @SavannahMoore,请参阅我编辑的答案。它应该可以解决您的问题
  • 运行谢谢!我唯一担心的是,当它运行时,它会询问 3 次年份。你知道如何让它停止这样做吗?
【解决方案3】:

你也可以缩短你的代码:

    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;

【讨论】:

  • 这确实有助于缩短它。我没有意识到这是一个选项谢谢!
猜你喜欢
  • 2013-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多