2013年9月28日星期六

A small program output questions

Just learning JAVA, the first question is to determine whether the input is a leap year , as follows:
package experimental one ;
import java.io.IOException;

public class Year {
/ ** use if ... else statement to construct multi-branch , to determine whether a particular year is a leap year . Leap year is in line with the conditions under
face either: divisible by 4 but not divisible by 100 ; evenly divisible by four , but also divisible by 400 ** /

public static void main (String [] args) throws IOException {
/ / byte [] b = new byte [1000];
/ / int y = System.in.read (b);
int y = System.in.read ();
int a = y% 4;
int c = y% 100;
int d = y% 400;
if (a == 0 && c! = 0 | | d == 0)
{
System.out.println (y + " is a leap year " ) ;
}
else
{
System.out.println (y + " not a leap year " ) ;
}
}

}
Enter "2000" after the output is " 50 years is not a leap year ." I do not know what went wrong , why is there a "50" of the

------ Solution ------------------------------------ --------
int y = System.in.read (); this is a problem, go see Inputstream read method API
------ Solution ---- ----------------------------------------
I have just less than a self- weeks. LZ write a good feeling trouble .
This is my own writing , no other meaning .
import java.util. *;
public class lianxi {
public static void main (String [] args) {
Scanner input = new Scanner (System.in);
System.out.println (" Please enter a year: " ) ;
int year = input.nextInt ();
if (year% 4 == 0 && year% 100! = 0) {
System.out.println ();
} else if (year% 400 == 0) {
System.out.println (year + " is a leap year " ) ;
}
}
}
------ Solution ----------------------------------- ---------
import java.util.Scanner;

public class Runnian{

public static void main(String[] args) throws Exception{

Scanner scanner = new Scanner(System.in);
int year = scanner.nextInt();

int a = year%4;
int b = year%400;
int c = year%100;

if(a==0 && c!=0){
System.out.println(year+"is Runnian!");
}else if(a==0 && b==0){
System.out.println(year+"is Runnian!");
}else{
System.out.println(year+"is not Runnian!");
}
}

}
Proposed landlord wrote , enter 2000 for 50 shows this problem is System.in.read () this method caused . read () method to read a character that you enter 2000 , he only read the first one that is '2 ' this character , and this character through the ASC code will turn into an int by ASC code table query '2' the ASC code is 50 characters , so the show is 50 , it is recommended that way with my landlord to get the terminal parameters.
------ For reference only -------------------------------------- -
public abstract int read () throws IOException

from the input stream to read the next data byte. Return within the range 0-255 int byte value. If the end of the stream has been reached because no byte is available , the return value is -1 . In the input data is available, the end of the stream is detected , or an exception is thrown , this method has been blocked.

Returns:
next data byte, if the end of the stream is reached , -1 is returned.

------ For reference only ---------------------------------- -----
Thank you parents learned a reply ~ ~ ~
has been run successfully

没有评论:

发表评论