package day2;
/**
*
* @author duhongyu 2018年11月23日
* 类型转换操作符
*
*/
public class Casting {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 200;
long lng = (long)i;
lng = i;//widening so cast no really required
long lng2 = (long)200;
lng2 = 200;
i = (int)lng2;
}
}
package day2;
import java.util.Random;
/**
*
* @author duhongyu 2018年11月23日
* Foreach语法
*
*/
public class ForEacheFloat {
public static void main(String[] args) {
// TODO Auto-generated method stub
Random rand = new Random(47);
float f[] = new float[10];
for(int i = 0;i<10;i++)
f[i] = rand.nextFloat();
for(float x: f)
System.out.println(x);
}
}
package day2;
/**
*
* @author duhongyu 2018年11月23日
* tocharArray
*
*/
public class ForEachString {
public static void main(String[] args) {
// TODO Auto-generated method stub
for(char c :"An China fly ".toCharArray())
{
System.out.print(c+" ");
}
}
}
package day2;
import static net.mindview.util.Print.*;
/**
*
* @author duhongyu 2018年11月23日
* if-else
*
*/
public class IfElse {
static int result = 0;
static void test(int testval,int target) {
if(testval>target)
result = +1;
else if (testval <target)
result = -1;
else result = 0;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
test(10,5);
print(result);
test(5,10);
print(result);
test(5,5);
print(result);
}
}
package day2;
/**
*
* @author duhongyu 2018年11月23日
* for 循环
*
*/
public class ListCharacters {
public static void main(String[] args) {
// TODO Auto-generated method stub
for (char c = 0 ;c<128;c++)
{
if(Character.isLowerCase(c)) {
System.out.println("value:"+(int)c+" character:"+c);
}
}
}
}
package day2;
import static net.mindview.util.Print.*;
/**
*
* @author duhongyu 2018年11月23日
* 操作符+ 和+=*
*/
public class StringOperators {
public static void main(String[] args) {
// TODO Auto-generated method stub
int x = 0,y = 1, z= 2;
String s = "x,y,z";
print(s+x+y+z);
print(x+ " "+s); ///Converts x to a String
s +="(sumed) = ";
print(s+(x+y+z));
print(""+x);
}
}
package day2;
import static net.mindview.util.Print.*;
/**
*
* @author duhongyu 2018年11月23日
* 三元操作符 if-else
*
*/
public class TernaryIfElse117 {
static int ternary(int i) {
return i<10?i*100:i*10;
}
static int standardIfElse(int i) {
if(i < 10)
return i*100;
else
return i*10;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
print(ternary(9));
print(ternary(10));
print(standardIfElse(9));
print(standardIfElse(10));
}
}
package day2;
import static net.mindview.util.Print.*;
/**
*
* @author duhongyu 2018年11月23日
*位移操作符
*
*/
public class URShift112 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = -1;
print(Integer.toBinaryString(i));
i >>>= 10;
print(Integer.toBinaryString(i));
long l = -1;
print(Long.toBinaryString(l));
l >>>= 10;
print(Long.toBinaryString(l));
short s = -1;
print(Integer.toBinaryString(s));
s >>>= 10;
print(Integer.toBinaryString(s));
byte b = -1;
print(Integer.toBinaryString(b));
b >>>= 10;
print(Integer.toBinaryString(b));
b = -1;
print(Integer.toBinaryString(b));
print(Integer.toBinaryString(b>>>10));
}
}
package day2;
/**
*
* @author duhongyu 2018年11月23日
* 迭代
*
*/
public class WhileTest {
static boolean condition() {
boolean result =Math.random()<0.99;
System.out.println(result+". ");
return result;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
while(condition())
System.out.println("Inside 'while;");
System.out.println("Exited 'while'");
}
}