(1)import java.util.Scanner; //导包
class Test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //创建键盘录入对象
System.out.println("请输入第一个整数"); //提示语
int x = sc.nextInt(); //把录入的值赋值给x
System.out.println("请输入第二个整数"); //提示语
int y = sc.nextInt(); //把录入的值赋值给y
boolean b = (x == y); //判断
System.out.println(b);
}
}
(2)import java.util.Scanner;
class Test2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个整数");
int x = sc.nextInt();
System.out.println("请输入第二个整数");
int y = sc.nextInt();
int min = (x < y) ? x : y;
System.out.println(min);
}
}
(3)import java.util.Scanner;
class Test3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个整数");
int x = sc.nextInt();
System.out.println("请输入第二个整数");
int y = sc.nextInt();
System.out.println("请输入第三个整数");
int z = sc.nextInt();
int temp;
int min = (temp = x < y ? x : y) < z ? temp : z;
System.out.println(min);
}
}
2.看程序写结果:请自己独立分析,先不要编译运行。
第一题
int x = 1,y = 1;
if(x++==2 & ++y==2)
{
x =7;
}
System.out.println("x="+x+",y="+y);
x = 2 , y = 2
---------------------------------------------------
第二题
int x = 1,y = 1;
if(x++==2 && ++y==2)
{
x =7;
}
System.out.println("x="+x+",y="+y);
x = 2 , y = 1
---------------------------------------------------
第三题
int x = 1,y = 1;
if(x++==1 | ++y==1)
{
x =7;
}
System.out.println("x="+x+",y="+y);
x = 7 , y = 2
---------------------------------------------------
第四题
int x = 1,y = 1;
if(x++==1 || ++y==1)
{
x =7;
}
System.out.println("x="+x+",y="+y);
x = 7 , y = 1
---------------------------------------------------
第五题
boolean b = true;
import java.util.Scanner;
class Test6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个整数");
int x = sc.nextInt();
System.out.println("请输入第二个整数");
int y = sc.nextInt();
System.out.println("请输入第三个整数");
int z = sc.nextInt();
if (x > y)
{
if (y > z) //if嵌套
{
System.out.println(z +"...."+ y +"...."+ x);
}else {
if (x > z)
{
System.out.println(y +"...."+ z +"...."+ x);
}else {
System.out.println(y +"...."+ x +"...."+ z);
}
}
}else {
if (x > z)
{
System.out.println(z +"...."+ x +"...."+ y);
}else {
if (y > z)
{
System.out.println(x +"...."+ z +"...."+ y);
}else {
System.out.println(x +"...."+ y +"...."+ z);
}
}
}
}
}
8.看程序,分析下面程序的结果:
int x = 2,y=3;
switch(x)
{
default:
y++;
case 3:
y++;
break;
case 4:
y++;
}