你要注意变量作用域JAVA程序块内定义的变量 ,只能在程序块内使用如你的 int mk=wst.nextInt();      int yw=mk;只能用于 case 1: 到 break间,出了这个范围就不能用了你的  sum=yw+yy+sx;中的yw,yy,sx都是没有定义的变量所以尽量在一个函数内且是程序开头定义变量,不要随时定义变量(特别是初学,对作用域概念不很清)改好的程序:import java.util.*;public class xh_while{  public static void main(String[] args){  Scanner wst=new Scanner(System.in);  System.out.println("请输入学员的姓名:");  String student=wst.next();  int subject=1;  int sum=0;  int yw=0,yy=0,sx=0; while(subject<=3){    switch(subject){      case 1:      System.out.println("请输入"+student+"的语文成绩");      yw=wst.nextInt();      break;      case 2:      System.out.println("请输入"+student+"的英语成绩");      yy=wst.nextInt();      break;      case 3:      System.out.println("请输入"+student+"的数学成绩");      sx=wst.nextInt();      break;      }    subject++;  }  sum=yw+yy+sx; System.out.println(student+"学生的总成绩是"+sum); }}