本帖最后由 月之吟 于 2015-2-17 18:18 编辑
No 01import java.util.Scanner;
public class No01 {
public static void main(String[] args) {
int i;
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要判断的整数:");
i = sc.nextInt();
if (i % 2 != 0) {
System.out.println("该整数是奇数");
} else {
System.out.println("该整数不是奇数");
}
sc.close();
}
}
No 02
for循环改为for(int i = 1; i <= 20; i++),把 t+i 强制转型为 short。把 t 定义成 int 型也行吧。public class No02 {
public static void main(String args[]) {
short t = 0;
for (int i = 1; i <= 20; i++) {
t = (short) (t + i);
}
System.out.println(t);
}
}
No 03public class No03 {
public static void main(String[] args) {
int num = 5;
for (int i = 0; i < num; i++) {
for (int m = 0; m < i; m++) {
System.out.print(" ");
}
for (int n = 0; n < (num - i) * 2 - 1; n++) {
System.out.print("*");
}
System.out.println();
}
}
}
No 04
将 if 中的60*1000用括号括起来。public class No04 {
public static void main(String[] args) {
int minutes = 0;
for (int ms = 0; ms < 60 * 60 * 1000; ms++) {
if (ms % (60 * 1000) == 0) {
minutes++;
}
}
System.out.println(" minutes = " + minutes);
}
}
No 05import java.util.Scanner;
public class No05_1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要反转的字符串:");
String s1 = sc.nextLine();
String s2 = new String();
for (int i = s1.length() - 1; i >= 0; i--) {
s2 += s1.charAt(i);
}
System.out.println("反转后的字符串为:\n" + s2);
sc.close();
}
}
上面不知道一个字符一个字符加到s2上是不是不好……就又写了下面的……import java.util.Scanner;
public class No05_2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要反转的字符串:");
String s1 = sc.nextLine();
char[] ch = new char[s1.length()];
for (int i = 0; i < s1.length(); i++) {
ch[i] = s1.charAt(s1.length() - i - 1);
}
String s2 = new String();
s2 = String.valueOf(ch);
System.out.println("反转后的字符串为:\n" + s2);
sc.close();
}
}
不过如果直接用StringBuilder的reverse方法应该不算吧……
No 06
2.0和1.1后面都加f。只会这么改了- -public class No06 {
public static void main(String[] args) {
System.out.println(2.0f - 1.1f);
}
}
No 07import java.util.Scanner;
public class No07 {
public static void main(String[] args) {
int word = 0, num = 0, cn = 0;
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要统计的字符串:");
String s = sc.nextLine();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= 48 && s.charAt(i) <= 57) {
num++;
}
if (s.charAt(i) >= 65 && s.charAt(i) <= 90) {
word++;
}
if (s.charAt(i) >= 97 && s.charAt(i) <= 122) {
word++;
}
if (s.charAt(i) >= 19968 && s.charAt(i) <= 40869) {
cn++;
}
}
System.out.println("有" + num + "个数字");
System.out.println("有" + word + "个英文");
System.out.println("有" + cn + "个汉字");
sc.close();
}
}
No 08import java.util.Scanner;
public class No08 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串:");
String s = sc.nextLine();
System.out.println(s + "字符串的长度为" + s.length());
sc.close();
System.out.println("a\\u0022.length()+\\u0022b的字符串长度为");
System.out.println("a\\u0022.length()+\\u0022b".length());
}
}
上面这个的"a\u0022.length()+\u0022b"的长度也只会这么着了- -
No 09public class No09 {
public static void main(String[] args) {
int x = 2014, y = 2015;
System.out.println("交换前的值为:x=" + x + " y=" + y);
x = x ^ y;
y = x ^ y;
x = x ^ y;
System.out.println("交换后的值为:x=" + x + " y=" + y);
}
}
No 10public class No10 {
public static void main(String[] args) {
System.out.println("2*8=" + (2 << 3));
}
}
有错或者不好也不要笑话我哦~
|