public class ZW{ public static void main(String[] args) { System.out.println("------for------"); String s = "abcdefghijklmn"; for (int i = 0; i <s.length(); i++) { System.out.println(s.charAt(i)); }
System.out.println("-------while-----"); int i=0; while (i<s.length()) { System.out.println(s.charAt(i)); i++; } } }
可以先转为数组,就能用for-each读啦
//String s = "abcdefghijklmn"; //分别用for,switch,for-earch,while循环把s读出来 import java.util.*; public class ReadString {
public void doWhile(String s) { int m=0; while(m<s.length()) { System.out.println("dowhile: "+s.charAt(m)); m++; } } public static void main(String[] args) { String s = "abcdefghijklmn"; ReadString rstr = new ReadString(); rstr.doFor(s); rstr.doSwitch(s); rstr.doForEarch(s); rstr.doWhile(s); } }以后作业还是自己写
结果是: for: a for: b for: c for: d for: e for: f for: g for: h for: i for: j for: k for: l for: m for: n switch: a switch: b switch: c switch: d switch: b switch: c switch: d switch: c switch: d switch: d for-earch; a for-earch; b for-earch; c for-earch; d for-earch; e for-earch; f for-earch; g for-earch; h for-earch; i for-earch; j for-earch; k for-earch; l for-earch; m for-earch; n dowhile: a dowhile: b dowhile: c dowhile: d dowhile: e dowhile: f dowhile: g dowhile: h dowhile: i dowhile: j dowhile: k dowhile: l dowhile: m dowhile: n
public static void main(String[] args) {
System.out.println("------for------");
String s = "abcdefghijklmn";
for (int i = 0; i <s.length(); i++) {
System.out.println(s.charAt(i));
}
System.out.println("-------while-----");
int i=0;
while (i<s.length()) {
System.out.println(s.charAt(i));
i++;
}
}
}
//String s = "abcdefghijklmn";
//分别用for,switch,for-earch,while循环把s读出来
import java.util.*;
public class ReadString
{
public void doFor(String s)
{
for(int i=0;i<s.length();i++)
{
System.out.println("for: "+s.charAt(i));
}
}
public void doSwitch(String s)
{
for(int i=0;i<s.length();i++)
switch(s.charAt(i))
{
case 'a' : System.out.println("switch: "+"a");
case 'b' : System.out.println("switch: "+"b");
case 'c' : System.out.println("switch: "+"c");
case 'd' : System.out.println("switch: "+"d");
//.....后面把字母表都列上去就行了
}
}
public void doForEarch(String s)
{
ArrayList<Character> str = new ArrayList<Character>();
for(int i=0;i<s.length();i++)
{
str.add(s.charAt(i));
}
for(Character strChar:str)
{
System.out.println("for-earch; "+strChar);
}
}
public void doWhile(String s)
{
int m=0;
while(m<s.length())
{
System.out.println("dowhile: "+s.charAt(m));
m++;
}
}
public static void main(String[] args)
{
String s = "abcdefghijklmn";
ReadString rstr = new ReadString();
rstr.doFor(s);
rstr.doSwitch(s);
rstr.doForEarch(s);
rstr.doWhile(s);
}
}以后作业还是自己写
for: a
for: b
for: c
for: d
for: e
for: f
for: g
for: h
for: i
for: j
for: k
for: l
for: m
for: n
switch: a
switch: b
switch: c
switch: d
switch: b
switch: c
switch: d
switch: c
switch: d
switch: d
for-earch; a
for-earch; b
for-earch; c
for-earch; d
for-earch; e
for-earch; f
for-earch; g
for-earch; h
for-earch; i
for-earch; j
for-earch; k
for-earch; l
for-earch; m
for-earch; n
dowhile: a
dowhile: b
dowhile: c
dowhile: d
dowhile: e
dowhile: f
dowhile: g
dowhile: h
dowhile: i
dowhile: j
dowhile: k
dowhile: l
dowhile: m
dowhile: n