生活随笔
收集整理的這篇文章主要介紹了
编码练习——Java-1-基础
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
基礎練習
public class MyApp {
public static void main (String[] args) {System.
out .println(
"我能學好Java語言!" );}
}
public class Test {
public static void main (String[] args) {
byte mybyte =
124 ;
short myshort =
32564 ;
int myint =
45784612 ;
long mylong =
46789451 L;
long result = mybyte+myshort+myint+mylong;System.
out .println(
"四種類型相加的結果為:" +result);}
}
public class Test {public static void main (String[] args) {
float f1 =
12.23 f;
double d1 =
4562.12 d;
double d2 =
45678.1564 ;
double result = f1+d1+d2;System.out.println(
"浮點數相加達到結果為:" +result);}
}
public class Test {
public static void main (String[] args) {
int i =
'd' ;
char c =
97 ;System.
out .println(
"字符d的Unicode碼是:" +i);System.
out .println(
"Unicode碼97代表的字符是:" +c);}
}
public class Test {
public static void main (String[] args) {
char char1 =
'\\' ;
char char2 =
'\u2605' ;System.
out .println(
"輸出反斜杠:" + char1);System.
out .println(
"輸出五角星:" + char2);}
}
public class Test {
public static void main (String[] args) {
int i=
10 ;System.
out .println(
"i的初值是:" +i);i=
100 ;System.
out .println(
"i現在的值是:" +i);}
}
public class Test {public static void main (String[] args) {
final double PI =
3.1415926 F;
final boolean BOOL =
true ;}
}
public class Test {
static final
double PI =
3.14 ;
static int age =
23 ;
public static void main (String[] args) {final
int number;number =
1235 ;age =
22 ;System.
out .println(
"常量PI的值為:" +PI);System.
out .println(
"賦值后number的值為:" +number);System.
out .println(
"int型變量age的值為:" + age);}
}
public class Test {
static int times =
3 ;
public static void main (String[] args) {
int times =
4 ;System.
out .println(
"times的值為:" + times);System.
out .println(
"times的值為:" + Test.times);}
}
public class Test {
public static void main (String[] args) {
int a,b,c;a =
15 ;c = b = a+
4 ;System.
out .println(
"c的值為:" + c);System.
out .println(
"b的值為:" + b);}
}
public static void main (String[] args) {
float number1 =
45.56 f;
int number2 =
152 ;System.
out .println(
"45.56f和152的和為:" + (number1+number2));System.
out .println(
"45.56f和152的差為:" + (number1-number2));System.
out .println(
"45.56f和152的積為:" + (number1*number2));System.
out .println(
"45.56f和152的商為:" + (number1/number2));}
}
public class Test {
public static void main (String[] args) {
int number1 =
4 ;
int number2 =
5 ;System.
out .println(
"4>5是否成立:" + (number1>number2));System.
out .println(
"4<5是否成立:" + (number1<number2));System.
out .println(
"4==5是否成立:" + (number1==number2));System.
out .println(
"4!=5是否成立:" + (number1!=number2));System.
out .println(
"4>=5是否成立:" + (number1>=number2));System.
out .println(
"4<=5是否成立:" + (number1<=number2));}
}
public class Test {
public static void main (String[] args) {
int a =
2 ;
int b =
5 ;boolean result1 = ((a>b) && (a!=b));boolean result2 = ((a>b) ||(a!=b));System.
out .println(
"(a>b) && (a!=b)的值是:" + result1);System.
out .println(
"(a>b) ||(a!=b)的值是:" + result2);}
}
public class Test {
public static void main (String[] args) {
int i =
46 ;
int j =
97 ;
char c =
'a' ;System.
out .println(
"46 & 97 的值是:" + (i&j));System.
out .println(
"46 & 'a' 的值是:" + (i^c));System.
out .println(
"46 >>1 的值是:" + (i>>
1 ));}
}
public class Test {
public static void main (String[] args) {
int i =
46 ;
int j =
97 ;
int z = i>j ?
100 :
200 ;System.
out .println(
"i>j?100:200 的值是:" + z);}
}
import java.util.Scanner;
public class Test {public static void main (String[] args) {Scanner scan =
new Scanner(System.in);System.out.println(
"請輸入變量A的值:\n" );
long A = scan.nextLong();System.out.println(
"請輸入變狼B的值:\n" );
long B = scan.nextLong();System.out.println(
"A=" +A+
"\tB=" +B);System.out.println(
"執行變量互換..." );A = A^B;B = B^A;A = A^B;System.out.println(
"A=" +A+
"\tB=" +B);}
}
import java.util.Scanner;
public class Test {
public static void main (String[] args) {Scanner scan =
new Scanner(System.
in );System.
out .println(
"請輸入一個整數:" );
long number = scan.nextLong();String check = (number%
2 ==
0 ) ?
"這個數字是:偶數" :
"這個數字是:奇數" ;System.
out .println(check);}
}
public class Test {
public static void main (String[] args) {
float number1 =
45 f;
int number2 =
152 ;System.
out .println(number1+number2);}
}
public class Test {
public static void main (String[] args) {
byte mybyte =
127 ;
int myint =
150 ;
float myfloat =
452.12 f;
char mychar =
10 ;
double mydouble =
45.46546 ;System.
out .println(
"127 + 452.12 = " + (mybyte+myfloat));System.
out .println(
"127*150 = " + (mybyte*myint));System.
out .println(
"127/10 = " + (mybyte/mychar));System.
out .println(
"45.46546+10 = " + (mydouble+mychar));}
}
public class Test {
public static void main (String[] args) {
int a = (
int )
45.23 ;
long y = (
long )
456.6 F;
int b = (
int )
'a' ;System.
out .println(
"a = " + a);System.
out .println(
"y = " + y);System.
out .println(
"b = " + b);}
}
public class Test {
public static void main (String[] args) {
int intNum =
4 ;
float floatNum =
9.5 F;floatNum /= intNum; System.
out .println(
"9.5F/4 = " + floatNum);
double numX =
4.88 ;
double numY =
78.83 ;
int numZ = (
int )numX + (
int )numY; System.
out .println(
"4.88 + 78.83 = " + numZ);
char charVar =
'T' ;
int intVar = (
int ) charVar;System.
out .println(
"將字符T轉換成int型變量是:" + intVar);
int num1 =
34 ;
double num2 = (
double ) num1/
3 ;System.
out .println(
"34的三分之一是:" +num2);}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
public class Test {public static void main (String[] args) {
/*** 文檔注釋*/ }
}
import java.util.Scanner;
public class Test {
public static void main (String[] args) {Scanner scan =
new Scanner(System.
in );System.
out .println(
"請輸入一個年份:" );
long year = scan.nextLong();
if (year%
4 ==
0 && year%
100 !=
0 || year%
400 ==
0 ) {System.
out .println(year +
"年是閏年!" );}
else {System.
out .println(year +
"年不是閏年!" );}}
}
import java.util.Scanner;
public class Test {
public static void main (String[] args) {Scanner scan =
new Scanner(System.
in );System.
out .println(
"請輸入球形的半徑:" );
double r = scan.nextDouble();final
double PI =
3.1415926 ;
double volume =
4.0 /
3.0 *PI*r*r*r;System.
out .println(
"球形的半徑是:" + r);System.
out .println(
"圓周率是:" + PI);System.
out .println(
"球形的體積是:" + volume);}
}
public class Test {
public static void main (String[] args) {
int y =
40 ;System.
out .println(
"輸出y的值:" + y);
int z =
245 ;boolean b;{b = y>z;System.
out .println(
"y>z城里嗎:" + b);}}
}
from: http://blog.csdn.net/xuezhisdc/article/details/52174220
《新程序員》:云原生和全面數字化實踐 50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔 為你收集整理的编码练习——Java-1-基础 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。