0

私が書いているプログラムは、1年がうるう年であるかどうかを判断することです。これは課題なので、プログラム内で書いた4つのメソッドを使用する必要があります。プログラムはコンパイルして実行し、適切な場所でユーザー入力を要求しますが、プログラムへの入力は受け取りません。また、年はうるう年であり、何を入力してもループしていると言っています。このプログラムは私たちが与えられた例と一致しているように見えるので、私はエラーがどこにあるかについて非常に混乱しています。

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

}
4

3 に答える 3

1
while(repeat=true);

whileループは次のようになります。

while(repeat == true);

また

while(repeat);

これがすべての人に指摘されていることを除けば、この間違いを2回犯していることに注意することができます。

 if (leap = true) {

する必要があります:

if (leap == true) {

また

if (leap) { 
于 2013-02-22T16:24:36.247 に答える
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;

于 2013-02-22T16:31:01.660 に答える
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);

これは機能するはずです。

于 2013-02-22T16:24:48.690 に答える