我寫的程序是確定今年是否是閏年。這是一項任務,所以我需要使用我在程序中編寫的四種方法。該程序編譯並運行,它要求在適當的地方進行用戶輸入,但不會將輸入輸入到程序中。也就是說,無論輸入什麼內容,今年都是閏年。我非常困惑,因爲這個程序看起來與我們給出的例子相匹配。方法不會拿起用戶輸入
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;
}
}
看到這個:HTTP://堆棧溢出。com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx,還要注意,形式爲'if(f(y))x = true else x = false'的任何代碼都可以簡化爲x = f(y)' – 2013-02-22 16:27:38
假設這是程序的最終結果,則在isLeap(int year) - 方法中實現的布爾跳躍沒有任何用處。 – 2013-02-22 17:19:43
@FlorisVelleman我可以刪除布爾值,只是告訴它返回true或false? – SMoore 2013-02-22 17:24:38