日期:2014-05-20  浏览次数:20595 次

(俺是新手,勿嘲笑)这段代码从书上抄下来的,但是报错不知道为什么
import java.util.*;
import static net.mindview.util.print.*;

public class VowelsAndConsonants{
  public static void main(String[] args){
  Random rand = new Random(47);
  for (int i=0;i<100;i++){
  int c = rand.nextInt(26) + 'a';//请问这句话是什么意思?包括nextInt()和 "+'a'"
  printnb((char)c + "," + c + ":");
  swith(c) {
  case 'a':
  case 'e':
  case 'i':
  case 'o':
  case 'u': print("vowel");
  break;
  case 'y':
  case 'w': print("Sometimes a vowel");
  break;
  default: print("Consonant")
  }
  }  
  }
}




//还有下面这段也有报错,再求高人替我解释一下下面这一段,有些看不懂
class Person {
  public void eat(Apple apple){
  Apple peeled = apple.getPeeled();
  System.out.println("Yummy");
  }
}


class Peeler {
  static Apple peel(Apple apple){
  return apple;
}

public class Passingthis {
  public static void main(String[] args) {
  new Person().eat(new Apple());
  }
}
 

------解决方案--------------------
Java code
import static net.mindview.util.print.*;

------解决方案--------------------
rand.nextInt(26),产生一个在0~25(包括0和25)之间的一个int型随机数.
rand.nextInt(26) +'a' 把那个随机数和字符'a'的整数值(97)相加。得到一个在97--122之间的一个整数。
------解决方案--------------------
int c = rand.nextInt(26) + 'a';//请问这句话是什么意思?包括nextInt()和 "+'a'"

rand.nextInt(n) 返回[0,n)中的随机正整数,注意后面是开区间,即范围是[0,n-1]
因为0 <= c <= 25,所以'a' - 'a' <= c <= 'z' - 'a' 即 'a' <= 'a' + c <= 'z'
所以'a' <= rand.nextInt(26) + 'a' <= 'z',整个表达式随机产生[a,z]中的一个小写字母