JAVA練習
.
import java.util.Scanner;
public class T {
public static void main(String[] args) {
System.out.println("5+5="+5+5);
}
}
.
(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);
}
}
.
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);
}
}
.
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);
}
}
.
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);
}
}
.
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");
}
}
}
JAVA練習