JAVA练习
1.
import java.util.Scanner;
public class T {
public static void main(String[] args) {
System.out.println("5+5="+5+5);
}
}
2.
(1)
import java.util.Scanner;
public class G{
public static void main(String[] args) {
int a=3,b;
b=a++;
sop("a="+a+",b="+b);
int a=3,b;
b=++a;
sop("a="+a+",b="+b);
}
}
(2).
public class H
{
public static void main (String[] args){
int a=3,b;
b=++a;
System.out.println("a="+a+",b="+b);
}
}
3.
import java.util.Scanner;
public class I {
public static void main(String[] args) {
short s=3;
s=s+4;
sop("s="+s);
short s=3;
s+=4;
sop("s="+s);
}
}
4.
import java.util.Scanner;
public class J{
public static void main(String[] args) {
sop(6&3);
sop(6|3);
sop(6^3);
sop(3<<2);
sop(3>>1);
}
}
5.
import java.util.Scanner;
public class K {
public static void main(String[] args) {
int x=0,y;
y=x>1?100:200;
sop("y="+y);
}
}
(1).输出两个整数中较大的整数
public class M
{
public static void main (String[] args){
int a=1,b=0,max;
max=a>b?a:b;
System.out.println("max="+a);
}
}
(2) 输出三个整数中最大的整数
public class M2
{
public static void main (String[] args){
int a=1,b=0,c=3,max;
max=a>b?a:b;
max=c>max?c:max;
System.out.println("max="+c);
}
}
6.
import java.util.Scanner;
public class N {
public static void main(String[] args) {
int weekDay=3;
if(weekDay==1){
sop("今天是星期一");
}
else if(weekDay==2){
sop("今天是星期二");
}
else if(weekDay==3){
sop("今天是星期三");
}
else{
sop("没有这一天");
}
int month=8;
}
}
(2)使用关联的if语句
public class O
{
public static void main (String[] args){
int weekDay=3;
if(weekDay==1){
System.out.println("今天是星期一");
}else if(weekDay==2){
System.out.println("今天是星期二");
}else if(weekDay==3){
System.out.println("今天是星期三");
}else if(weekDay==4){
System.out.println("今天是星期四");
}else if(weekDay==5){
System.out.println("今天是星期五");
}else if(weekDay==6){
System.out.println("今天是星期六");
}else if(weekDay==7){
System.out.println("今天是星期七");
}
else{
System.out.println("没有这一天");
}
}
}
练习.
public class P
{
public static void main (String[] args){
int month=3;
if(month==1){
System.out.println("这个月是冬天");
}
if(month==2){
System.out.println("这个月是冬天");
}
if(month==3){
System.out.println("这个月是春天");
}
if(month==4){
System.out.println("这个月是春天");
}
if(month==5){
System.out.println("这个月是春天");
}
if(month==6){
System.out.println("这个月是夏天");
}
if(month==7){
System.out.println("这个月是夏天");
}
if(month==8){
System.out.println("这个月是夏天");
}
if(month==9){
System.out.println("这个月是秋天");
}
if(month==10){
System.out.println("这个月是秋天");
}
if(month==11){
System.out.println("这个月是秋天");
}
if(month==12){
System.out.println("这个月是冬天");
}
System.out.println(month);
}
}
7.
import java.util.Scanner;
public class CH {
public static void main(String[] args) {
int a=84,b=3;
char option='%';
switch (option)
{
case '+':
System.out.println("a+b="+(a+b));
break;
case '-':
System.out.println("a-b="+(a-b));
break;
case '*':
System.out.println("a*b="+(a*b));
break;
case '/':
System.out.println("a/b="+(a/b));
break;
case '%':
System.out.println("a%b="+(a%b));
break;
default:
System.out.println("a%b="+(a%b));
break;
}
}
}
8.
import java.util.Scanner;
public class CH {
public static void main(String[] args) {
int x=1;
do{
sop("x="+x);
x++;
}
while(x<1);
}
}
import java.util.Scanner;
public class CH {
public static void main(String[] args) {
int y=1;
while(y<1){
sop("y="+y);
y++;
}
}
}
9.
import java.util.Scanner;
public class CH {
public static void main(String[] args) {
for(int x=1;x<3;x++)
{
sop("x="+x);
}
}
}
9.
import java.util.Scanner;
public class CH {
public static void main(String[] args) {
for(sop("a");sop("b");sop("c")
{
sop("d");
}
}
}