1, ternary operator: The returned data is a variable, be sure to assign a value to the variable.
public class test1{
public static void Main (string[] args) {
int number1 = 10;
int number2 = 8;
int number3 = 6;
int max = 0;
max = number1>number2?number1:number2;
System.out.println ("Max Max:" +max);
}
}
2, the condition must be x==0,== is equal to; = indicates an assignment. "=" Error: Boolean type, data type conflict.
Import Java.util.Scanner;
public class test2{
public static void Main (string[] args) {
Scanner scan = new Scanner (system.in);
Double x = scan.nextdouble ();
int y=0;
if (x>0) {
y = 1;
}else if (x==0) {
y = 0;
}else if (x<0) {
y =-1;
} System.out.println (the value of "Y is:" +y); } }
3, using the conditional structure to enter a month from the keyboard, print out the corresponding season (spring, summer, autumn, winter) with the solar calendar said, 12, 1, 2 is the winter, 3, 4, 5 is the spring, 6, 7, 8 is the summer, 9, 10, 11 is autumn.
Import Java.util.Scanner;
public class test3{
public static void Main (string[] args) {
Scanner scan = new Scanner (system.in);
--Get month
int month = Scan.nextint ();
if (month==12| | month==1| | month==2) {
System.out.println ("Winter");
}else if (month==3| | month==4| | month==5) {
System.out.println ("Spring");
}else if (month==6| | month==7| | MONTH==8) {
System.out.println ("Summer");
}else if (month==9| | month==10| | month==11) {
System.out.println ("Autumn");
}else{
SYSTEM.OUT.PRINTLN ("You entered the wrong data, please re-check!!!" ");
}
}
}
4, in the console from the keyboard input three integers, according to the order from large to small output---------compare the size of three numbers, ascending, descending.
Import Java.util.Scanner;
public class test4{
public static void Main (string[] args) {
Scanner scan = new Scanner (system.in);
System.out.println ("Please enter the first positive number:");
int number1 = Scan.nextint ();
System.out.println ("Please enter a second positive number:");
int number2 = Scan.nextint ();
System.out.println ("Please enter a third positive number:");
int number3 = Scan.nextint ();
if (Number1>number2&&number1>number3) {
if (Number2>number3) {
System.out.println ("NUM1:" +number1+ "num2:" +number2+ "num3:" +number3 ");
}else{
System.out.println ("NUM1:" +number1+ "num3:" +number3+ "num2:" +number2 ");
}
}else if (number2>number1&&number2>number3) {
if (Number1>number3) {
System.out.println ("num2:" +number2+ "NUM1:" +number1+ "num3:" +number3 ");
}else{
System.out.println ("num2:" +number2+ "num3:" +number3+ "NUM1:" +number1 ");
}
}else if (number3>number1&&number3>number2) {
if (number1>number2) {
System.out.println ("num3:" +number3+ "NUM1:" +number1+ "num2:" +number2 ");
}else{
System.out.println ("num3:" +number3+ "num2:" +number2+ "NUM1:" +number1 "); }
} } }
5, the realization from the keyboard input year, month, judge whether the year is leap years or common year, judge the corresponding month of the number of days (judging the conditions of the leap: can be divisible by 400, or can be divisible by 4 but not divisible by 100) leap February: 29 days, Common year February: 28 days.
Import Java.util.Scanner;
public class test5{
public static void Main (string[] args) {
Scanner scan = new Scanner (system.in);
System.out.println ("Please enter a year:");
int year = Scan.nextint ();
System.out.println ("Please enter a month:");
int month = Scan.nextint ();
--Define a variable: Determine whether it is a leap year (true) or common year (false)
Boolean flag = true;
if (year%400==0| | (year%4==0&&year%100>0)) {
Flag = true;
System.out.println (year+ "Years is a leap year");
}else{
Flag = false;
SYSTEM.OUT.PRINTLN (year+ "Year is common year"); }
--1, 3, 5, 7, 8, 10, 12 are all 31 days, others except February, are 30 days
if (month==1| | month==3| | month==5| | month==7| | month==8| |month==10| | MONTH==12)
{
System.out.println (month+ "Month has 31 days"); }
else if (month==4| | month==6| | month==9| | month==11) {
System.out.println (month+ "Month has 30 days");
}else if (month==2) {
if (flag) {//-true
System.out.println (month+ "Month has 29 days");
}else{
System.out.println (month+ "Month has 28 days");
}
}else{
System.out.println ("The month you entered is wrong!!! ");
}
} }
Java applet (2015-8-6)