簡體   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